Use nghttp2-hosted httpbin.org (#1586)

* Use nghttp2-hosted httpbin.org

* Add CPPHTTPLIB_DEFAULT_HTTPBIN macro to choose the default httpbin.org
This commit is contained in:
Jiwoo Park 2023-06-16 00:12:41 +09:00 committed by GitHub
parent d3076f5a70
commit 067890133c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -526,7 +526,13 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
} }
TEST(RangeTest, FromHTTPBin_Online) { TEST(RangeTest, FromHTTPBin_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org"; auto host = "httpbin.org";
auto path = std::string{"/range/32"};
#else
auto host = "nghttp2.org";
auto path = std::string{"/httpbin/range/32"};
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
@ -538,7 +544,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
cli.set_connection_timeout(5); cli.set_connection_timeout(5);
{ {
auto res = cli.Get("/range/32"); auto res = cli.Get(path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -546,7 +552,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{1, -1}})}; Headers headers = {make_range_header({{1, -1}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(206, res->status); EXPECT_EQ(206, res->status);
@ -554,7 +560,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{1, 10}})}; Headers headers = {make_range_header({{1, 10}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ("bcdefghijk", res->body);
EXPECT_EQ(206, res->status); EXPECT_EQ(206, res->status);
@ -562,7 +568,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{0, 31}})}; Headers headers = {make_range_header({{0, 31}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -570,7 +576,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{0, -1}})}; Headers headers = {make_range_header({{0, -1}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
@ -578,7 +584,7 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{0, 32}})}; Headers headers = {make_range_header({{0, 32}})};
auto res = cli.Get("/range/32", headers); auto res = cli.Get(path, headers);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(416, res->status); EXPECT_EQ(416, res->status);
} }
@ -673,7 +679,13 @@ TEST(ConnectionErrorTest, Timeout_Online) {
} }
TEST(CancelTest, NoCancel_Online) { TEST(CancelTest, NoCancel_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org"; auto host = "httpbin.org";
auto path = std::string{"/range/32"};
#else
auto host = "nghttp2.org";
auto path = std::string{"/httpbin/range/32"};
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
@ -684,14 +696,20 @@ TEST(CancelTest, NoCancel_Online) {
#endif #endif
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; }); auto res = cli.Get(path, [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
TEST(CancelTest, WithCancelSmallPayload_Online) { TEST(CancelTest, WithCancelSmallPayload_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org"; auto host = "httpbin.org";
auto path = std::string{"/range/32"};
#else
auto host = "nghttp2.org";
auto path = std::string{"/httpbin/range/32"};
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
@ -701,14 +719,20 @@ TEST(CancelTest, WithCancelSmallPayload_Online) {
Client cli(host, port); Client cli(host, port);
#endif #endif
auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; }); auto res = cli.Get(path, [](uint64_t, uint64_t) { return false; });
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
TEST(CancelTest, WithCancelLargePayload_Online) { TEST(CancelTest, WithCancelLargePayload_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org"; auto host = "httpbin.org";
auto path = std::string{"/range/65536"};
#else
auto host = "nghttp2.org";
auto path = std::string{"/httpbin/range/65536"};
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
@ -720,14 +744,20 @@ TEST(CancelTest, WithCancelLargePayload_Online) {
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
uint32_t count = 0; uint32_t count = 0;
auto res = cli.Get("/range/65536", auto res =
[&count](uint64_t, uint64_t) { return (count++ == 0); }); cli.Get(path, [&count](uint64_t, uint64_t) { return (count++ == 0); });
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
TEST(BaseAuthTest, FromHTTPWatch_Online) { TEST(BaseAuthTest, FromHTTPWatch_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org"; auto host = "httpbin.org";
auto path = std::string{"/basic-auth/hello/world"};
#else
auto host = "nghttp2.org";
auto path = std::string{"/httpbin/basic-auth/hello/world"};
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
@ -738,14 +768,14 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
#endif #endif
{ {
auto res = cli.Get("/basic-auth/hello/world"); auto res = cli.Get(path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(401, res->status); EXPECT_EQ(401, res->status);
} }
{ {
auto res = cli.Get("/basic-auth/hello/world", auto res =
{make_basic_authentication_header("hello", "world")}); cli.Get(path, {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);
@ -754,7 +784,7 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
{ {
cli.set_basic_auth("hello", "world"); cli.set_basic_auth("hello", "world");
auto res = cli.Get("/basic-auth/hello/world"); auto res = cli.Get(path);
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);
@ -763,14 +793,14 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
{ {
cli.set_basic_auth("hello", "bad"); cli.set_basic_auth("hello", "bad");
auto res = cli.Get("/basic-auth/hello/world"); auto res = cli.Get(path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(401, res->status); EXPECT_EQ(401, res->status);
} }
{ {
cli.set_basic_auth("bad", "world"); cli.set_basic_auth("bad", "world");
auto res = cli.Get("/basic-auth/hello/world"); auto res = cli.Get(path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(401, res->status); EXPECT_EQ(401, res->status);
} }
@ -778,26 +808,39 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(DigestAuthTest, FromHTTPWatch_Online) { TEST(DigestAuthTest, FromHTTPWatch_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org"; auto host = "httpbin.org";
auto unauth_path = std::string{"/digest-auth/auth/hello/world"};
auto paths = std::vector<std::string>{
"/digest-auth/auth/hello/world/MD5",
"/digest-auth/auth/hello/world/SHA-256",
"/digest-auth/auth/hello/world/SHA-512",
"/digest-auth/auth-int/hello/world/MD5",
};
#else
auto host = "nghttp2.org";
auto unauth_path = std::string{"/httpbin/digest-auth/auth/hello/world"};
auto paths = std::vector<std::string>{
"/httpbin/digest-auth/auth/hello/world/MD5",
"/httpbin/digest-auth/auth/hello/world/SHA-256",
"/httpbin/digest-auth/auth/hello/world/SHA-512",
"/httpbin/digest-auth/auth-int/hello/world/MD5",
};
#endif
auto port = 443; auto port = 443;
SSLClient cli(host, port); SSLClient cli(host, port);
{ {
auto res = cli.Get("/digest-auth/auth/hello/world"); auto res = cli.Get(unauth_path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(401, res->status); EXPECT_EQ(401, res->status);
} }
{ {
std::vector<std::string> paths = {
"/digest-auth/auth/hello/world/MD5",
"/digest-auth/auth/hello/world/SHA-256",
"/digest-auth/auth/hello/world/SHA-512",
"/digest-auth/auth-int/hello/world/MD5",
};
cli.set_digest_auth("hello", "world"); cli.set_digest_auth("hello", "world");
for (auto path : paths) { for (const auto &path : paths) {
auto res = cli.Get(path.c_str()); auto res = cli.Get(path.c_str());
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",
@ -806,7 +849,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
} }
cli.set_digest_auth("hello", "bad"); cli.set_digest_auth("hello", "bad");
for (auto path : paths) { for (const auto &path : paths) {
auto res = cli.Get(path.c_str()); auto res = cli.Get(path.c_str());
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ(401, res->status); EXPECT_EQ(401, res->status);
@ -815,7 +858,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
// NOTE: Until httpbin.org fixes issue #46, the following test is commented // NOTE: Until httpbin.org fixes issue #46, the following test is commented
// out. Please see https://httpbin.org/digest-auth/auth/hello/world // out. Please see https://httpbin.org/digest-auth/auth/hello/world
// cli.set_digest_auth("bad", "world"); // cli.set_digest_auth("bad", "world");
// for (auto path : paths) { // for (const auto& path : paths) {
// auto res = cli.Get(path.c_str()); // auto res = cli.Get(path.c_str());
// ASSERT_TRUE(res); // ASSERT_TRUE(res);
// EXPECT_EQ(400, res->status); // EXPECT_EQ(400, res->status);
@ -3919,16 +3962,16 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) {
svr.Get("/events", [](const Request & /*req*/, Response &res) { svr.Get("/events", [](const Request & /*req*/, Response &res) {
res.set_header("Cache-Control", "no-cache"); res.set_header("Cache-Control", "no-cache");
res.set_chunked_content_provider("text/event-stream", [](size_t offset, res.set_chunked_content_provider(
DataSink &sink) { "text/event-stream", [](size_t offset, DataSink &sink) {
std::string s = "data:"; std::string s = "data:";
s += std::to_string(offset); s += std::to_string(offset);
s += "\n\n"; s += "\n\n";
auto ret = sink.write(s.data(), s.size()); auto ret = sink.write(s.data(), s.size());
EXPECT_TRUE(ret); EXPECT_TRUE(ret);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
return true; return true;
}); });
}); });
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
@ -4414,19 +4457,32 @@ TEST(GetWithParametersTest, GetWithParameters2) {
} }
TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) { TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
Client cli("httpbin.org"); #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto host = "httpbin.org";
auto path = std::string{"/range/32"};
#else
auto host = "nghttp2.org";
auto path = std::string{"/httpbin/range/32"};
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLClient cli(host);
#else
Client cli(host);
#endif
cli.set_default_headers({make_range_header({{1, 10}})}); cli.set_default_headers({make_range_header({{1, 10}})});
cli.set_connection_timeout(5); cli.set_connection_timeout(5);
{ {
auto res = cli.Get("/range/32"); auto res = cli.Get(path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ("bcdefghijk", res->body);
EXPECT_EQ(206, res->status); EXPECT_EQ(206, res->status);
} }
{ {
auto res = cli.Get("/range/32"); auto res = cli.Get(path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ("bcdefghijk", res->body);
EXPECT_EQ(206, res->status); EXPECT_EQ(206, res->status);
@ -4652,8 +4708,16 @@ TEST(SSLClientTest, UpdateCAStore) {
} }
TEST(SSLClientTest, ServerNameIndication_Online) { TEST(SSLClientTest, ServerNameIndication_Online) {
SSLClient cli("httpbin.org", 443); #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
auto res = cli.Get("/get"); auto host = "httpbin.org";
auto path = std::string{"/get"};
#else
auto host = "nghttp2.org";
auto path = std::string{"/httpbin/get"};
#endif
SSLClient cli(host, 443);
auto res = cli.Get(path);
ASSERT_TRUE(res); ASSERT_TRUE(res);
ASSERT_EQ(200, res->status); ASSERT_EQ(200, res->status);
} }
@ -6164,19 +6228,19 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) {
TEST(VulnerabilityTest, CRLFInjection) { TEST(VulnerabilityTest, CRLFInjection) {
Server svr; Server svr;
svr.Post("/test1", [](const Request &/*req*/, Response &res) { svr.Post("/test1", [](const Request & /*req*/, Response &res) {
res.set_content("Hello 1", "text/plain"); res.set_content("Hello 1", "text/plain");
}); });
svr.Delete("/test2", [](const Request &/*req*/, Response &res) { svr.Delete("/test2", [](const Request & /*req*/, Response &res) {
res.set_content("Hello 2", "text/plain"); res.set_content("Hello 2", "text/plain");
}); });
svr.Put("/test3", [](const Request &/*req*/, Response &res) { svr.Put("/test3", [](const Request & /*req*/, Response &res) {
res.set_content("Hello 3", "text/plain"); res.set_content("Hello 3", "text/plain");
}); });
svr.Patch("/test4", [](const Request &/*req*/, Response &res) { svr.Patch("/test4", [](const Request & /*req*/, Response &res) {
res.set_content("Hello 4", "text/plain"); res.set_content("Hello 4", "text/plain");
}); });