Skip to content

Commit

Permalink
Add CPPHTTPLIB_DEFAULT_HTTPBIN macro to choose the default httpbin.org
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-park committed Jun 15, 2023
1 parent 320d305 commit e083511
Showing 1 changed file with 84 additions and 32 deletions.
116 changes: 84 additions & 32 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,13 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
}

TEST(RangeTest, FromHTTPBin_Online) {
#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
auto port = 443;
Expand Down Expand Up @@ -674,7 +679,13 @@ TEST(ConnectionErrorTest, Timeout_Online) {
}

TEST(CancelTest, NoCancel_Online) {
#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
auto port = 443;
Expand All @@ -685,15 +696,20 @@ TEST(CancelTest, NoCancel_Online) {
#endif
cli.set_connection_timeout(std::chrono::seconds(5));

auto res =
cli.Get("/httpbin/range/32", [](uint64_t, uint64_t) { return true; });
auto res = cli.Get(path, [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res);
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(200, res->status);
}

TEST(CancelTest, WithCancelSmallPayload_Online) {
#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
auto port = 443;
Expand All @@ -703,15 +719,20 @@ TEST(CancelTest, WithCancelSmallPayload_Online) {
Client cli(host, port);
#endif

auto res =
cli.Get("/httpbin/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));
ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error());
}

TEST(CancelTest, WithCancelLargePayload_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
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
auto port = 443;
Expand All @@ -723,15 +744,20 @@ TEST(CancelTest, WithCancelLargePayload_Online) {
cli.set_connection_timeout(std::chrono::seconds(5));

uint32_t count = 0;
auto res = cli.Get("/httpbin/range/65536",
[&count](uint64_t, uint64_t) { return (count++ == 0); });
auto res =
cli.Get(path, [&count](uint64_t, uint64_t) { return (count++ == 0); });
ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error());
}

TEST(BaseAuthTest, FromHTTPWatch_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
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
auto port = 443;
Expand Down Expand Up @@ -782,26 +808,39 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(DigestAuthTest, FromHTTPWatch_Online) {
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
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;
SSLClient cli(host, port);

{
auto res = cli.Get("/httpbin/digest-auth/auth/hello/world");
auto res = cli.Get(unauth_path);
ASSERT_TRUE(res);
EXPECT_EQ(401, res->status);
}

{
std::vector<std::string> paths = {
"/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",
};

cli.set_digest_auth("hello", "world");
for (auto path : paths) {
for (const auto &path : paths) {
auto res = cli.Get(path.c_str());
ASSERT_TRUE(res);
EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
Expand All @@ -810,7 +849,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
}

cli.set_digest_auth("hello", "bad");
for (auto path : paths) {
for (const auto &path : paths) {
auto res = cli.Get(path.c_str());
ASSERT_TRUE(res);
EXPECT_EQ(401, res->status);
Expand All @@ -819,7 +858,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
// NOTE: Until httpbin.org fixes issue #46, the following test is commented
// out. Please see https://httpbin.org/digest-auth/auth/hello/world
// cli.set_digest_auth("bad", "world");
// for (auto path : paths) {
// for (const auto& path : paths) {
// auto res = cli.Get(path.c_str());
// ASSERT_TRUE(res);
// EXPECT_EQ(400, res->status);
Expand Down Expand Up @@ -3923,16 +3962,16 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) {

svr.Get("/events", [](const Request & /*req*/, Response &res) {
res.set_header("Cache-Control", "no-cache");
res.set_chunked_content_provider("text/event-stream", [](size_t offset,
DataSink &sink) {
std::string s = "data:";
s += std::to_string(offset);
s += "\n\n";
auto ret = sink.write(s.data(), s.size());
EXPECT_TRUE(ret);
std::this_thread::sleep_for(std::chrono::seconds(1));
return true;
});
res.set_chunked_content_provider(
"text/event-stream", [](size_t offset, DataSink &sink) {
std::string s = "data:";
s += std::to_string(offset);
s += "\n\n";
auto ret = sink.write(s.data(), s.size());
EXPECT_TRUE(ret);
std::this_thread::sleep_for(std::chrono::seconds(1));
return true;
});
});

auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
Expand Down Expand Up @@ -4418,8 +4457,13 @@ TEST(GetWithParametersTest, GetWithParameters2) {
}

TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
#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);
Expand Down Expand Up @@ -4664,8 +4708,16 @@ TEST(SSLClientTest, UpdateCAStore) {
}

TEST(SSLClientTest, ServerNameIndication_Online) {
SSLClient cli("nghttp2.org", 443);
auto res = cli.Get("/httpbin/get");
#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN
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_EQ(200, res->status);
}
Expand Down Expand Up @@ -6176,19 +6228,19 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) {
TEST(VulnerabilityTest, CRLFInjection) {
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");
});

svr.Delete("/test2", [](const Request &/*req*/, Response &res) {
svr.Delete("/test2", [](const Request & /*req*/, Response &res) {
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");
});

svr.Patch("/test4", [](const Request &/*req*/, Response &res) {
svr.Patch("/test4", [](const Request & /*req*/, Response &res) {
res.set_content("Hello 4", "text/plain");
});

Expand Down

0 comments on commit e083511

Please sign in to comment.