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

Method customMimeLookup

Tests/Libraries/Http/HttpAsyncFileServerTest.cpp:76–147  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

74};
75
76void SC::HttpAsyncFileServerTest::customMimeLookup()
77{
78 StringView webServerFolder = report.applicationRootDirectory.view();
79 AsyncEventLoop eventLoop;
80 SC_TEST_EXPECT(eventLoop.create());
81
82 using HttpConnectionType = HttpAsyncConnection<2, 2, 8 * 1024, 8 * 1024>;
83
84 HttpConnectionType connections[1];
85 HttpAsyncFileServer::StreamQueue<2> streams[1];
86 HttpAsyncServer httpServer;
87 HttpAsyncFileServer fileServer;
88 ThreadPool threadPool;
89 const uint16_t serverPort = report.mapPort(26120);
90
91 if (eventLoop.needsThreadPoolForFileOperations())
92 {
93 SC_TEST_EXPECT(threadPool.create(2));
94 }
95 SC_TEST_EXPECT(httpServer.init(Span<HttpConnectionType>(connections)));
96 SC_TEST_EXPECT(httpServer.start(eventLoop, "127.0.0.1", serverPort));
97 SC_TEST_EXPECT(fileServer.init(threadPool, eventLoop, webServerFolder));
98
99 int lookupCount = 0;
100 HttpAsyncFileServerOptions options;
101 options.mimeTypeLookup = httpAsyncFileServerTestMimeLookup;
102 options.mimeTypeUserData = &lookupCount;
103 SC_TEST_EXPECT(fileServer.setOptions(options));
104
105 httpServer.onRequest = [&](HttpConnection& connection)
106 { SC_ASSERT_RELEASE(fileServer.handleRequest(streams[connection.getConnectionID().getIndex()], connection)); };
107
108 FileSystem fs;
109 SC_TEST_EXPECT(fs.init(webServerFolder));
110 SC_TEST_EXPECT(fs.writeString("custom.sane", "custom mime"));
111
112 HttpTestClient client;
113 String url = StringEncoding::Ascii;
114 SC_TEST_EXPECT(StringBuilder::format(url, "http://127.0.0.1:{}/custom.sane", serverPort));
115
116 struct CustomMimeContext
117 {
118 HttpAsyncFileServerTest* test;
119 HttpAsyncServer* httpServer;
120 FileSystem* fs;
121 int* lookupCount;
122 } context = {this, &httpServer, &fs, &lookupCount};
123 client.callback = [&context](HttpTestClient& result)
124 {
125 const StringView response(result.getResponse());
126 context.test->recordExpectation("custom MIME response", response.containsString("200 OK"));
127 context.test->recordExpectation("custom MIME type",
128 response.containsString("Content-Type: application/x-sane"));
129 context.test->recordExpectation("custom MIME body", response.containsString("custom mime"));
130 context.test->recordExpectation("custom MIME lookup called", *context.lookupCount == 1);
131 context.test->recordExpectation("remove custom MIME fixture", context.fs->removeFile("custom.sane"));
132 context.test->recordExpectation("stop server", context.httpServer->stop());
133 };

Callers

nothing calls this directly

Calls 15

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

Tested by

no test coverage detected