1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-12 14:14:48 +02:00

relay/api: add support of buffer id in GET /api/buffers (issue #2081)

This commit is contained in:
Sébastien Helleu
2024-03-10 14:21:44 +01:00
parent 7ad3c1979c
commit aa989767a1
2 changed files with 51 additions and 2 deletions
+20 -1
View File
@@ -41,6 +41,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,
@@ -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");