Skip to content

Commit 577746a

Browse files
committed
fix: make the tests work for both FoxyClient and Curl
1 parent ea05ff4 commit 577746a

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

libs/server-sent-events/src/client.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ class FoxyClient : public Client,
283283
return;
284284
}
285285

286+
host_ = new_url->host();
287+
port_ =
288+
new_url->has_port() ? new_url->port() : new_url->scheme();
286289
req_.set(http::field::host, new_url->host());
287290
req_.target(new_url->encoded_target());
288291
} else {
@@ -665,7 +668,9 @@ std::shared_ptr<Client> Builder::build() {
665668
std::string host = uri_components->host();
666669

667670
request.set(http::field::host, host);
668-
request.target(uri_components->encoded_target());
671+
// RFC 7230: an empty path in origin-form must be sent as "/".
672+
auto target = uri_components->encoded_target();
673+
request.target(target.empty() ? "/" : target);
669674

670675
if (uri_components->has_scheme()) {
671676
if (!(uri_components->scheme_id() == boost::urls::scheme::http ||

libs/server-sent-events/tests/curl_client_test.cpp renamed to libs/server-sent-events/tests/client_test.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#ifdef LD_CURL_NETWORKING
21

32
#include <gmock/gmock.h>
43
#include <gtest/gtest.h>
@@ -121,7 +120,7 @@ class IoContextRunner {
121120

122121
// Basic connectivity tests
123122

124-
TEST(CurlClientTest, ConnectsToHttpServer) {
123+
TEST(ClientTest, ConnectsToHttpServer) {
125124
MockSSEServer server;
126125
auto port = server.start(TestHandlers::simple_event("hello world"));
127126

@@ -149,7 +148,7 @@ TEST(CurlClientTest, ConnectsToHttpServer) {
149148
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
150149
}
151150

152-
TEST(CurlClientTest, HandlesMultipleEvents) {
151+
TEST(ClientTest, HandlesMultipleEvents) {
153152
MockSSEServer server;
154153
auto port = server.start(
155154
TestHandlers::multiple_events({"event1", "event2", "event3"}));
@@ -179,7 +178,7 @@ TEST(CurlClientTest, HandlesMultipleEvents) {
179178

180179
// SSE parsing tests
181180

182-
TEST(CurlClientTest, ParsesEventWithType) {
181+
TEST(ClientTest, ParsesEventWithType) {
183182
MockSSEServer server;
184183
auto port = server.start(
185184
[](auto const&, auto send_response, auto send_sse_event, auto close) {
@@ -215,7 +214,7 @@ TEST(CurlClientTest, ParsesEventWithType) {
215214
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
216215
}
217216

218-
TEST(CurlClientTest, ParsesEventWithId) {
217+
TEST(ClientTest, ParsesEventWithId) {
219218
MockSSEServer server;
220219
auto port = server.start(
221220
[](auto const&, auto send_response, auto send_sse_event, auto close) {
@@ -251,7 +250,7 @@ TEST(CurlClientTest, ParsesEventWithId) {
251250
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
252251
}
253252

254-
TEST(CurlClientTest, ParsesMultiLineData) {
253+
TEST(ClientTest, ParsesMultiLineData) {
255254
MockSSEServer server;
256255
auto port = server.start(
257256
[](auto const&, auto send_response, auto send_sse_event, auto close) {
@@ -286,7 +285,7 @@ TEST(CurlClientTest, ParsesMultiLineData) {
286285
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
287286
}
288287

289-
TEST(CurlClientTest, HandlesComments) {
288+
TEST(ClientTest, HandlesComments) {
290289
GTEST_SKIP()
291290
<< "Comment filtering is not yet implemented in the SSE parser";
292291

@@ -330,7 +329,7 @@ TEST(CurlClientTest, HandlesComments) {
330329

331330
// HTTP method tests
332331

333-
TEST(CurlClientTest, SupportsPostMethod) {
332+
TEST(ClientTest, SupportsPostMethod) {
334333
MockSSEServer server;
335334
std::string received_method;
336335

@@ -369,7 +368,7 @@ TEST(CurlClientTest, SupportsPostMethod) {
369368
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
370369
}
371370

372-
TEST(CurlClientTest, SupportsReportMethod) {
371+
TEST(ClientTest, SupportsReportMethod) {
373372
MockSSEServer server;
374373
std::string received_method;
375374

@@ -410,7 +409,7 @@ TEST(CurlClientTest, SupportsReportMethod) {
410409

411410
// HTTP header tests
412411

413-
TEST(CurlClientTest, SendsCustomHeaders) {
412+
TEST(ClientTest, SendsCustomHeaders) {
414413
MockSSEServer server;
415414
std::string custom_header_value;
416415

@@ -453,7 +452,7 @@ TEST(CurlClientTest, SendsCustomHeaders) {
453452

454453
// HTTP status code tests
455454

456-
TEST(CurlClientTest, Handles404Error) {
455+
TEST(ClientTest, Handles404Error) {
457456
MockSSEServer server;
458457
auto port = server.start(TestHandlers::http_error(http::status::not_found));
459458

@@ -478,7 +477,7 @@ TEST(CurlClientTest, Handles404Error) {
478477
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
479478
}
480479

481-
TEST(CurlClientTest, Handles500Error) {
480+
TEST(ClientTest, Handles500Error) {
482481
// 500 errors are treated as transient server errors and should trigger
483482
// backoff/retry behavior, not error callbacks. This is correct SSE client
484483
// behavior.
@@ -527,7 +526,7 @@ TEST(CurlClientTest, Handles500Error) {
527526

528527
// Redirect tests
529528

530-
TEST(CurlClientTest, FollowsRedirects) {
529+
TEST(ClientTest, FollowsRedirects) {
531530
MockSSEServer redirect_server;
532531
MockSSEServer target_server;
533532

@@ -559,7 +558,7 @@ TEST(CurlClientTest, FollowsRedirects) {
559558

560559
// Connection lifecycle tests
561560

562-
TEST(CurlClientTest, ShutdownStopsClient) {
561+
TEST(ClientTest, ShutdownStopsClient) {
563562
MockSSEServer server;
564563
auto port = server.start([](auto const&, auto send_response,
565564
auto send_sse_event, auto) {
@@ -600,7 +599,7 @@ TEST(CurlClientTest, ShutdownStopsClient) {
600599
EXPECT_LT(shutdown_duration, 2000ms);
601600
}
602601

603-
TEST(CurlClientTest, CanShutdownBeforeConnection) {
602+
TEST(ClientTest, CanShutdownBeforeConnection) {
604603
MockSSEServer server;
605604
auto port = server.start(TestHandlers::simple_event("test"));
606605

@@ -619,7 +618,7 @@ TEST(CurlClientTest, CanShutdownBeforeConnection) {
619618
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
620619
}
621620

622-
TEST(CurlClientTest, HandlesImmediateClose) {
621+
TEST(ClientTest, HandlesImmediateClose) {
623622
// Immediate connection close is treated as a transient network error and
624623
// should trigger backoff/retry behavior, not error callbacks. This is
625624
// correct SSE client behavior.
@@ -664,7 +663,7 @@ TEST(CurlClientTest, HandlesImmediateClose) {
664663

665664
// Timeout tests
666665

667-
TEST(CurlClientTest, RespectsReadTimeout) {
666+
TEST(ClientTest, RespectsReadTimeout) {
668667
MockSSEServer server;
669668
auto port = server.start(
670669
[](auto const&, auto send_response, auto send_sse_event, auto) {
@@ -711,7 +710,7 @@ TEST(CurlClientTest, RespectsReadTimeout) {
711710
EXPECT_TRUE(shutdown_latch.wait_for(100ms));
712711
}
713712

714-
TEST(CurlClientTest, DestructorCleansUpProperly) {
713+
TEST(ClientTest, DestructorCleansUpProperly) {
715714
{
716715
MockSSEServer server;
717716
auto port = server.start(
@@ -746,7 +745,7 @@ TEST(CurlClientTest, DestructorCleansUpProperly) {
746745
// Test passing indicates proper cleanup in destructor
747746
}
748747

749-
TEST(CurlClientTest, HandlesEmptyEventData) {
748+
TEST(ClientTest, HandlesEmptyEventData) {
750749
MockSSEServer server;
751750
auto port = server.start(
752751
[](auto const&, auto send_response, auto send_sse_event, auto close) {
@@ -781,7 +780,7 @@ TEST(CurlClientTest, HandlesEmptyEventData) {
781780
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
782781
}
783782

784-
TEST(CurlClientTest, HandlesEventWithOnlyType) {
783+
TEST(ClientTest, HandlesEventWithOnlyType) {
785784
MockSSEServer server;
786785
auto port = server.start(
787786
[](auto const&, auto send_response, auto send_sse_event, auto close) {
@@ -818,7 +817,7 @@ TEST(CurlClientTest, HandlesEventWithOnlyType) {
818817
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
819818
}
820819

821-
TEST(CurlClientTest, HandlesRapidEvents) {
820+
TEST(ClientTest, HandlesRapidEvents) {
822821
MockSSEServer server;
823822
constexpr int num_events = 100;
824823

@@ -858,7 +857,7 @@ TEST(CurlClientTest, HandlesRapidEvents) {
858857
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
859858
}
860859

861-
TEST(CurlClientTest, ShutdownDuringBackoffDelay) {
860+
TEST(ClientTest, ShutdownDuringBackoffDelay) {
862861
// This ensures clean shutdown during backoff/retry wait period
863862
std::atomic<int> connection_attempts{0};
864863

@@ -906,7 +905,7 @@ TEST(CurlClientTest, ShutdownDuringBackoffDelay) {
906905
EXPECT_EQ(1, connection_attempts.load());
907906
}
908907

909-
TEST(CurlClientTest, ShutdownDuringDataReception) {
908+
TEST(ClientTest, ShutdownDuringDataReception) {
910909
// This covers the branch where we abort during SSE data parsing
911910
SimpleLatch server_sending(1);
912911
SimpleLatch client_received_some(1);
@@ -966,7 +965,7 @@ TEST(CurlClientTest, ShutdownDuringDataReception) {
966965
EXPECT_LT(shutdown_duration, 2000ms);
967966
}
968967

969-
TEST(CurlClientTest, ShutdownDuringProgressCallback) {
968+
TEST(ClientTest, ShutdownDuringProgressCallback) {
970969
// This ensures we can abort during slow data transfer
971970
SimpleLatch server_started(1);
972971

@@ -1016,7 +1015,7 @@ TEST(CurlClientTest, ShutdownDuringProgressCallback) {
10161015
EXPECT_LT(shutdown_duration, 2000ms);
10171016
}
10181017

1019-
TEST(CurlClientTest, MultipleShutdownCalls) {
1018+
TEST(ClientTest, MultipleShutdownCalls) {
10201019
// Ensures multiple shutdown calls don't cause issues (idempotency test)
10211020
MockSSEServer server;
10221021
auto port = server.start(TestHandlers::simple_event("test"));
@@ -1048,7 +1047,7 @@ TEST(CurlClientTest, MultipleShutdownCalls) {
10481047
EXPECT_TRUE(shutdown_latch3.wait_for(5000ms));
10491048
}
10501049

1051-
TEST(CurlClientTest, ShutdownAfterConnectionClosed) {
1050+
TEST(ClientTest, ShutdownAfterConnectionClosed) {
10521051
// Tests shutdown when connection has already ended naturally
10531052
MockSSEServer server;
10541053
auto port = server.start(
@@ -1086,7 +1085,7 @@ TEST(CurlClientTest, ShutdownAfterConnectionClosed) {
10861085
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
10871086
}
10881087

1089-
TEST(CurlClientTest, ShutdownDuringConnectionAttempt) {
1088+
TEST(ClientTest, ShutdownDuringConnectionAttempt) {
10901089
// Server that delays before responding to test shutdown during connection
10911090
// phase
10921091
SimpleLatch connection_started(1);
@@ -1141,7 +1140,7 @@ TEST(CurlClientTest, ShutdownDuringConnectionAttempt) {
11411140

11421141
// on_connect hook tests
11431142

1144-
TEST(CurlClientTest, OnConnectHookInvokedBeforeRequest) {
1143+
TEST(ClientTest, OnConnectHookInvokedBeforeRequest) {
11451144
MockSSEServer server;
11461145
auto port = server.start(TestHandlers::simple_event("hello"));
11471146

@@ -1179,7 +1178,7 @@ TEST(CurlClientTest, OnConnectHookInvokedBeforeRequest) {
11791178
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
11801179
}
11811180

1182-
TEST(CurlClientTest, OnConnectHookCanMutateTarget) {
1181+
TEST(ClientTest, OnConnectHookCanMutateTarget) {
11831182
MockSSEServer server;
11841183
std::string seen_target;
11851184
std::mutex target_mutex;
@@ -1227,7 +1226,7 @@ TEST(CurlClientTest, OnConnectHookCanMutateTarget) {
12271226
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
12281227
}
12291228

1230-
TEST(CurlClientTest, OnConnectHookCanMutateHeaders) {
1229+
TEST(ClientTest, OnConnectHookCanMutateHeaders) {
12311230
MockSSEServer server;
12321231
std::string seen_header;
12331232
std::mutex header_mutex;
@@ -1274,7 +1273,7 @@ TEST(CurlClientTest, OnConnectHookCanMutateHeaders) {
12741273
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
12751274
}
12761275

1277-
TEST(CurlClientTest, OnConnectHookInvokedOnEachReconnect) {
1276+
TEST(ClientTest, OnConnectHookInvokedOnEachReconnect) {
12781277
MockSSEServer server;
12791278
auto port = server.start(
12801279
[](auto const&, auto send_response, auto send_sse_event, auto close) {
@@ -1313,7 +1312,7 @@ TEST(CurlClientTest, OnConnectHookInvokedOnEachReconnect) {
13131312
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
13141313
}
13151314

1316-
TEST(CurlClientTest, OnConnectHookSeesPreviousMutations) {
1315+
TEST(ClientTest, OnConnectHookSeesPreviousMutations) {
13171316
MockSSEServer server;
13181317
std::vector<std::string> seen_targets;
13191318
std::mutex targets_mutex;
@@ -1371,4 +1370,3 @@ TEST(CurlClientTest, OnConnectHookSeesPreviousMutations) {
13711370
client->async_shutdown([&] { shutdown_latch.count_down(); });
13721371
EXPECT_TRUE(shutdown_latch.wait_for(5000ms));
13731372
}
1374-
#endif // LD_CURL_NETWORKING

0 commit comments

Comments
 (0)