Skip to content

Commit 846151b

Browse files
committed
Added a unit test case for large multipart form data
1 parent e44e31d commit 846151b

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
@@ -4792,3 +4792,66 @@ TEST(HttpToHttpsRedirectTest, CertFile) {
47924792
t2.join();
47934793
}
47944794
#endif
4795+
4796+
TEST(MultipartFormDataTest, LargeData) {
4797+
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
4798+
4799+
svr.Post("/post", [&](const Request &req, Response & /*res*/,
4800+
const ContentReader &content_reader) {
4801+
if (req.is_multipart_form_data()) {
4802+
MultipartFormDataItems files;
4803+
content_reader(
4804+
[&](const MultipartFormData &file) {
4805+
files.push_back(file);
4806+
return true;
4807+
},
4808+
[&](const char *data, size_t data_length) {
4809+
files.back().content.append(data, data_length);
4810+
return true;
4811+
});
4812+
4813+
EXPECT_TRUE(std::string(files[0].name) == "document");
4814+
EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size());
4815+
EXPECT_TRUE(files[0].filename == "2MB_data");
4816+
EXPECT_TRUE(files[0].content_type == "application/octet-stream");
4817+
4818+
EXPECT_TRUE(files[1].name == "hello");
4819+
EXPECT_TRUE(files[1].content == "world");
4820+
EXPECT_TRUE(files[1].filename == "");
4821+
EXPECT_TRUE(files[1].content_type == "");
4822+
} else {
4823+
std::string body;
4824+
content_reader([&](const char *data, size_t data_length) {
4825+
body.append(data, data_length);
4826+
return true;
4827+
});
4828+
}
4829+
});
4830+
4831+
auto t = std::thread([&]() { svr.listen("localhost", 8080); });
4832+
while (!svr.is_running()) {
4833+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
4834+
}
4835+
std::this_thread::sleep_for(std::chrono::seconds(1));
4836+
4837+
{
4838+
std::string data(1024 * 1024 * 2, '.');
4839+
std::stringstream buffer;
4840+
buffer << data;
4841+
4842+
Client cli("https://localhost:8080");
4843+
cli.enable_server_certificate_verification(false);
4844+
4845+
MultipartFormDataItems items{
4846+
{"document", buffer.str(), "2MB_data", "application/octet-stream"},
4847+
{"hello", "world", "", ""},
4848+
};
4849+
4850+
auto res = cli.Post("/post", items);
4851+
ASSERT_TRUE(res);
4852+
ASSERT_EQ(200, res->status);
4853+
}
4854+
4855+
svr.stop();
4856+
t.join();
4857+
}

0 commit comments

Comments
 (0)