From cb785d5035b88e63f356726f6f50039617f8c5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sun, 31 Mar 2024 16:57:40 +0200 Subject: [PATCH] relay/api: add "request" and "request_body" in response when connected via websocket (issue #2066) --- src/plugins/relay/api/relay-api-msg.c | 28 ++++++++++++++-- .../relay/api/test-relay-api-protocol.cpp | 33 +++++++++++-------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/plugins/relay/api/relay-api-msg.c b/src/plugins/relay/api/relay-api-msg.c index 6f5cc30c6..d6af528f9 100644 --- a/src/plugins/relay/api/relay-api-msg.c +++ b/src/plugins/relay/api/relay-api-msg.c @@ -121,9 +121,9 @@ relay_api_msg_send_json_internal (struct t_relay_client *client, cJSON *json_body) { cJSON *json, *json_event; - int num_bytes; + int num_bytes, length; const char *ptr_id; - char *string, *error; + char *string, *error, *request; long long id; if (!client || !message) @@ -165,6 +165,30 @@ relay_api_msg_send_json_internal (struct t_relay_client *client, cJSON_AddItemToObject (json, "event", json_event); } } + else + { + length = ((client->http_req->method) ? strlen (client->http_req->method) : 0) + + 1 + + ((client->http_req->path) ? strlen (client->http_req->path) : 0) + + 1; + request = malloc (length); + if (request) + { + snprintf ( + request, length, + "%s%s%s", + (client->http_req->method) ? client->http_req->method : "", + (client->http_req->method) ? " " : "", + (client->http_req->path) ? client->http_req->path : ""); + cJSON_AddItemToObject (json, "request", + cJSON_CreateString (request)); + cJSON_AddItemToObject ( + json, "request_body", + (client->http_req->body) ? + cJSON_Parse (client->http_req->body) :cJSON_CreateNull ()); + free (request); + } + } if (json_body) { cJSON_AddItemToObject (json, "body_type", 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 1a06d9a8d..53fa7c247 100644 --- a/tests/unit/plugins/relay/api/test-relay-api-protocol.cpp +++ b/tests/unit/plugins/relay/api/test-relay-api-protocol.cpp @@ -54,9 +54,12 @@ extern int relay_api_protocol_command_delay; data_sent, \ strlen ("HTTP/1.1 " #__code " " __message "\r\n")); -#define WEE_CHECK_TEXT(__code, __message) \ +#define WEE_CHECK_TEXT(__code, __message, __request, __body) \ STRCMP_EQUAL("{\"code\":" #__code "," \ - "\"message\":\"" __message "\"}", \ + "\"message\":\"" __message "\"," \ + "\"request\":\"" __request "\"," \ + "\"request_body\":" __body "" \ + "}", \ data_sent); #define WEE_CHECK_OBJ_STR(__expected, __json, __name) \ @@ -671,7 +674,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket) data_sent); test_client_recv_text ("{\"request\": \"POST /api/sync\"}"); - WEE_CHECK_TEXT(204, "No Content"); + WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "null"); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled)); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks)); @@ -679,7 +682,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket) test_client_recv_text ("{\"request\": \"POST /api/sync\", " "\"body\": {\"sync\": false}}"); - WEE_CHECK_TEXT(204, "No Content"); + WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":false}"); LONGS_EQUAL(0, RELAY_API_DATA(ptr_relay_client, sync_enabled)); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks)); @@ -687,7 +690,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket) test_client_recv_text ("{\"request\": \"POST /api/sync\", " "\"body\": {\"sync\": true, \"nicks\": false}}"); - WEE_CHECK_TEXT(204, "No Content"); + WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":true,\"nicks\":false}"); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled)); LONGS_EQUAL(0, RELAY_API_DATA(ptr_relay_client, sync_nicks)); @@ -695,7 +698,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket) test_client_recv_text ("{\"request\": \"POST /api/sync\", " "\"body\": {\"sync\": true, \"nicks\": true, \"colors\": \"weechat\"}}"); - WEE_CHECK_TEXT(204, "No Content"); + WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":true,\"nicks\":true,\"colors\":\"weechat\"}"); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled)); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks)); @@ -703,7 +706,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket) test_client_recv_text ("{\"request\": \"POST /api/sync\", " "\"body\": {\"sync\": true, \"nicks\": true, \"colors\": \"strip\"}}"); - WEE_CHECK_TEXT(204, "No Content"); + WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":true,\"nicks\":true,\"colors\":\"strip\"}"); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled)); LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks)); @@ -735,27 +738,31 @@ TEST(RelayApiProtocolWithClient, RecvJson) /* error: empty string */ test_client_recv_text (""); - WEE_CHECK_TEXT(400, "Bad Request"); + WEE_CHECK_TEXT(400, "Bad Request", "", "null"); /* error: empty body */ test_client_recv_text ("{}"); - WEE_CHECK_TEXT(400, "Bad Request"); + WEE_CHECK_TEXT(400, "Bad Request", "", "null"); /* error: empty request */ test_client_recv_text ("{\"request\": \"\"}"); - WEE_CHECK_TEXT(400, "Bad Request"); + WEE_CHECK_TEXT(400, "Bad Request", "", "null"); /* error: invalid request (number) */ test_client_recv_text ("{\"request\": 123}"); - WEE_CHECK_TEXT(400, "Bad Request"); + WEE_CHECK_TEXT(400, "Bad Request", "", "null"); /* error: invalid request (string, not a valid request) */ test_client_recv_text ("{\"request\": \"abc\"}"); - WEE_CHECK_TEXT(400, "Bad Request"); + WEE_CHECK_TEXT(400, "Bad Request", "", "null"); /* error: invalid request (string, resource not found) */ test_client_recv_text ("{\"request\": \"GET /api/unknown\"}"); - WEE_CHECK_TEXT(404, "Not Found"); + WEE_CHECK_TEXT(404, "Not Found", "GET /api/unknown", "null"); + + /* error: invalid request (string, resource not found) */ + test_client_recv_text ("{\"request\": \"GET /api/unknown\", \"body\": {\"test\": 123}}"); + WEE_CHECK_TEXT(404, "Not Found", "GET /api/unknown", "{\"test\":123}"); } /*