This commit is contained in:
yhirose 2025-02-22 02:48:52 +00:00 committed by GitHub
commit 0486f0ca57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 281 additions and 607 deletions

View File

@ -578,7 +578,7 @@ auto res = cli.Get("/hi", headers);
``` ```
or or
```c++ ```c++
auto res = cli.Get("/hi", {{"Hello", "World!"}}); auto res = cli.Get("/hi", httplib::Headers{{"Hello", "World!"}});
``` ```
or or
```c++ ```c++
@ -675,7 +675,7 @@ auto res = cli.Get("/large-data",
std::string body; std::string body;
auto res = cli.Get( auto res = cli.Get(
"/stream", Headers(), "/stream",
[&](const Response &response) { [&](const Response &response) {
EXPECT_EQ(StatusCode::OK_200, response.status); EXPECT_EQ(StatusCode::OK_200, response.status);
return true; // return 'false' if you want to cancel the request. return true; // return 'false' if you want to cancel the request.
@ -847,13 +847,13 @@ The default `Accept-Encoding` value contains all possible compression types. So,
```c++ ```c++
res = cli.Get("/resource/foo"); res = cli.Get("/resource/foo");
res = cli.Get("/resource/foo", {{"Accept-Encoding", "gzip, deflate, br"}}); res = cli.Get("/resource/foo", httplib::Headers{{"Accept-Encoding", "gzip, deflate, br"}});
``` ```
If we don't want a response without compression, we have to set `Accept-Encoding` to an empty string. This behavior is similar to curl. If we don't want a response without compression, we have to set `Accept-Encoding` to an empty string. This behavior is similar to curl.
```c++ ```c++
res = cli.Get("/resource/foo", {{"Accept-Encoding", ""}}); res = cli.Get("/resource/foo", httplib::Headers{{"Accept-Encoding", ""}});
``` ```
### Compress request body on client ### Compress request body on client

626
httplib.h

File diff suppressed because it is too large Load Diff

View File

@ -524,37 +524,37 @@ TEST(GetHeaderValueTest, RegularInvalidValueInt) {
TEST(GetHeaderValueTest, Range) { 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); auto val = detail::get_header_value(headers, "Range", 0, 0);
EXPECT_STREQ("bytes=1-", val); 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); auto val = detail::get_header_value(headers, "Range", 0, 0);
EXPECT_STREQ("bytes=-1", val); 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); auto val = detail::get_header_value(headers, "Range", 0, 0);
EXPECT_STREQ("bytes=1-10", val); 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); auto val = detail::get_header_value(headers, "Range", 0, 0);
EXPECT_STREQ("bytes=1-10, 100-", val); 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); auto val = detail::get_header_value(headers, "Range", 0, 0);
EXPECT_STREQ("bytes=1-10, 100-200", val); 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); auto val = detail::get_header_value(headers, "Range", 0, 0);
EXPECT_STREQ("bytes=0-0, -1", val); EXPECT_STREQ("bytes=0-0, -1", val);
} }
@ -800,24 +800,47 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
#endif #endif
cli.set_connection_timeout(2); cli.set_connection_timeout(2);
std::string body; {
auto res = cli.Get( std::string body;
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", auto res = cli.Get(
[&](const Response &response) { "/httpgallery/chunked/chunkedimage.aspx",
EXPECT_EQ(StatusCode::OK_200, response.status); [&](const Response &response) {
return true; EXPECT_EQ(StatusCode::OK_200, response.status);
}, return true;
[&](const char *data, size_t data_length) { },
body.append(data, data_length); [&](const char *data, size_t data_length) {
return true; body.append(data, data_length);
}); return true;
ASSERT_TRUE(res); });
ASSERT_TRUE(res);
std::string out; std::string out;
detail::read_file("./image.jpg", out); detail::read_file("./image.jpg", out);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ(out, body); 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) { 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); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body); 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); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("bcdefghijk", res->body); 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); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); 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); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); 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); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
@ -1064,9 +1087,8 @@ TEST(CancelTest, NoCancelPost) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Post("/", JSON_DATA, "application/json",
cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return true; });
"application/json", [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("Hello World!", res->body); EXPECT_EQ("Hello World!", res->body);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@ -1091,9 +1113,8 @@ TEST(CancelTest, WithCancelSmallPayloadPost) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Post("/", JSON_DATA, "application/json",
cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1117,9 +1138,8 @@ TEST(CancelTest, WithCancelLargePayloadPost) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Post("/", JSON_DATA, "application/json",
cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1143,9 +1163,8 @@ TEST(CancelTest, NoCancelPut) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Put("/", JSON_DATA, "application/json",
cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return true; });
"application/json", [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("Hello World!", res->body); EXPECT_EQ("Hello World!", res->body);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@ -1170,9 +1189,8 @@ TEST(CancelTest, WithCancelSmallPayloadPut) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Put("/", JSON_DATA, "application/json",
cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1196,9 +1214,8 @@ TEST(CancelTest, WithCancelLargePayloadPut) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Put("/", JSON_DATA, "application/json",
cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1222,9 +1239,8 @@ TEST(CancelTest, NoCancelPatch) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Patch("/", JSON_DATA, "application/json",
cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return true; });
"application/json", [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("Hello World!", res->body); EXPECT_EQ("Hello World!", res->body);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@ -1249,9 +1265,8 @@ TEST(CancelTest, WithCancelSmallPayloadPatch) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Patch("/", JSON_DATA, "application/json",
cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1275,9 +1290,8 @@ TEST(CancelTest, WithCancelLargePayloadPatch) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Patch("/", JSON_DATA, "application/json",
cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1301,9 +1315,8 @@ TEST(CancelTest, NoCancelDelete) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Delete("/", JSON_DATA, "application/json",
cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return true; });
"application/json", [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("Hello World!", res->body); EXPECT_EQ("Hello World!", res->body);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@ -1328,9 +1341,8 @@ TEST(CancelTest, WithCancelSmallPayloadDelete) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Delete("/", JSON_DATA, "application/json",
cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1354,9 +1366,8 @@ TEST(CancelTest, WithCancelLargePayloadDelete) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = auto res = cli.Delete("/", JSON_DATA, "application/json",
cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), [](uint64_t, uint64_t) { return false; });
"application/json", [](uint64_t, uint64_t) { return false; });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@ -1385,8 +1396,8 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
} }
{ {
auto res = auto res = cli.Get(
cli.Get(path, {make_basic_authentication_header("hello", "world")}); path, Headers{make_basic_authentication_header("hello", "world")});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
res->body); res->body);
@ -1603,7 +1614,7 @@ TEST(HttpsToHttpRedirectTest2, Redirect_Online) {
params.emplace("url", "http://www.google.com"); params.emplace("url", "http://www.google.com");
params.emplace("status_code", "302"); 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@ -1615,7 +1626,7 @@ TEST(HttpsToHttpRedirectTest3, Redirect_Online) {
Params params; Params params;
params.emplace("url", "http://www.google.com"); 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@ -2041,7 +2052,7 @@ TEST(ErrorHandlerTest, ContentLength) {
{ {
Client cli(HOST, PORT); Client cli(HOST, PORT);
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}}); auto res = cli.Get("/hi", Headers{{"Accept-Encoding", ""}});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
@ -2124,7 +2135,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
Client cli(HOST, PORT); Client cli(HOST, PORT);
for (size_t j = 0; j < 100; j++) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
@ -2135,7 +2146,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
cli.set_keep_alive(true); cli.set_keep_alive(true);
for (size_t j = 0; j < 100; j++) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
@ -3079,7 +3090,7 @@ TEST_F(ServerTest, GetFileContent) {
} }
TEST_F(ServerTest, GetFileContentWithRange) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
@ -3401,7 +3412,7 @@ TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
} }
TEST_F(ServerTest, StaticFileRange) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
@ -3412,8 +3423,8 @@ TEST_F(ServerTest, StaticFileRange) {
} }
TEST_F(ServerTest, StaticFileRanges) { TEST_F(ServerTest, StaticFileRanges) {
auto res = auto res = cli_.Get("/dir/test.abcde",
cli_.Get("/dir/test.abcde", {{make_range_header({{1, 2}, {4, -1}})}}); Headers{make_range_header({{1, 2}, {4, -1}})});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_TRUE( EXPECT_TRUE(
@ -3425,7 +3436,7 @@ TEST_F(ServerTest, StaticFileRanges) {
} }
TEST_F(ServerTest, StaticFileRangeHead) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
@ -3435,7 +3446,7 @@ TEST_F(ServerTest, StaticFileRangeHead) {
} }
TEST_F(ServerTest, StaticFileRangeBigFile) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
@ -3447,7 +3458,7 @@ TEST_F(ServerTest, StaticFileRangeBigFile) {
} }
TEST_F(ServerTest, StaticFileRangeBigFile2) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
@ -3789,7 +3800,7 @@ TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
} }
TEST_F(ServerTest, GetStreamed2) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ("2", res->get_header_value("Content-Length"));
@ -3807,7 +3818,8 @@ TEST_F(ServerTest, GetStreamed) {
} }
TEST_F(ServerTest, GetStreamedWithRange1) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ("3", res->get_header_value("Content-Length"));
@ -3817,7 +3829,8 @@ TEST_F(ServerTest, GetStreamedWithRange1) {
} }
TEST_F(ServerTest, GetStreamedWithRange2) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("6", res->get_header_value("Content-Length")); EXPECT_EQ("6", res->get_header_value("Content-Length"));
@ -3827,7 +3840,7 @@ TEST_F(ServerTest, GetStreamedWithRange2) {
} }
TEST_F(ServerTest, GetStreamedWithRangeSuffix1) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ("3", res->get_header_value("Content-Length"));
@ -3837,7 +3850,8 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix1) {
} }
TEST_F(ServerTest, GetStreamedWithRangeSuffix2) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ("0", res->get_header_value("Content-Length"));
@ -3846,8 +3860,9 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix2) {
} }
TEST_F(ServerTest, GetStreamedWithRangeError) { TEST_F(ServerTest, GetStreamedWithRangeError) {
auto res = cli_.Get("/streamed-with-range", auto res =
{{"Range", "bytes=92233720368547758079223372036854775806-" cli_.Get("/streamed-with-range",
Headers{{"Range", "bytes=92233720368547758079223372036854775806-"
"92233720368547758079223372036854775807"}}); "92233720368547758079223372036854775807"}});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
@ -3859,8 +3874,9 @@ TEST_F(ServerTest, GetStreamedWithRangeError) {
TEST_F(ServerTest, GetRangeWithMaxLongLength) { TEST_F(ServerTest, GetRangeWithMaxLongLength) {
auto res = cli_.Get( auto res = cli_.Get(
"/with-range", "/with-range",
{{"Range", "bytes=0-" + std::to_string(std::numeric_limits<long>::max())}, Headers{{"Range",
{"Accept-Encoding", ""}}); "bytes=0-" + std::to_string(std::numeric_limits<long>::max())},
{"Accept-Encoding", ""}});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("7", res->get_header_value("Content-Length")); EXPECT_EQ("7", res->get_header_value("Content-Length"));
@ -3870,7 +3886,7 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) {
} }
TEST_F(ServerTest, GetRangeWithZeroToInfinite) { TEST_F(ServerTest, GetRangeWithZeroToInfinite) {
auto res = cli_.Get("/with-range", { auto res = cli_.Get("/with-range", Headers{
{"Range", "bytes=0-"}, {"Range", "bytes=0-"},
{"Accept-Encoding", ""}, {"Accept-Encoding", ""},
}); });
@ -3883,8 +3899,8 @@ TEST_F(ServerTest, GetRangeWithZeroToInfinite) {
} }
TEST_F(ServerTest, GetStreamedWithRangeMultipart) { TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
auto res = auto res = cli_.Get("/streamed-with-range",
cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}}); Headers{make_range_header({{1, 2}, {4, 5}})});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("267", res->get_header_value("Content-Length")); EXPECT_EQ("267", res->get_header_value("Content-Length"));
@ -3898,8 +3914,8 @@ TEST_F(ServerTest, GetStreamedWithTooManyRanges) {
ranges.emplace_back(0, -1); ranges.emplace_back(0, -1);
} }
auto res = auto res = cli_.Get("/streamed-with-range?error",
cli_.Get("/streamed-with-range?error", {{make_range_header(ranges)}}); Headers{make_range_header(ranges)});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ("0", res->get_header_value("Content-Length"));
@ -3909,7 +3925,7 @@ TEST_F(ServerTest, GetStreamedWithTooManyRanges) {
TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) { TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) {
auto res = cli_.Get("/streamed-with-range?error", 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ("0", res->get_header_value("Content-Length"));
@ -3918,8 +3934,9 @@ TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) {
} }
TEST_F(ServerTest, GetStreamedWithRangesMoreThanTwoOverwrapping) { TEST_F(ServerTest, GetStreamedWithRangesMoreThanTwoOverwrapping) {
auto res = cli_.Get("/streamed-with-range?error", auto res =
{{make_range_header({{0, 1}, {1, 2}, {2, 3}, {3, 4}})}}); cli_.Get("/streamed-with-range?error",
Headers{make_range_header({{0, 1}, {1, 2}, {2, 3}, {3, 4}})});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ("0", res->get_header_value("Content-Length"));
@ -3969,7 +3986,7 @@ TEST_F(ServerTest, ClientStop) {
} }
TEST_F(ServerTest, GetWithRange1) { TEST_F(ServerTest, GetWithRange1) {
auto res = cli_.Get("/with-range", { auto res = cli_.Get("/with-range", Headers{
make_range_header({{3, 5}}), make_range_header({{3, 5}}),
{"Accept-Encoding", ""}, {"Accept-Encoding", ""},
}); });
@ -3982,7 +3999,7 @@ TEST_F(ServerTest, GetWithRange1) {
} }
TEST_F(ServerTest, GetWithRange2) { TEST_F(ServerTest, GetWithRange2) {
auto res = cli_.Get("/with-range", { auto res = cli_.Get("/with-range", Headers{
make_range_header({{1, -1}}), make_range_header({{1, -1}}),
{"Accept-Encoding", ""}, {"Accept-Encoding", ""},
}); });
@ -3995,7 +4012,7 @@ TEST_F(ServerTest, GetWithRange2) {
} }
TEST_F(ServerTest, GetWithRange3) { TEST_F(ServerTest, GetWithRange3) {
auto res = cli_.Get("/with-range", { auto res = cli_.Get("/with-range", Headers{
make_range_header({{0, 0}}), make_range_header({{0, 0}}),
{"Accept-Encoding", ""}, {"Accept-Encoding", ""},
}); });
@ -4008,7 +4025,7 @@ TEST_F(ServerTest, GetWithRange3) {
} }
TEST_F(ServerTest, GetWithRange4) { TEST_F(ServerTest, GetWithRange4) {
auto res = cli_.Get("/with-range", { auto res = cli_.Get("/with-range", Headers{
make_range_header({{-1, 2}}), make_range_header({{-1, 2}}),
{"Accept-Encoding", ""}, {"Accept-Encoding", ""},
}); });
@ -4021,13 +4038,15 @@ TEST_F(ServerTest, GetWithRange4) {
} }
TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
} }
TEST_F(ServerTest, GetWithRangeMultipart) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("267", res->get_header_value("Content-Length")); EXPECT_EQ("267", res->get_header_value("Content-Length"));
@ -4036,15 +4055,15 @@ TEST_F(ServerTest, GetWithRangeMultipart) {
} }
TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
auto res = auto res = cli_.Get("/with-range",
cli_.Get("/with-range", {{make_range_header({{-1, 2}, {10000, 30000}})}}); Headers{make_range_header({{-1, 2}, {10000, 30000}})});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status);
} }
TEST_F(ServerTest, GetWithRangeCustomizedResponse) { TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
auto res = cli_.Get("/with-range-customized-response", auto res = cli_.Get("/with-range-customized-response",
{{make_range_header({{1, 2}})}}); Headers{make_range_header({{1, 2}})});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::BadRequest_400, res->status); EXPECT_EQ(StatusCode::BadRequest_400, res->status);
EXPECT_EQ(true, res->has_header("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Length"));
@ -4054,7 +4073,7 @@ TEST_F(ServerTest, GetWithRangeCustomizedResponse) {
TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) { TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) {
auto res = cli_.Get("/with-range-customized-response", 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::BadRequest_400, res->status); EXPECT_EQ(StatusCode::BadRequest_400, res->status);
EXPECT_EQ(true, res->has_header("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Length"));
@ -4063,7 +4082,7 @@ TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) {
} }
TEST_F(ServerTest, Issue1772) { 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::Unauthorized_401, res->status); EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
} }
@ -5766,7 +5785,7 @@ TEST(GetWithParametersTest, GetWithParameters) {
params.emplace("hello", "world"); params.emplace("hello", "world");
params.emplace("hello2", "world2"); params.emplace("hello2", "world2");
params.emplace("hello3", "world3"); params.emplace("hello3", "world3");
auto res = cli.Get("/", params, Headers{}); auto res = cli.Get("/", params);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@ -5823,11 +5842,10 @@ TEST(GetWithParametersTest, GetWithParameters2) {
params.emplace("hello", "world"); params.emplace("hello", "world");
std::string body; std::string body;
auto res = cli.Get("/", params, Headers{}, auto res = cli.Get("/", params, [&](const char *data, size_t data_length) {
[&](const char *data, size_t data_length) { body.append(data, data_length);
body.append(data, data_length); return true;
return true; });
});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@ -5849,7 +5867,7 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
Client cli(host); Client cli(host);
#endif #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); cli.set_connection_timeout(5);
{ {
@ -6669,7 +6687,7 @@ TEST(SendAPI, WithParamsInRequest) {
ASSERT_TRUE(res); ASSERT_TRUE(res);
} }
{ {
auto res = cli.Get("/", {{"test", "test_value"}}, Headers{}); auto res = cli.Get("/", Params{{"test", "test_value"}});
ASSERT_TRUE(res); ASSERT_TRUE(res);
} }
} }
@ -6798,8 +6816,8 @@ TEST(YahooRedirectTest3, NewResultInterface_Online) {
#ifdef CPPHTTPLIB_BROTLI_SUPPORT #ifdef CPPHTTPLIB_BROTLI_SUPPORT
TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) { TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) {
Client cli("https://cdnjs.cloudflare.com"); Client cli("https://cdnjs.cloudflare.com");
auto res = auto res = cli.Get("/ajax/libs/jquery/3.5.1/jquery.js",
cli.Get("/ajax/libs/jquery/3.5.1/jquery.js", {{"Accept-Encoding", "br"}}); Headers{{"Accept-Encoding", "br"}});
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@ -6828,7 +6846,7 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) {
params.emplace("url", "http://www.google.com"); params.emplace("url", "http://www.google.com");
params.emplace("status_code", "302"); 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@ -6840,7 +6858,7 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) {
Params params; Params params;
params.emplace("url", "http://www.google.com"); 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); ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@ -7994,7 +8012,7 @@ TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) {
svr.wait_until_ready(); svr.wait_until_ready();
Client cli(HOST, PORT); 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) { TEST(InvalidHeaderCharsTest, is_field_name) {