Skip to content

Commit d32b213

Browse files
rluboskartben
authored andcommitted
tests: net: http_server: core: Add tests for Method Not Allowed reply
Add tests cases verifying that the server replies with HTTP 405 Method Not Allowed reply in case the client tries to access the resource with a method it does not allow. Signed-off-by: Robert Lubos <[email protected]>
1 parent e9bedcc commit d32b213

File tree

1 file changed

+90
-0
lines changed
  • tests/net/lib/http_server/core/src

1 file changed

+90
-0
lines changed

tests/net/lib/http_server/core/src/main.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ BUILD_ASSERT(sizeof(long_payload) - 1 > CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE,
226226
0x93, 0x13, 0x7a, 0x88, 0x25, 0xb6, 0x50, 0xc3, 0xcb, 0xbc, 0xb8, 0x3f, \
227227
0x53, 0x03, 0x2a, 0x2f, 0x2a, 0x5f, 0x87, 0x49, 0x7c, 0xa5, 0x8a, 0xe8, \
228228
0x19, 0xaa
229+
#define TEST_HTTP2_HEADERS_POST_ROOT_STREAM_1 \
230+
0x00, 0x00, 0x21, 0x01, 0x05, 0x00, 0x00, 0x00, TEST_STREAM_ID_1, \
231+
0x83, 0x84, 0x86, 0x41, 0x8a, 0x0b, 0xe2, 0x5c, 0x0b, 0x89, 0x70, 0xdc, \
232+
0x78, 0x0f, 0x03, 0x53, 0x03, 0x2a, 0x2f, 0x2a, 0x90, 0x7a, 0x8a, 0xaa, \
233+
0x69, 0xd2, 0x9a, 0xc4, 0xc0, 0x57, 0x68, 0x0b, 0x83
234+
#define TEST_HTTP2_DATA_POST_ROOT_STREAM_1 TEST_HTTP2_DATA_POST_DYNAMIC_STREAM_1
229235

230236
static uint16_t test_http_service_port = SERVER_PORT;
231237
HTTP_SERVICE_DEFINE(test_http_service, SERVER_IPV4_ADDR,
@@ -2154,6 +2160,90 @@ ZTEST(server_function_tests, test_http2_dynamic_post_response_header_long)
21542160
zassert_mem_equal(dynamic_response_headers_buffer, long_payload, strlen(long_payload));
21552161
}
21562162

2163+
ZTEST(server_function_tests, test_http1_409_method_not_allowed)
2164+
{
2165+
static const char http1_request[] =
2166+
"POST / HTTP/1.1\r\n"
2167+
"Host: 127.0.0.1:8080\r\n"
2168+
"Content-Type: text/html\r\n"
2169+
"Content-Length: 13\r\n\r\n"
2170+
TEST_STATIC_PAYLOAD;
2171+
static const char expected_response[] =
2172+
"HTTP/1.1 405 Method Not Allowed\r\n";
2173+
size_t offset = 0;
2174+
int ret;
2175+
2176+
ret = zsock_send(client_fd, http1_request, strlen(http1_request), 0);
2177+
zassert_not_equal(ret, -1, "send() failed (%d)", errno);
2178+
2179+
memset(buf, 0, sizeof(buf));
2180+
2181+
test_read_data(&offset, sizeof(expected_response) - 1);
2182+
zassert_mem_equal(buf, expected_response, sizeof(expected_response) - 1,
2183+
"Received data doesn't match expected response");
2184+
}
2185+
2186+
ZTEST(server_function_tests, test_http1_upgrade_409_method_not_allowed)
2187+
{
2188+
static const char http1_request[] =
2189+
"POST / HTTP/1.1\r\n"
2190+
"Host: 127.0.0.1:8080\r\n"
2191+
"Content-Type: text/html\r\n"
2192+
"Content-Length: 13\r\n"
2193+
"Connection: Upgrade, HTTP2-Settings\r\n"
2194+
"Upgrade: h2c\r\n"
2195+
"HTTP2-Settings: AAMAAABkAAQAoAAAAAIAAAAA\r\n\r\n"
2196+
TEST_STATIC_PAYLOAD;
2197+
const struct http_header expected_headers[] = {
2198+
{.name = ":status", .value = "405"}
2199+
};
2200+
size_t offset = 0;
2201+
int ret;
2202+
2203+
ret = zsock_send(client_fd, http1_request, strlen(http1_request), 0);
2204+
zassert_not_equal(ret, -1, "send() failed (%d)", errno);
2205+
2206+
memset(buf, 0, sizeof(buf));
2207+
2208+
/* Verify HTTP1 switching protocols response. */
2209+
expect_http1_switching_protocols(&offset);
2210+
2211+
/* Verify HTTP2 frames. */
2212+
expect_http2_settings_frame(&offset, false);
2213+
expect_http2_headers_frame(&offset, UPGRADE_STREAM_ID,
2214+
HTTP2_FLAG_END_HEADERS | HTTP2_FLAG_END_STREAM,
2215+
expected_headers, 1);
2216+
}
2217+
2218+
ZTEST(server_function_tests, test_http2_409_method_not_allowed)
2219+
{
2220+
static const uint8_t request_post_static[] = {
2221+
TEST_HTTP2_MAGIC,
2222+
TEST_HTTP2_SETTINGS,
2223+
TEST_HTTP2_SETTINGS_ACK,
2224+
TEST_HTTP2_HEADERS_POST_ROOT_STREAM_1,
2225+
TEST_HTTP2_DATA_POST_ROOT_STREAM_1,
2226+
TEST_HTTP2_GOAWAY,
2227+
};
2228+
const struct http_header expected_headers[] = {
2229+
{.name = ":status", .value = "405"}
2230+
};
2231+
size_t offset = 0;
2232+
int ret;
2233+
2234+
ret = zsock_send(client_fd, request_post_static,
2235+
sizeof(request_post_static), 0);
2236+
zassert_not_equal(ret, -1, "send() failed (%d)", errno);
2237+
2238+
memset(buf, 0, sizeof(buf));
2239+
2240+
expect_http2_settings_frame(&offset, false);
2241+
expect_http2_settings_frame(&offset, true);
2242+
expect_http2_headers_frame(&offset, TEST_STREAM_ID_1,
2243+
HTTP2_FLAG_END_HEADERS | HTTP2_FLAG_END_STREAM,
2244+
expected_headers, 1);
2245+
}
2246+
21572247
ZTEST(server_function_tests_no_init, test_http_server_start_stop)
21582248
{
21592249
struct sockaddr_in sa = { 0 };

0 commit comments

Comments
 (0)