diff options
Diffstat (limited to 'src/zenhttp/httpclient_test.cpp')
| -rw-r--r-- | src/zenhttp/httpclient_test.cpp | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/zenhttp/httpclient_test.cpp b/src/zenhttp/httpclient_test.cpp index af653cbb2..deaeca2a8 100644 --- a/src/zenhttp/httpclient_test.cpp +++ b/src/zenhttp/httpclient_test.cpp @@ -194,7 +194,7 @@ public: "slow", [](HttpRouterRequest& Req) { Req.ServerRequest().WriteResponseAsync([](HttpServerRequest& Request) { - Sleep(2000); + Sleep(100); Request.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, "slow response"); }); }, @@ -414,6 +414,17 @@ TEST_CASE("httpclient.post") CHECK_EQ(Resp.AsText(), "{\"key\":\"value\"}"); } + SUBCASE("POST with content type override via additional header") + { + const char* Payload = "test payload"; + IoBuffer Buf(IoBuffer::Clone, Payload, strlen(Payload)); + + HttpClient::Response Resp = Client.Post("/api/test/echo", Buf, ZenContentType::kJSON, {{"Content-Type", "text/plain"}}); + CHECK(Resp.IsSuccess()); + CHECK_EQ(Resp.AsText(), "test payload"); + CHECK_EQ(Resp.ResponsePayload.GetContentType(), ZenContentType::kText); + } + SUBCASE("POST with CbObject payload round-trip") { CbObjectWriter Writer; @@ -750,7 +761,9 @@ TEST_CASE("httpclient.error-handling") { SUBCASE("Connection refused") { - HttpClient Client("127.0.0.1:19999", HttpClientSettings{}, /*CheckIfAbortFunction*/ {}); + HttpClientSettings Settings; + Settings.ConnectTimeout = std::chrono::milliseconds(200); + HttpClient Client("127.0.0.1:19999", Settings, /*CheckIfAbortFunction*/ {}); HttpClient::Response Resp = Client.Get("/api/test/hello"); CHECK(!Resp.IsSuccess()); CHECK(Resp.Error.has_value()); @@ -760,7 +773,7 @@ TEST_CASE("httpclient.error-handling") { TestServerFixture Fixture; HttpClientSettings Settings; - Settings.Timeout = std::chrono::milliseconds(500); + Settings.Timeout = std::chrono::milliseconds(50); HttpClient Client = Fixture.MakeClient(Settings); HttpClient::Response Resp = Client.Get("/api/test/slow"); @@ -970,7 +983,9 @@ TEST_CASE("httpclient.measurelatency") SUBCASE("Failed measurement against unreachable port") { - HttpClient Client("127.0.0.1:19999", HttpClientSettings{}, /*CheckIfAbortFunction*/ {}); + HttpClientSettings Settings; + Settings.ConnectTimeout = std::chrono::milliseconds(200); + HttpClient Client("127.0.0.1:19999", Settings, /*CheckIfAbortFunction*/ {}); LatencyTestResult Result = MeasureLatency(Client, "/api/test/hello"); CHECK(!Result.Success); CHECK(!Result.FailureReason.empty()); @@ -1144,7 +1159,7 @@ struct FaultTcpServer ~FaultTcpServer() { // io_context::stop() is thread-safe; do NOT call m_Acceptor.close() from this - // thread — ASIO I/O objects are not safe for concurrent access and the io_context + // thread - ASIO I/O objects are not safe for concurrent access and the io_context // thread may be touching the acceptor in StartAccept(). m_IoContext.stop(); if (m_Thread.joinable()) @@ -1498,7 +1513,7 @@ TEST_CASE("httpclient.transport-faults-post" * doctest::skip()) std::atomic<bool> StallActive{true}; FaultTcpServer Server([&StallActive](asio::ip::tcp::socket& Socket) { DrainHttpRequest(Socket); - // Stop reading body — TCP window will fill and client send will stall + // Stop reading body - TCP window will fill and client send will stall while (StallActive.load()) { std::this_thread::sleep_for(std::chrono::milliseconds(50)); @@ -1735,21 +1750,21 @@ TEST_CASE("httpclient.uri_decoding") TestServerFixture Fixture; HttpClient Client = Fixture.MakeClient(); - // URI without encoding — should pass through unchanged + // URI without encoding - should pass through unchanged { HttpClient::Response Resp = Client.Get("/api/test/echo/uri/hello/world.txt"); REQUIRE(Resp.IsSuccess()); CHECK(Resp.AsText() == "uri=echo/uri/hello/world.txt\ncapture=hello/world.txt"); } - // Percent-encoded space — server should see decoded path + // Percent-encoded space - server should see decoded path { HttpClient::Response Resp = Client.Get("/api/test/echo/uri/hello%20world.txt"); REQUIRE(Resp.IsSuccess()); CHECK(Resp.AsText() == "uri=echo/uri/hello world.txt\ncapture=hello world.txt"); } - // Percent-encoded slash (%2F) — should be decoded to / + // Percent-encoded slash (%2F) - should be decoded to / { HttpClient::Response Resp = Client.Get("/api/test/echo/uri/a%2Fb.txt"); REQUIRE(Resp.IsSuccess()); @@ -1763,21 +1778,21 @@ TEST_CASE("httpclient.uri_decoding") CHECK(Resp.AsText() == "uri=echo/uri/file & name.txt\ncapture=file & name.txt"); } - // No capture — echo/uri route returns just RelativeUri + // No capture - echo/uri route returns just RelativeUri { HttpClient::Response Resp = Client.Get("/api/test/echo/uri"); REQUIRE(Resp.IsSuccess()); CHECK(Resp.AsText() == "echo/uri"); } - // Literal percent that is not an escape (%ZZ) — should be kept as-is + // Literal percent that is not an escape (%ZZ) - should be kept as-is { HttpClient::Response Resp = Client.Get("/api/test/echo/uri/100%25done.txt"); REQUIRE(Resp.IsSuccess()); CHECK(Resp.AsText() == "uri=echo/uri/100%done.txt\ncapture=100%done.txt"); } - // Query params — raw values are returned as-is from GetQueryParams + // Query params - raw values are returned as-is from GetQueryParams { HttpClient::Response Resp = Client.Get("/api/test/echo/uri?key=value&name=test"); REQUIRE(Resp.IsSuccess()); @@ -1788,7 +1803,7 @@ TEST_CASE("httpclient.uri_decoding") { HttpClient::Response Resp = Client.Get("/api/test/echo/uri?prefix=listing%2F&mode=s3"); REQUIRE(Resp.IsSuccess()); - // GetQueryParams returns raw (still-encoded) values — callers must Decode() explicitly + // GetQueryParams returns raw (still-encoded) values - callers must Decode() explicitly CHECK(Resp.AsText() == "echo/uri\nprefix=listing%2F\nmode=s3"); } |