/* * Copyright (c) 2024, Dan Klishch * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include struct HTTPUnitTest { StringView name; HTTP::Method method; StringView url; Vector headers; StringView response; StringView request_expectation; StringView body_expectation; }; Vector const http_unit_tests = { { .name = "Basic"sv, .method = HTTP::Method::GET, .url = "/"sv, .headers = { { "Host", "localhost" }, }, .response = "HTTP/1.1 200 OK\r\n" "Content-Length: 16\r\n" "\r\n" "0123456789abcdef"sv, .request_expectation = "GET / HTTP/1.1\r\n" "Host: localhost\r\n" "\r\n"sv, .body_expectation = "0123456789abcdef"sv, }, { .name = "Chunked"sv, .method = HTTP::Method::GET, .url = "/"sv, .headers = { { "Host", "localhost" }, }, .response = "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "\r\n" "18\r\n" "0123456789abcdef\r\n\r\n" "19\r\n" "Well hello friends!\r\n" "0\r\n" "\r\n"sv, .request_expectation = "GET / HTTP/1.1\r\n" "Host: localhost\r\n" "\r\n"sv, .body_expectation = "0123456789abcdef\r\nWell hello friends!"sv, }, }; ASYNC_TEST_CASE(unit_tests_single) { for (auto const& test : http_unit_tests) { outln("Running '{}'...", test.name); auto response_partitioning = Test::randomly_partition_input(1, AK::get_random_uniform(50) + 1, test.response.length()); outln("Input partitioning: {}", response_partitioning); auto input = make(test.response, Test::StreamCloseExpectation::Close, move(response_partitioning)); auto output = make(Test::StreamCloseExpectation::Close); auto output_ref = output.ptr(); auto stream_pair = make(move(input), move(output)); auto connection = make(move(stream_pair)); CO_TRY_OR_FAIL(co_await connection->request( { .method = test.method, .url = test.url, .headers = test.headers, }, [&](HTTP::Http11Response& response) -> Coroutine> { auto body = CO_TRY(co_await Test::read_until_eof(response.body())); EXPECT_EQ(StringView { body }, test.body_expectation); co_return {}; })); CO_TRY_OR_FAIL(co_await connection->close()); EXPECT_EQ(StringView { output_ref->view() }, test.request_expectation); } }