Skip to content

Commit 4ef3708

Browse files
committed
Adds multipart header access test
Verifies the correct retrieval of headers from multipart form data file parts. Ensures that custom and content-related headers are accessible and parsed as expected.
1 parent 320c64f commit 4ef3708

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/test.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8010,6 +8010,69 @@ TEST(MultipartFormDataTest, ContentLength) {
80108010
ASSERT_TRUE(send_request(1, req, &response));
80118011
ASSERT_EQ("200", response.substr(9, 3));
80128012
}
8013+
8014+
TEST(MultipartFormDataTest, AccessPartHeaders) {
8015+
auto handled = false;
8016+
8017+
Server svr;
8018+
svr.Post("/test", [&](const Request &req, Response &) {
8019+
ASSERT_EQ(2u, req.files.size());
8020+
8021+
auto it = req.files.begin();
8022+
ASSERT_EQ("text1", it->second.name);
8023+
ASSERT_EQ("text1", it->second.content);
8024+
ASSERT_EQ(1, it->second.headers.count("Content-Length"));
8025+
auto content_lenght = it->second.headers.find("CONTENT-length");
8026+
ASSERT_EQ("5", content_lenght->second);
8027+
ASSERT_EQ(3, it->second.headers.size());
8028+
8029+
++it;
8030+
ASSERT_EQ("text2", it->second.name);
8031+
ASSERT_EQ("text2", it->second.content);
8032+
auto& headers = it->second.headers;
8033+
ASSERT_EQ(3, headers.size());
8034+
auto customHeader = headers.find("x-whatever");
8035+
ASSERT_TRUE(customHeader != headers.end());
8036+
ASSERT_NE("customvalue", customHeader->second);
8037+
ASSERT_EQ("CustomValue", customHeader->second);
8038+
ASSERT_TRUE(headers.find("X-Test") == headers.end()); //text1 header
8039+
8040+
8041+
handled = true;
8042+
});
8043+
8044+
thread t = thread([&] { svr.listen(HOST, PORT); });
8045+
auto se = detail::scope_exit([&] {
8046+
svr.stop();
8047+
t.join();
8048+
ASSERT_FALSE(svr.is_running());
8049+
ASSERT_TRUE(handled);
8050+
});
8051+
8052+
svr.wait_until_ready();
8053+
8054+
auto req = "POST /test HTTP/1.1\r\n"
8055+
"Content-Type: multipart/form-data;boundary=--------\r\n"
8056+
"Content-Length: 232\r\n"
8057+
"\r\n----------\r\n"
8058+
"Content-Disposition: form-data; name=\"text1\"\r\n"
8059+
"Content-Length: 5\r\n"
8060+
"X-Test: 1\r\n"
8061+
"\r\n"
8062+
"text1"
8063+
"\r\n----------\r\n"
8064+
"Content-Disposition: form-data; name=\"text2\"\r\n"
8065+
"Content-Type: text/plain\r\n"
8066+
"X-Whatever: CustomValue\r\n"
8067+
"\r\n"
8068+
"text2"
8069+
"\r\n------------\r\n"
8070+
"That should be disregarded. Not even read";
8071+
8072+
std::string response;
8073+
ASSERT_TRUE(send_request(1, req, &response));
8074+
ASSERT_EQ("200", response.substr(9, 3));
8075+
}
80138076
#endif
80148077

80158078
TEST(TaskQueueTest, IncreaseAtomicInteger) {

0 commit comments

Comments
 (0)