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

Method putWritableBody

Tests/Libraries/Http/HttpAsyncClientTest.cpp:1152–1254  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1150 SC_TEST_EXPECT(httpServer.close());
1151 SC_TEST_EXPECT(loop.close());
1152}
1153
1154void SC::HttpAsyncClientTest::putWritableBody()
1155{
1156 AsyncEventLoop loop;
1157 SC_TEST_EXPECT(loop.create());
1158
1159 ServerConnection connections[1];
1160 HttpAsyncServer httpServer;
1161 const uint16_t port = report.mapPort(26108);
1162 SC_TEST_EXPECT(httpServer.init(Span<ServerConnection>(connections)));
1163 SC_TEST_EXPECT(httpServer.start(loop, "127.0.0.1", port));
1164
1165 struct ServerContext
1166 {
1167 HttpConnection* connection = nullptr;
1168
1169 Buffer body;
1170 bool responseSent = false;
1171
1172 Result append(Span<const char> data)
1173 {
1174 GrowableBuffer<Buffer> gb(body);
1175 HttpStringAppend& sb = static_cast<HttpStringAppend&>(static_cast<IGrowableBuffer&>(gb));
1176 return Result(sb.append(data, 0));
1177 }
1178
1179 void onData(AsyncBufferView::ID bufferID)
1180 {
1181 SC_ASSERT_RELEASE(connection != nullptr);
1182 AsyncReadableStream& readable = connection->request.getReadableStream();
1183 Span<const char> data;
1184 SC_ASSERT_RELEASE(readable.getBuffersPool().getReadableData(bufferID, data));
1185 SC_ASSERT_RELEASE(data.sizeInBytes() <= connection->request.getBodyBytesRemaining());
1186 SC_ASSERT_RELEASE(append(data));
1187 SC_ASSERT_RELEASE(connection->request.consumeBodyBytes(data.sizeInBytes()));
1188 if (connection->request.getBodyBytesRemaining() == 0 and not responseSent)
1189 {
1190 responseSent = true;
1191 const bool removed = readable.eventData.removeListener<ServerContext, &ServerContext::onData>(*this);
1192 SC_ASSERT_RELEASE(removed);
1193 SC_ASSERT_RELEASE(connection->response.startResponse(200));
1194 SC_ASSERT_RELEASE(connection->response.addHeader("Content-Length", "6"));
1195 SC_ASSERT_RELEASE(connection->response.sendHeaders());
1196 SC_ASSERT_RELEASE(connection->response.getWritableStream().write("stored"));
1197 SC_ASSERT_RELEASE(connection->response.end());
1198 }
1199 }
1200
1201 [[nodiscard]] StringSpan view() const { return {body.toSpanConst(), false, StringEncoding::Ascii}; }
1202 } serverCtx;
1203
1204 httpServer.onRequest = [this, &serverCtx](HttpConnection& connection)
1205 {
1206 serverCtx.connection = &connection;
1207 SC_TEST_EXPECT(connection.request.getBodyBytesRemaining() == 11);
1208 const bool added =
1209 connection.request.getReadableStream().eventData.addListener<ServerContext, &ServerContext::onData>(

Callers

nothing calls this directly

Calls 15

mapPortMethod · 0.80
setExpectedBodyLengthMethod · 0.80
sendHeadersMethod · 0.80
formatFunction · 0.50
createMethod · 0.45
initMethod · 0.45
startMethod · 0.45
writeMethod · 0.45
endMethod · 0.45
attachMethod · 0.45
detachMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected