diff options
-rw-r--r-- | src/plugins/relay/api/relay-api-protocol.c | 21 | ||||
-rw-r--r-- | tests/unit/plugins/relay/api/test-relay-api-protocol.cpp | 32 |
2 files changed, 51 insertions, 2 deletions
diff --git a/src/plugins/relay/api/relay-api-protocol.c b/src/plugins/relay/api/relay-api-protocol.c index 61b0b4a43..7bab96505 100644 --- a/src/plugins/relay/api/relay-api-protocol.c +++ b/src/plugins/relay/api/relay-api-protocol.c @@ -42,6 +42,21 @@ int relay_api_protocol_command_delay = 1; /* delay to execute command */ /* + * Searches buffer by id or full name. + */ + +struct t_gui_buffer * +relay_api_search_buffer_id_name (const char *string) +{ + struct t_gui_buffer *ptr_buffer; + + ptr_buffer = weechat_buffer_search ("==id", string); + if (ptr_buffer) + return ptr_buffer; + return weechat_buffer_search ("==", string); +} + +/* * Callback for signals "buffer_*". */ @@ -358,6 +373,10 @@ RELAY_API_PROTOCOL_CALLBACK(version) * * Routes: * GET /api/buffers + * GET /api/buffers/{buffer_id} + * GET /api/buffers/{buffer_id}/lines + * GET /api/buffers/{buffer_id}/lines/{line_id} + * GET /api/buffers/{buffer_id}/nicks * GET /api/buffers/{buffer_name} * GET /api/buffers/{buffer_name}/lines * GET /api/buffers/{buffer_name}/lines/{line_id} @@ -375,7 +394,7 @@ RELAY_API_PROTOCOL_CALLBACK(buffers) ptr_buffer = NULL; if (client->http_req->num_path_items > 2) { - ptr_buffer = weechat_buffer_search ("==", client->http_req->path_items[2]); + ptr_buffer = relay_api_search_buffer_id_name (client->http_req->path_items[2]); if (!ptr_buffer) { relay_api_msg_send_error_json (client, RELAY_HTTP_404_NOT_FOUND, NULL, diff --git a/tests/unit/plugins/relay/api/test-relay-api-protocol.cpp b/tests/unit/plugins/relay/api/test-relay-api-protocol.cpp index fb2cb823f..96ae2aa62 100644 --- a/tests/unit/plugins/relay/api/test-relay-api-protocol.cpp +++ b/tests/unit/plugins/relay/api/test-relay-api-protocol.cpp @@ -338,6 +338,7 @@ TEST(RelayApiProtocolWithClient, CbVersion) TEST(RelayApiProtocolWithClient, CbBuffers) { cJSON *json, *json_obj, *json_var, *json_groups; + char str_http[256]; /* error: invalid buffer name */ test_client_recv_http ("GET /api/buffers/invalid", NULL); @@ -348,6 +349,15 @@ TEST(RelayApiProtocolWithClient, CbBuffers) "{\"error\": \"Buffer \\\"invalid\\\" not found\"}", data_sent); + /* error: invalid buffer id */ + test_client_recv_http ("GET /api/buffers/123", NULL); + STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n" + "Content-Type: application/json; charset=utf-8\r\n" + "Content-Length: 37\r\n" + "\r\n" + "{\"error\": \"Buffer \\\"123\\\" not found\"}", + data_sent); + /* error: invalid sub-resource */ test_client_recv_http ("GET /api/buffers/core.weechat/invalid", NULL); STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n" @@ -385,7 +395,7 @@ TEST(RelayApiProtocolWithClient, CbBuffers) WEE_CHECK_OBJ_STR("core", json_var, "plugin"); WEE_CHECK_OBJ_STR("weechat", json_var, "name"); - /* get one buffer */ + /* get one buffer by name */ test_client_recv_http ("GET /api/buffers/core.weechat", NULL); WEE_CHECK_HTTP_CODE(200, "OK"); CHECK(json_body_sent); @@ -403,6 +413,26 @@ TEST(RelayApiProtocolWithClient, CbBuffers) WEE_CHECK_OBJ_STR("core", json_var, "plugin"); WEE_CHECK_OBJ_STR("weechat", json_var, "name"); + /* get one buffer by id */ + snprintf (str_http, sizeof (str_http), + "GET /api/buffers/%lld", gui_buffers->id); + test_client_recv_http (str_http, NULL); + WEE_CHECK_HTTP_CODE(200, "OK"); + CHECK(json_body_sent); + CHECK(cJSON_IsObject (json_body_sent)); + json = json_body_sent; + WEE_CHECK_OBJ_NUM(gui_buffers->id, json, "id"); + WEE_CHECK_OBJ_STR("core.weechat", json, "name"); + WEE_CHECK_OBJ_STR("weechat", json, "short_name"); + WEE_CHECK_OBJ_NUM(1, json, "number"); + WEE_CHECK_OBJ_STR("formatted", json, "type"); + WEE_CHECK_OBJ_STRN("WeeChat", 7, json, "title"); + json_var = cJSON_GetObjectItem (json, "local_variables"); + CHECK(json_var); + CHECK(cJSON_IsObject (json_var)); + WEE_CHECK_OBJ_STR("core", json_var, "plugin"); + WEE_CHECK_OBJ_STR("weechat", json_var, "name"); + /* get the 2 last lines of core buffer */ gui_chat_printf (NULL, "test line 1"); gui_chat_printf (NULL, "test line 2"); |