123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- #include <future>
- #include <gtest/gtest.h>
- #include <httplib.h>
- using namespace std;
- using namespace httplib;
- void ProxyTest(Client& cli, bool basic) {
- cli.set_proxy("localhost", basic ? 3128 : 3129);
- auto res = cli.Get("/get");
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ(407, res->status);
- }
- TEST(ProxyTest, NoSSLBasic) {
- Client cli("httpbin.org");
- ProxyTest(cli, true);
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(ProxyTest, SSLBasic) {
- SSLClient cli("httpbin.org");
- ProxyTest(cli, true);
- }
- TEST(ProxyTest, NoSSLDigest) {
- Client cli("httpbin.org");
- ProxyTest(cli, false);
- }
- TEST(ProxyTest, SSLDigest) {
- SSLClient cli("httpbin.org");
- ProxyTest(cli, false);
- }
- #endif
- // ----------------------------------------------------------------------------
- void RedirectProxyText(Client& cli, const char *path, bool basic) {
- cli.set_proxy("localhost", basic ? 3128 : 3129);
- if (basic) {
- cli.set_proxy_basic_auth("hello", "world");
- } else {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- cli.set_proxy_digest_auth("hello", "world");
- #endif
- }
- cli.set_follow_location(true);
- auto res = cli.Get(path);
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ(200, res->status);
- }
- TEST(RedirectTest, HTTPBinNoSSLBasic) {
- Client cli("httpbin.org");
- RedirectProxyText(cli, "/redirect/2", true);
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(RedirectTest, HTTPBinNoSSLDigest) {
- Client cli("httpbin.org");
- RedirectProxyText(cli, "/redirect/2", false);
- }
- TEST(RedirectTest, HTTPBinSSLBasic) {
- SSLClient cli("httpbin.org");
- RedirectProxyText(cli, "/redirect/2", true);
- }
- TEST(RedirectTest, HTTPBinSSLDigest) {
- SSLClient cli("httpbin.org");
- RedirectProxyText(cli, "/redirect/2", false);
- }
- #endif
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(RedirectTest, YouTubeNoSSLBasic) {
- Client cli("youtube.com");
- RedirectProxyText(cli, "/", true);
- }
- TEST(RedirectTest, YouTubeNoSSLDigest) {
- Client cli("youtube.com");
- RedirectProxyText(cli, "/", false);
- }
- TEST(RedirectTest, YouTubeSSLBasic) {
- SSLClient cli("youtube.com");
- RedirectProxyText(cli, "/", true);
- }
- TEST(RedirectTest, YouTubeSSLDigest) {
- SSLClient cli("youtube.com");
- RedirectProxyText(cli, "/", false);
- }
- #endif
- // ----------------------------------------------------------------------------
- void BaseAuthTestFromHTTPWatch(Client& cli) {
- cli.set_proxy("localhost", 3128);
- cli.set_proxy_basic_auth("hello", "world");
- {
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ(401, res->status);
- }
- {
- auto res =
- cli.Get("/basic-auth/hello/world",
- {make_basic_authentication_header("hello", "world")});
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
- EXPECT_EQ(200, res->status);
- }
- {
- cli.set_basic_auth("hello", "world");
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
- EXPECT_EQ(200, res->status);
- }
- {
- cli.set_basic_auth("hello", "bad");
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ(401, res->status);
- }
- {
- cli.set_basic_auth("bad", "world");
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ(401, res->status);
- }
- }
- TEST(BaseAuthTest, NoSSL) {
- Client cli("httpbin.org");
- BaseAuthTestFromHTTPWatch(cli);
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(BaseAuthTest, SSL) {
- SSLClient cli("httpbin.org");
- BaseAuthTestFromHTTPWatch(cli);
- }
- #endif
- // ----------------------------------------------------------------------------
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- void DigestAuthTestFromHTTPWatch(Client& cli) {
- cli.set_proxy("localhost", 3129);
- cli.set_proxy_digest_auth("hello", "world");
- {
- auto res = cli.Get("/digest-auth/auth/hello/world");
- ASSERT_TRUE(res != nullptr);
- 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");
- for (auto path : paths) {
- auto res = cli.Get(path.c_str());
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
- EXPECT_EQ(200, res->status);
- }
- cli.set_digest_auth("hello", "bad");
- for (auto path : paths) {
- auto res = cli.Get(path.c_str());
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ(401, res->status);
- }
- // NOTE: Until httpbin.org fixes issue #46, the following test is commented
- // out. Plese see https://httpbin.org/digest-auth/auth/hello/world
- // cli.set_digest_auth("bad", "world");
- // for (auto path : paths) {
- // auto res = cli.Get(path.c_str());
- // ASSERT_TRUE(res != nullptr);
- // EXPECT_EQ(401, res->status);
- // }
- }
- }
- TEST(DigestAuthTest, SSL) {
- SSLClient cli("httpbin.org");
- DigestAuthTestFromHTTPWatch(cli);
- }
- TEST(DigestAuthTest, NoSSL) {
- Client cli("httpbin.org");
- DigestAuthTestFromHTTPWatch(cli);
- }
- #endif
- // ----------------------------------------------------------------------------
- void KeepAliveTest(Client& cli, bool basic) {
- cli.set_proxy("localhost", basic ? 3128 : 3129);
- if (basic) {
- cli.set_proxy_basic_auth("hello", "world");
- } else {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- cli.set_proxy_digest_auth("hello", "world");
- #endif
- }
- cli.set_keep_alive_max_count(4);
- cli.set_follow_location(true);
- cli.set_digest_auth("hello", "world");
- std::vector<Request> requests;
- Get(requests, "/get");
- Get(requests, "/redirect/2");
- 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",
- };
- for (auto path : paths) {
- Get(requests, path.c_str());
- }
- {
- int count = 100;
- while (count--) {
- Get(requests, "/get");
- }
- }
- std::vector<Response> responses;
- auto ret = cli.send(requests, responses);
- ASSERT_TRUE(ret == true);
- ASSERT_TRUE(requests.size() == responses.size());
- size_t i = 0;
- {
- auto &res = responses[i++];
- EXPECT_EQ(200, res.status);
- }
- {
- auto &res = responses[i++];
- EXPECT_EQ(200, res.status);
- }
- {
- int count = static_cast<int>(paths.size());
- while (count--) {
- auto &res = responses[i++];
- EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res.body);
- EXPECT_EQ(200, res.status);
- }
- }
- for (; i < responses.size(); i++) {
- auto &res = responses[i];
- EXPECT_EQ(200, res.status);
- }
- }
- TEST(KeepAliveTest, NoSSLWithBasic) {
- Client cli("httpbin.org");
- KeepAliveTest(cli, true);
- }
- TEST(KeepAliveTest, SSLWithBasic) {
- SSLClient cli("httpbin.org");
- KeepAliveTest(cli, true);
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(KeepAliveTest, NoSSLWithDigest) {
- Client cli("httpbin.org");
- KeepAliveTest(cli, false);
- }
- TEST(KeepAliveTest, SSLWithDigest) {
- SSLClient cli("httpbin.org");
- KeepAliveTest(cli, false);
- }
- #endif
|