MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / basicGet

Method basicGet

Tests/Libraries/Http/HttpAsyncClientTest.cpp:430–486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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
483void SC::HttpAsyncClientTest::httpsRequiresTransportAdapter()
484{
485 AsyncEventLoop loop;
486 SC_TEST_EXPECT(loop.create());
487

Callers

nothing calls this directly

Calls 15

mapPortMethod · 0.80
startResponseMethod · 0.80
sendHeadersMethod · 0.80
formatFunction · 0.50
createMethod · 0.45
initMethod · 0.45
startMethod · 0.45
addHeaderMethod · 0.45
writeMethod · 0.45
endMethod · 0.45
attachMethod · 0.45
detachMethod · 0.45

Tested by

no test coverage detected