| 428 | SC_TEST_EXPECT(loop.create()); |
| 429 | |
| 430 | ServerConnection connections[2]; |
| 431 | HttpAsyncServer httpServer; |
| 432 | const uint16_t port = report.mapPort(26100); |
| 433 | SC_TEST_EXPECT(httpServer.init(Span<ServerConnection>(connections))); |
| 434 | SC_TEST_EXPECT(httpServer.start(loop, "127.0.0.1", port)); |
| 435 | |
| 436 | httpServer.onRequest = [this](HttpConnection& connection) |
| 437 | { |
| 438 | SC_TEST_EXPECT(connection.response.startResponse(200)); |
| 439 | SC_TEST_EXPECT(connection.response.addHeader("Content-Length", "5")); |
| 440 | SC_TEST_EXPECT(connection.response.sendHeaders()); |
| 441 | SC_TEST_EXPECT(connection.response.getWritableStream().write("hello")); |
| 442 | SC_TEST_EXPECT(connection.response.end()); |
| 443 | }; |
| 444 | |
| 445 | ClientConnection clientStorage; |
| 446 | HttpAsyncClient client; |
| 447 | ResponseCollector collector; |
| 448 | |
| 449 | TimeoutGuard timeout; |
| 450 | String url = StringEncoding::Ascii; |
| 451 | struct Context |
| 452 | { |
| 453 | ResponseCollector& collector; |
| 454 | HttpAsyncServer& httpServer; |
| 455 | } ctx = {collector, httpServer}; |
| 456 | |
| 457 | SC_TEST_EXPECT(client.init(clientStorage)); |
| 458 | SC_TEST_EXPECT(StringBuilder::format(url, "http://127.0.0.1:{}/hello", port)); |
| 459 | |
| 460 | //! [HttpAsyncClientBasicSnippet] |
| 461 | client.onResponse = [this, &ctx](HttpAsyncClientResponse& response) |
| 462 | { |
| 463 | ctx.collector.attach(response, |
| 464 | [this, &ctx](HttpAsyncClientResponse& completedResponse) |
| 465 | { |
| 466 | ctx.collector.detach(); |
| 467 | SC_TEST_EXPECT(completedResponse.getParser().statusCode == 200); |
| 468 | SC_TEST_EXPECT(StringView(ctx.collector.view()) == "hello"); |
| 469 | SC_TEST_EXPECT(ctx.httpServer.stop()); |
| 470 | }); |
| 471 | }; |
| 472 | client.onError = [this](Result result) { SC_TEST_EXPECT(result); }; |
| 473 | |
| 474 | SC_TEST_EXPECT(client.get(loop, url.view())); |
| 475 | //! [HttpAsyncClientBasicSnippet] |
| 476 | |
| 477 | SC_TEST_EXPECT(timeout.start(loop, TimeMs{2000})); |
| 478 | SC_TEST_EXPECT(loop.run()); |
| 479 | SC_TEST_EXPECT(httpServer.close()); |
| 480 | SC_TEST_EXPECT(loop.close()); |
| 481 | } |
| 482 | |
| 483 | void SC::HttpAsyncClientTest::httpsRequiresTransportAdapter() |
| 484 | { |
| 485 | AsyncEventLoop loop; |
| 486 | SC_TEST_EXPECT(loop.create()); |
| 487 |
nothing calls this directly
no test coverage detected