Update test.cc

This commit is contained in:
yhirose 2021-12-19 14:17:15 -05:00
parent 3b35279b16
commit 20056f6cda

View File

@ -1195,11 +1195,12 @@ TEST(ErrorHandlerTest, ContentLength) {
TEST(ExceptionHandlerTest, ContentLength) { TEST(ExceptionHandlerTest, ContentLength) {
Server svr; Server svr;
svr.set_exception_handler( svr.set_exception_handler([](const Request & /*req*/, Response &res,
[](const Request & /*req*/, Response &res, std::exception & /*e*/) { std::exception &e) {
EXPECT_EQ("abc", std::string(e.what()));
res.status = 500; res.status = 500;
res.set_content("abcdefghijklmnopqrstuvwxyz", res.set_content("abcdefghijklmnopqrstuvwxyz",
"text/html"); // <= Content-Length still 13 "text/html"); // <= Content-Length still 13 at this point
}); });
svr.Get("/hi", [](const Request & /*req*/, Response &res) { svr.Get("/hi", [](const Request & /*req*/, Response &res) {
@ -1212,9 +1213,10 @@ TEST(ExceptionHandlerTest, ContentLength) {
// Give GET time to get a few messages. // Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
{ for (size_t i = 0; i < 10; i++) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
for (size_t j = 0; j < 100; j++) {
auto res = cli.Get("/hi"); auto res = cli.Get("/hi");
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(500, res->status); EXPECT_EQ(500, res->status);
@ -1223,6 +1225,18 @@ TEST(ExceptionHandlerTest, ContentLength) {
EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body);
} }
cli.set_keep_alive(true);
for (size_t j = 0; j < 100; j++) {
auto res = cli.Get("/hi");
ASSERT_TRUE(res);
EXPECT_EQ(500, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
EXPECT_EQ("26", res->get_header_value("Content-Length"));
EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body);
}
}
svr.stop(); svr.stop();
thread.join(); thread.join();
ASSERT_FALSE(svr.is_running()); ASSERT_FALSE(svr.is_running());
@ -3625,7 +3639,8 @@ TEST(StreamingTest, NoContentLengthStreaming) {
auto get_thread = std::thread([&client]() { auto get_thread = std::thread([&client]() {
std::string s; std::string s;
auto res = client.Get("/stream", [&s](const char *data, size_t len) -> bool { auto res =
client.Get("/stream", [&s](const char *data, size_t len) -> bool {
s += std::string(data, len); s += std::string(data, len);
return true; return true;
}); });