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

Method uploadPolicy

Tests/Libraries/Http/HttpAsyncFileServerTest.cpp:176–238  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

174}
175
176void SC::HttpAsyncFileServerTest::uploadPolicy()
177{
178 StringView webServerFolder = report.applicationRootDirectory.view();
179 AsyncEventLoop eventLoop;
180 SC_TEST_EXPECT(eventLoop.create());
181
182 using HttpConnectionType = HttpAsyncConnection<2, 2, 8 * 1024, 8 * 1024>;
183
184 HttpConnectionType connections[1];
185 HttpAsyncFileServer::StreamQueue<2> streams[1];
186 HttpAsyncServer httpServer;
187 HttpAsyncFileServer fileServer;
188 ThreadPool threadPool;
189 const uint16_t serverPort = report.mapPort(26119);
190
191 if (eventLoop.needsThreadPoolForFileOperations())
192 {
193 SC_TEST_EXPECT(threadPool.create(2));
194 }
195 SC_TEST_EXPECT(httpServer.init(Span<HttpConnectionType>(connections)));
196 SC_TEST_EXPECT(httpServer.start(eventLoop, "127.0.0.1", serverPort));
197 SC_TEST_EXPECT(fileServer.init(threadPool, eventLoop, webServerFolder));
198
199 HttpAsyncFileServerOptions options;
200 options.maxUploadBytes = 4;
201 SC_TEST_EXPECT(fileServer.setOptions(options));
202
203 httpServer.onRequest = [&](HttpConnection& connection)
204 { SC_ASSERT_RELEASE(fileServer.handleRequest(streams[connection.getConnectionID().getIndex()], connection)); };
205
206 FileSystem fs;
207 SC_TEST_EXPECT(fs.init(webServerFolder));
208
209 HttpTestClient client;
210 String url = StringEncoding::Ascii;
211 SC_TEST_EXPECT(StringBuilder::format(url, "http://127.0.0.1:{}/limited-upload.txt", serverPort));
212 struct UploadPolicyContext
213 {
214 HttpAsyncFileServerTest* test;
215 HttpAsyncServer* httpServer;
216 FileSystem* fs;
217 } context = {this, &httpServer, &fs};
218 client.callback = [&context](HttpTestClient& result)
219 {
220 const StringView response(result.getResponse());
221 context.test->recordExpectation("413 response", response.containsString("413 Payload Too Large"));
222 context.test->recordExpectation("limited file missing", not context.fs->existsAndIsFile("limited-upload.txt"));
223 context.test->recordExpectation("stop server", context.httpServer->stop());
224 };
225
226 SC_TEST_EXPECT(client.put(eventLoop, url.view(), "too-large"));
227
228 AsyncLoopTimeout timeout;
229 timeout.callback = [this](AsyncLoopTimeout::Result&)
230 { SC_TEST_EXPECT("Test never finished. Event Loop is stuck. Timeout expired." && false); };
231 SC_TEST_EXPECT(timeout.start(eventLoop, TimeMs{2000}));
232 eventLoop.excludeFromActiveCount(timeout);
233

Callers

nothing calls this directly

Calls 15

mapPortMethod · 0.80
getResponseMethod · 0.80
recordExpectationMethod · 0.80
existsAndIsFileMethod · 0.80
formatFunction · 0.50
viewMethod · 0.45
createMethod · 0.45
initMethod · 0.45
startMethod · 0.45
setOptionsMethod · 0.45

Tested by

no test coverage detected