Update test.cc

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

View File

@ -750,7 +750,7 @@ TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) {
auto host = "google.com"; auto host = "google.com";
auto another_host = "example.com"; auto another_host = "example.com";
auto wrong_ip = "0.0.0.0"; auto wrong_ip = "0.0.0.0";
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLClient cli(host); SSLClient cli(host);
#else #else
@ -766,7 +766,7 @@ TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) {
TEST(SpecifyServerIPAddressTest, RealHostname_Online) { TEST(SpecifyServerIPAddressTest, RealHostname_Online) {
auto host = "google.com"; auto host = "google.com";
auto wrong_ip = "0.0.0.0"; auto wrong_ip = "0.0.0.0";
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLClient cli(host); SSLClient cli(host);
#else #else
@ -1195,12 +1195,13 @@ 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) {
res.status = 500; EXPECT_EQ("abc", std::string(e.what()));
res.set_content("abcdefghijklmnopqrstuvwxyz", res.status = 500;
"text/html"); // <= Content-Length still 13 res.set_content("abcdefghijklmnopqrstuvwxyz",
}); "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) {
res.set_content("Hello World!\n", "text/plain"); res.set_content("Hello World!\n", "text/plain");
@ -1212,15 +1213,28 @@ 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);
auto res = cli.Get("/hi"); for (size_t j = 0; j < 100; j++) {
ASSERT_TRUE(res); auto res = cli.Get("/hi");
EXPECT_EQ(500, res->status); ASSERT_TRUE(res);
EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ(500, res->status);
EXPECT_EQ("26", res->get_header_value("Content-Length")); EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body); EXPECT_EQ("26", res->get_header_value("Content-Length"));
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();
@ -3625,10 +3639,11 @@ 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 =
s += std::string(data, len); client.Get("/stream", [&s](const char *data, size_t len) -> bool {
return true; s += std::string(data, len);
}); return true;
});
EXPECT_EQ("aaabbb", s); EXPECT_EQ("aaabbb", s);
}); });
@ -3766,7 +3781,7 @@ TEST(KeepAliveTest, Issue1041) {
res.set_content("Hello World!", "text/plain"); res.set_content("Hello World!", "text/plain");
}); });
auto a2 = std::async(std::launch::async, [&svr]{ svr.listen(HOST, PORT); }); auto a2 = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); });
std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
Client cli(HOST, PORT); Client cli(HOST, PORT);