|
|
|
|
@ -156,7 +156,7 @@ TEST_F(UnixSocketTest, abstract) {
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
TEST(SocketStream, wait_writable_UNIX) {
|
|
|
|
|
TEST(SocketStream, is_writable_UNIX) {
|
|
|
|
|
int fds[2];
|
|
|
|
|
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
|
|
|
|
|
|
|
|
|
|
@ -167,17 +167,17 @@ TEST(SocketStream, wait_writable_UNIX) {
|
|
|
|
|
};
|
|
|
|
|
asSocketStream(fds[0], [&](Stream &s0) {
|
|
|
|
|
EXPECT_EQ(s0.socket(), fds[0]);
|
|
|
|
|
EXPECT_TRUE(s0.wait_writable());
|
|
|
|
|
EXPECT_TRUE(s0.is_writable());
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(0, close(fds[1]));
|
|
|
|
|
EXPECT_FALSE(s0.wait_writable());
|
|
|
|
|
EXPECT_FALSE(s0.is_writable());
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
EXPECT_EQ(0, close(fds[0]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(SocketStream, wait_writable_INET) {
|
|
|
|
|
TEST(SocketStream, is_writable_INET) {
|
|
|
|
|
sockaddr_in addr;
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
@ -212,7 +212,7 @@ TEST(SocketStream, wait_writable_INET) {
|
|
|
|
|
};
|
|
|
|
|
asSocketStream(disconnected_svr_sock, [&](Stream &ss) {
|
|
|
|
|
EXPECT_EQ(ss.socket(), disconnected_svr_sock);
|
|
|
|
|
EXPECT_FALSE(ss.wait_writable());
|
|
|
|
|
EXPECT_FALSE(ss.is_writable());
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
@ -524,37 +524,37 @@ TEST(GetHeaderValueTest, RegularInvalidValueInt) {
|
|
|
|
|
|
|
|
|
|
TEST(GetHeaderValueTest, Range) {
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{1, -1}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{1, -1}})};
|
|
|
|
|
auto val = detail::get_header_value(headers, "Range", 0, 0);
|
|
|
|
|
EXPECT_STREQ("bytes=1-", val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{-1, 1}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{-1, 1}})};
|
|
|
|
|
auto val = detail::get_header_value(headers, "Range", 0, 0);
|
|
|
|
|
EXPECT_STREQ("bytes=-1", val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{1, 10}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{1, 10}})};
|
|
|
|
|
auto val = detail::get_header_value(headers, "Range", 0, 0);
|
|
|
|
|
EXPECT_STREQ("bytes=1-10", val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{1, 10}, {100, -1}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{1, 10}, {100, -1}})};
|
|
|
|
|
auto val = detail::get_header_value(headers, "Range", 0, 0);
|
|
|
|
|
EXPECT_STREQ("bytes=1-10, 100-", val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{1, 10}, {100, 200}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{1, 10}, {100, 200}})};
|
|
|
|
|
auto val = detail::get_header_value(headers, "Range", 0, 0);
|
|
|
|
|
EXPECT_STREQ("bytes=1-10, 100-200", val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{0, 0}, {-1, 1}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{0, 0}, {-1, 1}})};
|
|
|
|
|
auto val = detail::get_header_value(headers, "Range", 0, 0);
|
|
|
|
|
EXPECT_STREQ("bytes=0-0, -1", val);
|
|
|
|
|
}
|
|
|
|
|
@ -800,9 +800,10 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
|
|
|
|
|
#endif
|
|
|
|
|
cli.set_connection_timeout(2);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::string body;
|
|
|
|
|
auto res = cli.Get(
|
|
|
|
|
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
|
|
|
|
|
"/httpgallery/chunked/chunkedimage.aspx",
|
|
|
|
|
[&](const Response &response) {
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, response.status);
|
|
|
|
|
return true;
|
|
|
|
|
@ -818,6 +819,28 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
EXPECT_EQ(out, body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::string body;
|
|
|
|
|
auto res = cli.Get(
|
|
|
|
|
"/httpgallery/chunked/chunkedimage.aspx", Params{},
|
|
|
|
|
[&](const Response &response) {
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, response.status);
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
[&](const char *data, size_t data_length) {
|
|
|
|
|
body.append(data, data_length);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
|
|
|
|
|
std::string out;
|
|
|
|
|
detail::read_file("./image.jpg", out);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
EXPECT_EQ(out, body);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(RangeTest, FromHTTPBin_Online) {
|
|
|
|
|
@ -846,7 +869,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{1, -1}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{1, -1}})};
|
|
|
|
|
auto res = cli.Get(path, headers);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
|
|
|
|
@ -854,7 +877,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{1, 10}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{1, 10}})};
|
|
|
|
|
auto res = cli.Get(path, headers);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("bcdefghijk", res->body);
|
|
|
|
|
@ -862,7 +885,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{0, 31}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{0, 31}})};
|
|
|
|
|
auto res = cli.Get(path, headers);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
|
|
|
|
@ -870,7 +893,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{0, -1}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{0, -1}})};
|
|
|
|
|
auto res = cli.Get(path, headers);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
|
|
|
|
|
@ -878,7 +901,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Headers headers = {make_range_header({{0, 32}})};
|
|
|
|
|
auto headers = Headers{make_range_header({{0, 32}})};
|
|
|
|
|
auto res = cli.Get(path, headers);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
@ -1064,9 +1087,8 @@ TEST(CancelTest, NoCancelPost) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return true; });
|
|
|
|
|
auto res = cli.Post("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return true; });
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("Hello World!", res->body);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
@ -1091,9 +1113,8 @@ TEST(CancelTest, WithCancelSmallPayloadPost) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Post("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1117,9 +1138,8 @@ TEST(CancelTest, WithCancelLargePayloadPost) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Post("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1143,9 +1163,8 @@ TEST(CancelTest, NoCancelPut) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return true; });
|
|
|
|
|
auto res = cli.Put("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return true; });
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("Hello World!", res->body);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
@ -1170,9 +1189,8 @@ TEST(CancelTest, WithCancelSmallPayloadPut) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Put("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1196,9 +1214,8 @@ TEST(CancelTest, WithCancelLargePayloadPut) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Put("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1222,9 +1239,8 @@ TEST(CancelTest, NoCancelPatch) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return true; });
|
|
|
|
|
auto res = cli.Patch("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return true; });
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("Hello World!", res->body);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
@ -1249,9 +1265,8 @@ TEST(CancelTest, WithCancelSmallPayloadPatch) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Patch("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1275,9 +1290,8 @@ TEST(CancelTest, WithCancelLargePayloadPatch) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Patch("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1301,9 +1315,8 @@ TEST(CancelTest, NoCancelDelete) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return true; });
|
|
|
|
|
auto res = cli.Delete("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return true; });
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("Hello World!", res->body);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
@ -1328,9 +1341,8 @@ TEST(CancelTest, WithCancelSmallPayloadDelete) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Delete("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1354,9 +1366,8 @@ TEST(CancelTest, WithCancelLargePayloadDelete) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.set_connection_timeout(std::chrono::seconds(5));
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
|
|
|
|
"application/json", [](uint64_t, uint64_t) { return false; });
|
|
|
|
|
auto res = cli.Delete("/", JSON_DATA, "application/json",
|
|
|
|
|
[](uint64_t, uint64_t) { return false; });
|
|
|
|
|
ASSERT_TRUE(!res);
|
|
|
|
|
EXPECT_EQ(Error::Canceled, res.error());
|
|
|
|
|
}
|
|
|
|
|
@ -1385,8 +1396,8 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Get(path, {make_basic_authentication_header("hello", "world")});
|
|
|
|
|
auto res = cli.Get(
|
|
|
|
|
path, Headers{make_basic_authentication_header("hello", "world")});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
|
|
|
|
|
res->body);
|
|
|
|
|
@ -1603,7 +1614,7 @@ TEST(HttpsToHttpRedirectTest2, Redirect_Online) {
|
|
|
|
|
params.emplace("url", "http://www.google.com");
|
|
|
|
|
params.emplace("status_code", "302");
|
|
|
|
|
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to", params);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
}
|
|
|
|
|
@ -1615,7 +1626,7 @@ TEST(HttpsToHttpRedirectTest3, Redirect_Online) {
|
|
|
|
|
Params params;
|
|
|
|
|
params.emplace("url", "http://www.google.com");
|
|
|
|
|
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
}
|
|
|
|
|
@ -2041,7 +2052,7 @@ TEST(ErrorHandlerTest, ContentLength) {
|
|
|
|
|
{
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
|
|
|
|
|
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
|
|
|
|
|
auto res = cli.Get("/hi", Headers{{"Accept-Encoding", ""}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -2124,7 +2135,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < 100; j++) {
|
|
|
|
|
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
|
|
|
|
|
auto res = cli.Get("/hi", Headers{{"Accept-Encoding", ""}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
|
|
|
|
|
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -2135,7 +2146,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
|
|
|
|
|
cli.set_keep_alive(true);
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < 100; j++) {
|
|
|
|
|
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
|
|
|
|
|
auto res = cli.Get("/hi", Headers{{"Accept-Encoding", ""}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
|
|
|
|
|
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -3079,7 +3090,7 @@ TEST_F(ServerTest, GetFileContent) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetFileContentWithRange) {
|
|
|
|
|
auto res = cli_.Get("/file_content", {{make_range_header({{1, 3}})}});
|
|
|
|
|
auto res = cli_.Get("/file_content", Headers{{make_range_header({{1, 3}})}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -3401,7 +3412,7 @@ TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, StaticFileRange) {
|
|
|
|
|
auto res = cli_.Get("/dir/test.abcde", {{make_range_header({{2, 3}})}});
|
|
|
|
|
auto res = cli_.Get("/dir/test.abcde", Headers{make_range_header({{2, 3}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -3412,8 +3423,8 @@ TEST_F(ServerTest, StaticFileRange) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, StaticFileRanges) {
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/dir/test.abcde", {{make_range_header({{1, 2}, {4, -1}})}});
|
|
|
|
|
auto res = cli_.Get("/dir/test.abcde",
|
|
|
|
|
Headers{make_range_header({{1, 2}, {4, -1}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
|
@ -3425,7 +3436,7 @@ TEST_F(ServerTest, StaticFileRanges) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, StaticFileRangeHead) {
|
|
|
|
|
auto res = cli_.Head("/dir/test.abcde", {{make_range_header({{2, 3}})}});
|
|
|
|
|
auto res = cli_.Head("/dir/test.abcde", Headers{make_range_header({{2, 3}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -3435,7 +3446,7 @@ TEST_F(ServerTest, StaticFileRangeHead) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, StaticFileRangeBigFile) {
|
|
|
|
|
auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{-1, 5}})}});
|
|
|
|
|
auto res = cli_.Get("/dir/1MB.txt", Headers{make_range_header({{-1, 5}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -3447,7 +3458,7 @@ TEST_F(ServerTest, StaticFileRangeBigFile) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, StaticFileRangeBigFile2) {
|
|
|
|
|
auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{1, 4097}})}});
|
|
|
|
|
auto res = cli_.Get("/dir/1MB.txt", Headers{make_range_header({{1, 4097}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
|
|
|
|
|
@ -3789,7 +3800,7 @@ TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamed2) {
|
|
|
|
|
auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
|
|
|
|
|
auto res = cli_.Get("/streamed", Headers{make_range_header({{2, 3}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("2", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3807,7 +3818,8 @@ TEST_F(ServerTest, GetStreamed) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithRange1) {
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/streamed-with-range", Headers{make_range_header({{3, 5}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("3", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3817,7 +3829,8 @@ TEST_F(ServerTest, GetStreamedWithRange1) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithRange2) {
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/streamed-with-range", Headers{make_range_header({{1, -1}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("6", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3827,7 +3840,7 @@ TEST_F(ServerTest, GetStreamedWithRange2) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithRangeSuffix1) {
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-3"}});
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range", Headers{{"Range", "bytes=-3"}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("3", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3837,7 +3850,8 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix1) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithRangeSuffix2) {
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range?error", {{"Range", "bytes=-9999"}});
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/streamed-with-range?error", Headers{{"Range", "bytes=-9999"}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
EXPECT_EQ("0", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3846,8 +3860,9 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix2) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithRangeError) {
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range",
|
|
|
|
|
{{"Range", "bytes=92233720368547758079223372036854775806-"
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/streamed-with-range",
|
|
|
|
|
Headers{{"Range", "bytes=92233720368547758079223372036854775806-"
|
|
|
|
|
"92233720368547758079223372036854775807"}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
@ -3859,7 +3874,8 @@ TEST_F(ServerTest, GetStreamedWithRangeError) {
|
|
|
|
|
TEST_F(ServerTest, GetRangeWithMaxLongLength) {
|
|
|
|
|
auto res = cli_.Get(
|
|
|
|
|
"/with-range",
|
|
|
|
|
{{"Range", "bytes=0-" + std::to_string(std::numeric_limits<long>::max())},
|
|
|
|
|
Headers{{"Range",
|
|
|
|
|
"bytes=0-" + std::to_string(std::numeric_limits<long>::max())},
|
|
|
|
|
{"Accept-Encoding", ""}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
@ -3870,7 +3886,7 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetRangeWithZeroToInfinite) {
|
|
|
|
|
auto res = cli_.Get("/with-range", {
|
|
|
|
|
auto res = cli_.Get("/with-range", Headers{
|
|
|
|
|
{"Range", "bytes=0-"},
|
|
|
|
|
{"Accept-Encoding", ""},
|
|
|
|
|
});
|
|
|
|
|
@ -3883,8 +3899,8 @@ TEST_F(ServerTest, GetRangeWithZeroToInfinite) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range",
|
|
|
|
|
Headers{make_range_header({{1, 2}, {4, 5}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("267", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3898,8 +3914,8 @@ TEST_F(ServerTest, GetStreamedWithTooManyRanges) {
|
|
|
|
|
ranges.emplace_back(0, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/streamed-with-range?error", {{make_range_header(ranges)}});
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range?error",
|
|
|
|
|
Headers{make_range_header(ranges)});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
EXPECT_EQ("0", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3909,7 +3925,7 @@ TEST_F(ServerTest, GetStreamedWithTooManyRanges) {
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) {
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range?error",
|
|
|
|
|
{{make_range_header({{0, -1}, {0, -1}})}});
|
|
|
|
|
Headers{make_range_header({{0, -1}, {0, -1}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
EXPECT_EQ("0", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3918,8 +3934,9 @@ TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetStreamedWithRangesMoreThanTwoOverwrapping) {
|
|
|
|
|
auto res = cli_.Get("/streamed-with-range?error",
|
|
|
|
|
{{make_range_header({{0, 1}, {1, 2}, {2, 3}, {3, 4}})}});
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/streamed-with-range?error",
|
|
|
|
|
Headers{make_range_header({{0, 1}, {1, 2}, {2, 3}, {3, 4}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
EXPECT_EQ("0", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -3969,7 +3986,7 @@ TEST_F(ServerTest, ClientStop) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRange1) {
|
|
|
|
|
auto res = cli_.Get("/with-range", {
|
|
|
|
|
auto res = cli_.Get("/with-range", Headers{
|
|
|
|
|
make_range_header({{3, 5}}),
|
|
|
|
|
{"Accept-Encoding", ""},
|
|
|
|
|
});
|
|
|
|
|
@ -3982,7 +3999,7 @@ TEST_F(ServerTest, GetWithRange1) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRange2) {
|
|
|
|
|
auto res = cli_.Get("/with-range", {
|
|
|
|
|
auto res = cli_.Get("/with-range", Headers{
|
|
|
|
|
make_range_header({{1, -1}}),
|
|
|
|
|
{"Accept-Encoding", ""},
|
|
|
|
|
});
|
|
|
|
|
@ -3995,7 +4012,7 @@ TEST_F(ServerTest, GetWithRange2) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRange3) {
|
|
|
|
|
auto res = cli_.Get("/with-range", {
|
|
|
|
|
auto res = cli_.Get("/with-range", Headers{
|
|
|
|
|
make_range_header({{0, 0}}),
|
|
|
|
|
{"Accept-Encoding", ""},
|
|
|
|
|
});
|
|
|
|
|
@ -4008,7 +4025,7 @@ TEST_F(ServerTest, GetWithRange3) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRange4) {
|
|
|
|
|
auto res = cli_.Get("/with-range", {
|
|
|
|
|
auto res = cli_.Get("/with-range", Headers{
|
|
|
|
|
make_range_header({{-1, 2}}),
|
|
|
|
|
{"Accept-Encoding", ""},
|
|
|
|
|
});
|
|
|
|
|
@ -4021,13 +4038,15 @@ TEST_F(ServerTest, GetWithRange4) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) {
|
|
|
|
|
auto res = cli_.Get("/with-range", {{make_range_header({{10000, 20000}})}});
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/with-range", Headers{make_range_header({{10000, 20000}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRangeMultipart) {
|
|
|
|
|
auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/with-range", Headers{make_range_header({{1, 2}, {4, 5}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
|
|
|
|
EXPECT_EQ("267", res->get_header_value("Content-Length"));
|
|
|
|
|
@ -4036,15 +4055,15 @@ TEST_F(ServerTest, GetWithRangeMultipart) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
|
|
|
|
|
auto res =
|
|
|
|
|
cli_.Get("/with-range", {{make_range_header({{-1, 2}, {10000, 30000}})}});
|
|
|
|
|
auto res = cli_.Get("/with-range",
|
|
|
|
|
Headers{make_range_header({{-1, 2}, {10000, 30000}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
|
|
|
|
|
auto res = cli_.Get("/with-range-customized-response",
|
|
|
|
|
{{make_range_header({{1, 2}})}});
|
|
|
|
|
Headers{make_range_header({{1, 2}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
|
|
|
|
|
EXPECT_EQ(true, res->has_header("Content-Length"));
|
|
|
|
|
@ -4054,7 +4073,7 @@ TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) {
|
|
|
|
|
auto res = cli_.Get("/with-range-customized-response",
|
|
|
|
|
{{make_range_header({{1, 2}, {4, 5}})}});
|
|
|
|
|
Headers{make_range_header({{1, 2}, {4, 5}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::BadRequest_400, res->status);
|
|
|
|
|
EXPECT_EQ(true, res->has_header("Content-Length"));
|
|
|
|
|
@ -4063,7 +4082,7 @@ TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ServerTest, Issue1772) {
|
|
|
|
|
auto res = cli_.Get("/issue1772", {{make_range_header({{1000, -1}})}});
|
|
|
|
|
auto res = cli_.Get("/issue1772", Headers{make_range_header({{1000, -1}})});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
|
|
|
|
|
}
|
|
|
|
|
@ -5766,7 +5785,7 @@ TEST(GetWithParametersTest, GetWithParameters) {
|
|
|
|
|
params.emplace("hello", "world");
|
|
|
|
|
params.emplace("hello2", "world2");
|
|
|
|
|
params.emplace("hello3", "world3");
|
|
|
|
|
auto res = cli.Get("/", params, Headers{});
|
|
|
|
|
auto res = cli.Get("/", params);
|
|
|
|
|
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
@ -5823,8 +5842,7 @@ TEST(GetWithParametersTest, GetWithParameters2) {
|
|
|
|
|
params.emplace("hello", "world");
|
|
|
|
|
|
|
|
|
|
std::string body;
|
|
|
|
|
auto res = cli.Get("/", params, Headers{},
|
|
|
|
|
[&](const char *data, size_t data_length) {
|
|
|
|
|
auto res = cli.Get("/", params, [&](const char *data, size_t data_length) {
|
|
|
|
|
body.append(data, data_length);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
@ -5849,7 +5867,7 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
|
|
|
|
|
Client cli(host);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
cli.set_default_headers({make_range_header({{1, 10}})});
|
|
|
|
|
cli.set_default_headers(Headers{make_range_header({{1, 10}})});
|
|
|
|
|
cli.set_connection_timeout(5);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
@ -6669,7 +6687,7 @@ TEST(SendAPI, WithParamsInRequest) {
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto res = cli.Get("/", {{"test", "test_value"}}, Headers{});
|
|
|
|
|
auto res = cli.Get("/", Params{{"test", "test_value"}});
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -6798,8 +6816,8 @@ TEST(YahooRedirectTest3, NewResultInterface_Online) {
|
|
|
|
|
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
|
|
|
|
|
TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) {
|
|
|
|
|
Client cli("https://cdnjs.cloudflare.com");
|
|
|
|
|
auto res =
|
|
|
|
|
cli.Get("/ajax/libs/jquery/3.5.1/jquery.js", {{"Accept-Encoding", "br"}});
|
|
|
|
|
auto res = cli.Get("/ajax/libs/jquery/3.5.1/jquery.js",
|
|
|
|
|
Headers{{"Accept-Encoding", "br"}});
|
|
|
|
|
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
@ -6828,7 +6846,7 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) {
|
|
|
|
|
params.emplace("url", "http://www.google.com");
|
|
|
|
|
params.emplace("status_code", "302");
|
|
|
|
|
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to", params);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
}
|
|
|
|
|
@ -6840,7 +6858,7 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) {
|
|
|
|
|
Params params;
|
|
|
|
|
params.emplace("url", "http://www.google.com");
|
|
|
|
|
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
|
|
|
|
|
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params);
|
|
|
|
|
ASSERT_TRUE(res);
|
|
|
|
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
|
|
|
|
}
|
|
|
|
|
@ -7994,7 +8012,7 @@ TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) {
|
|
|
|
|
svr.wait_until_ready();
|
|
|
|
|
|
|
|
|
|
Client cli(HOST, PORT);
|
|
|
|
|
cli.Get("/test", {{"Test", "_\n\r_\n\r_"}});
|
|
|
|
|
cli.Get("/test", Headers{{"Test", "_\n\r_\n\r_"}});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(InvalidHeaderCharsTest, is_field_name) {
|
|
|
|
|
@ -8207,8 +8225,9 @@ TEST(Expect100ContinueTest, ServerClosesConnection) {
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
template <typename S, typename C>
|
|
|
|
|
inline void max_timeout_test(S &svr, C &cli, time_t timeout, time_t threshold) {
|
|
|
|
|
TEST(MaxTimeoutTest, ContentStream) {
|
|
|
|
|
Server svr;
|
|
|
|
|
|
|
|
|
|
svr.Get("/stream", [&](const Request &, Response &res) {
|
|
|
|
|
auto data = new std::string("01234567890123456789");
|
|
|
|
|
|
|
|
|
|
@ -8278,6 +8297,10 @@ inline void max_timeout_test(S &svr, C &cli, time_t timeout, time_t threshold) {
|
|
|
|
|
|
|
|
|
|
svr.wait_until_ready();
|
|
|
|
|
|
|
|
|
|
const time_t timeout = 2000;
|
|
|
|
|
const time_t threshold = 200;
|
|
|
|
|
|
|
|
|
|
Client cli("localhost", PORT);
|
|
|
|
|
cli.set_max_timeout(std::chrono::milliseconds(timeout));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
@ -8329,108 +8352,132 @@ inline void max_timeout_test(S &svr, C &cli, time_t timeout, time_t threshold) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(MaxTimeoutTest, ContentStream) {
|
|
|
|
|
time_t timeout = 2000;
|
|
|
|
|
time_t threshold = 200;
|
|
|
|
|
|
|
|
|
|
Server svr;
|
|
|
|
|
Client cli("localhost", PORT);
|
|
|
|
|
max_timeout_test(svr, cli, timeout, threshold);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
|
|
|
|
TEST(MaxTimeoutTest, ContentStreamSSL) {
|
|
|
|
|
time_t timeout = 2000;
|
|
|
|
|
time_t threshold = 500; // SSL_shutdown is slow on some operating systems.
|
|
|
|
|
|
|
|
|
|
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
|
|
|
|
|
|
|
|
|
SSLClient cli("localhost", PORT);
|
|
|
|
|
cli.enable_server_certificate_verification(false);
|
|
|
|
|
svr.Get("/stream", [&](const Request &, Response &res) {
|
|
|
|
|
auto data = new std::string("01234567890123456789");
|
|
|
|
|
|
|
|
|
|
max_timeout_test(svr, cli, timeout, threshold);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
class EventDispatcher {
|
|
|
|
|
public:
|
|
|
|
|
EventDispatcher() {}
|
|
|
|
|
|
|
|
|
|
void wait_event(DataSink *sink) {
|
|
|
|
|
unique_lock<mutex> lk(m_);
|
|
|
|
|
int id = id_;
|
|
|
|
|
cv_.wait(lk, [&] { return cid_ == id; });
|
|
|
|
|
sink->write(message_.data(), message_.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void send_event(const string &message) {
|
|
|
|
|
lock_guard<mutex> lk(m_);
|
|
|
|
|
cid_ = id_++;
|
|
|
|
|
message_ = message;
|
|
|
|
|
cv_.notify_all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
mutex m_;
|
|
|
|
|
condition_variable cv_;
|
|
|
|
|
atomic_int id_{0};
|
|
|
|
|
atomic_int cid_{-1};
|
|
|
|
|
string message_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST(ClientInThreadTest, Issue2068) {
|
|
|
|
|
EventDispatcher ed;
|
|
|
|
|
|
|
|
|
|
Server svr;
|
|
|
|
|
svr.Get("/event1", [&](const Request & /*req*/, Response &res) {
|
|
|
|
|
res.set_chunked_content_provider("text/event-stream",
|
|
|
|
|
[&](size_t /*offset*/, DataSink &sink) {
|
|
|
|
|
ed.wait_event(&sink);
|
|
|
|
|
res.set_content_provider(
|
|
|
|
|
data->size(), "text/plain",
|
|
|
|
|
[&, data](size_t offset, size_t length, DataSink &sink) {
|
|
|
|
|
const size_t DATA_CHUNK_SIZE = 4;
|
|
|
|
|
const auto &d = *data;
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE));
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
[data](bool success) {
|
|
|
|
|
EXPECT_FALSE(success);
|
|
|
|
|
delete data;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); });
|
|
|
|
|
svr.Get("/stream_without_length", [&](const Request &, Response &res) {
|
|
|
|
|
auto i = new size_t(0);
|
|
|
|
|
|
|
|
|
|
svr.wait_until_ready();
|
|
|
|
|
|
|
|
|
|
thread event_thread([&] {
|
|
|
|
|
int id = 0;
|
|
|
|
|
while (svr.is_running()) {
|
|
|
|
|
this_thread::sleep_for(chrono::milliseconds(500));
|
|
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << "data: " << id << "\n\n";
|
|
|
|
|
ed.send_event(ss.str());
|
|
|
|
|
id++;
|
|
|
|
|
res.set_content_provider(
|
|
|
|
|
"text/plain",
|
|
|
|
|
[i](size_t, DataSink &sink) {
|
|
|
|
|
if (*i < 5) {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
sink.write("abcd", 4);
|
|
|
|
|
(*i)++;
|
|
|
|
|
} else {
|
|
|
|
|
sink.done();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
[i](bool success) {
|
|
|
|
|
EXPECT_FALSE(success);
|
|
|
|
|
delete i;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
svr.Get("/chunked", [&](const Request &, Response &res) {
|
|
|
|
|
auto i = new size_t(0);
|
|
|
|
|
|
|
|
|
|
res.set_chunked_content_provider(
|
|
|
|
|
"text/plain",
|
|
|
|
|
[i](size_t, DataSink &sink) {
|
|
|
|
|
if (*i < 5) {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
sink.os << "abcd";
|
|
|
|
|
(*i)++;
|
|
|
|
|
} else {
|
|
|
|
|
sink.done();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
[i](bool success) {
|
|
|
|
|
EXPECT_FALSE(success);
|
|
|
|
|
delete i;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
|
|
|
|
|
auto se = detail::scope_exit([&] {
|
|
|
|
|
svr.stop();
|
|
|
|
|
|
|
|
|
|
listen_thread.join();
|
|
|
|
|
event_thread.join();
|
|
|
|
|
|
|
|
|
|
ASSERT_FALSE(svr.is_running());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
svr.wait_until_ready();
|
|
|
|
|
|
|
|
|
|
const time_t timeout = 2000;
|
|
|
|
|
const time_t threshold = 1000; // SSL_shutdown is slow...
|
|
|
|
|
|
|
|
|
|
SSLClient cli("localhost", PORT);
|
|
|
|
|
cli.enable_server_certificate_verification(false);
|
|
|
|
|
cli.set_max_timeout(std::chrono::milliseconds(timeout));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto client = detail::make_unique<Client>(HOST, PORT);
|
|
|
|
|
client->set_read_timeout(std::chrono::minutes(10));
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
std::atomic<bool> stop{false};
|
|
|
|
|
auto res = cli.Get("/stream");
|
|
|
|
|
|
|
|
|
|
std::thread t([&] {
|
|
|
|
|
client->Get("/event1",
|
|
|
|
|
[&](const char *, size_t) -> bool { return !stop; });
|
|
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
|
std::chrono::steady_clock::now() - start)
|
|
|
|
|
.count();
|
|
|
|
|
|
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
|
EXPECT_EQ(Error::Read, res.error());
|
|
|
|
|
EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold)
|
|
|
|
|
<< "Timeout exceeded by " << (elapsed - timeout) << "ms";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
auto res = cli.Get("/stream_without_length");
|
|
|
|
|
|
|
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
|
std::chrono::steady_clock::now() - start)
|
|
|
|
|
.count();
|
|
|
|
|
|
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
|
EXPECT_EQ(Error::Read, res.error());
|
|
|
|
|
EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold)
|
|
|
|
|
<< "Timeout exceeded by " << (elapsed - timeout) << "ms";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
|
|
auto res = cli.Get("/chunked", [&](const char *data, size_t data_length) {
|
|
|
|
|
EXPECT_EQ("abcd", string(data, data_length));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
|
|
|
stop = true;
|
|
|
|
|
client->stop();
|
|
|
|
|
client.reset();
|
|
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
|
std::chrono::steady_clock::now() - start)
|
|
|
|
|
.count();
|
|
|
|
|
|
|
|
|
|
t.join();
|
|
|
|
|
ASSERT_FALSE(res);
|
|
|
|
|
EXPECT_EQ(Error::Read, res.error());
|
|
|
|
|
EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold)
|
|
|
|
|
<< "Timeout exceeded by " << (elapsed - timeout) << "ms";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|