mirror of
https://github.com/weechat/weechat.git
synced 2026-07-01 15:26:37 +02:00
relay/api: always return a body with field "error" in error responses
This commit is contained in:
@@ -54,14 +54,15 @@ extern int relay_api_protocol_command_delay;
|
||||
data_sent[0], \
|
||||
strlen ("HTTP/1.1 " #__code " " __message "\r\n"));
|
||||
|
||||
#define WEE_CHECK_TEXT(__code, __message, __request, __body) \
|
||||
#define WEE_CHECK_TEXT(__code, __message, __request, __request_body, \
|
||||
__body) \
|
||||
STRCMP_EQUAL("{\"code\":" #__code "," \
|
||||
"\"message\":\"" __message "\"," \
|
||||
"\"request\":\"" __request "\"," \
|
||||
"\"request_body\":" __body "," \
|
||||
"\"request_body\":" __request_body "," \
|
||||
"\"request_id\":null," \
|
||||
"\"body_type\":null," \
|
||||
"\"body\":null" \
|
||||
"\"body\":" __body \
|
||||
"}", \
|
||||
data_sent[0]);
|
||||
|
||||
@@ -407,11 +408,12 @@ TEST(RelayApiProtocolWithClient, CbBuffers)
|
||||
/* error: too many parameters in path */
|
||||
test_client_recv_http ("GET /api/buffers/core.weechat/too/many/parameters",
|
||||
NULL, NULL);
|
||||
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
|
||||
STRCMP_EQUAL("HTTP/1.1 400 Bad Request\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 58\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Bad request: too many path parameters (max: 3)\"}",
|
||||
data_sent[0]);
|
||||
|
||||
/* get all buffers */
|
||||
@@ -613,8 +615,9 @@ TEST(RelayApiProtocolWithClient, CbCompletion)
|
||||
STRCMP_EQUAL("HTTP/1.1 400 Bad Request\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 23\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Bad request\"}",
|
||||
data_sent[0]);
|
||||
|
||||
/* error: invalid buffer name */
|
||||
@@ -693,8 +696,9 @@ TEST(RelayApiProtocolWithClient, CbInput)
|
||||
STRCMP_EQUAL("HTTP/1.1 400 Bad Request\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 23\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Bad request\"}",
|
||||
data_sent[0]);
|
||||
|
||||
/* error: invalid buffer name */
|
||||
@@ -817,7 +821,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket)
|
||||
data_sent[0]);
|
||||
|
||||
test_client_recv_text ("{\"request\": \"POST /api/sync\"}");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "null");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "null", "null");
|
||||
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled));
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks));
|
||||
@@ -825,7 +829,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket)
|
||||
|
||||
test_client_recv_text ("{\"request\": \"POST /api/sync\", "
|
||||
"\"body\": {\"sync\": false}}");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":false}");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":false}", "null");
|
||||
|
||||
LONGS_EQUAL(0, RELAY_API_DATA(ptr_relay_client, sync_enabled));
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks));
|
||||
@@ -833,7 +837,7 @@ TEST(RelayApiProtocolWithClient, CbSyncWebsocket)
|
||||
|
||||
test_client_recv_text ("{\"request\": \"POST /api/sync\", "
|
||||
"\"body\": {\"sync\": true, \"nicks\": false}}");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":true,\"nicks\":false}");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":true,\"nicks\":false}", "null");
|
||||
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled));
|
||||
LONGS_EQUAL(0, RELAY_API_DATA(ptr_relay_client, sync_nicks));
|
||||
@@ -841,7 +845,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", "POST /api/sync", "{\"sync\":true,\"nicks\":true,\"colors\":\"weechat\"}");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":true,\"nicks\":true,\"colors\":\"weechat\"}", "null");
|
||||
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled));
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks));
|
||||
@@ -849,7 +853,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", "POST /api/sync", "{\"sync\":true,\"nicks\":true,\"colors\":\"strip\"}");
|
||||
WEE_CHECK_TEXT(204, "No Content", "POST /api/sync", "{\"sync\":true,\"nicks\":true,\"colors\":\"strip\"}", "null");
|
||||
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_enabled));
|
||||
LONGS_EQUAL(1, RELAY_API_DATA(ptr_relay_client, sync_nicks));
|
||||
@@ -882,31 +886,32 @@ TEST(RelayApiProtocolWithClient, RecvJson)
|
||||
|
||||
/* error: empty string */
|
||||
test_client_recv_text ("");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null", "{\"error\":\"Bad request: invalid JSON\"}");
|
||||
|
||||
/* error: empty body */
|
||||
test_client_recv_text ("{}");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null", "{\"error\":\"Bad request\"}");
|
||||
|
||||
/* error: empty request */
|
||||
test_client_recv_text ("{\"request\": \"\"}");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null", "{\"error\":\"Bad request\"}");
|
||||
|
||||
/* error: invalid request (number) */
|
||||
test_client_recv_text ("{\"request\": 123}");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null", "{\"error\":\"Bad request\"}");
|
||||
|
||||
/* error: invalid request (string, not a valid request) */
|
||||
test_client_recv_text ("{\"request\": \"abc\"}");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null");
|
||||
WEE_CHECK_TEXT(400, "Bad Request", "", "null", "{\"error\":\"Bad request\"}");
|
||||
|
||||
/* error: invalid request (string, resource not found) */
|
||||
test_client_recv_text ("{\"request\": \"GET /api/unknown\"}");
|
||||
WEE_CHECK_TEXT(404, "Not Found", "GET /api/unknown", "null");
|
||||
WEE_CHECK_TEXT(404, "Not Found", "GET /api/unknown", "null", "{\"error\":\"Resource not found\"}");
|
||||
|
||||
/* 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}");
|
||||
WEE_CHECK_TEXT(404, "Not Found", "GET /api/unknown", "{\"test\":123}",
|
||||
"{\"error\":\"Resource not found\"}");
|
||||
|
||||
/* ping */
|
||||
test_client_recv_text ("{\"request\": \"POST /api/ping\", \"request_id\": \"ping\"}");
|
||||
@@ -961,8 +966,9 @@ TEST(RelayApiProtocolWithClient, RecvHttp404)
|
||||
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 30\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Resource not found\"}",
|
||||
data_sent[0]);
|
||||
|
||||
/* resource not found: error 404 */
|
||||
@@ -970,8 +976,9 @@ TEST(RelayApiProtocolWithClient, RecvHttp404)
|
||||
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 30\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Resource not found\"}",
|
||||
data_sent[0]);
|
||||
|
||||
/* resource not found: error 404 */
|
||||
@@ -979,8 +986,9 @@ TEST(RelayApiProtocolWithClient, RecvHttp404)
|
||||
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 30\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Resource not found\"}",
|
||||
data_sent[0]);
|
||||
|
||||
/* resource not found: error 404 */
|
||||
@@ -988,8 +996,9 @@ TEST(RelayApiProtocolWithClient, RecvHttp404)
|
||||
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 30\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Resource not found\"}",
|
||||
data_sent[0]);
|
||||
|
||||
/* resource not found: error 404 */
|
||||
@@ -997,8 +1006,9 @@ TEST(RelayApiProtocolWithClient, RecvHttp404)
|
||||
STRCMP_EQUAL("HTTP/1.1 404 Not Found\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n",
|
||||
"Content-Length: 30\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":\"Resource not found\"}",
|
||||
data_sent[0]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user