mirror of
https://github.com/weechat/weechat.git
synced 2026-07-07 10:13:12 +02:00
relay/api: add "request" and "request_body" in response when connected via websocket (issue #2066)
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user