test_proxy.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <future>
  2. #include <gtest/gtest.h>
  3. #include <httplib.h>
  4. using namespace std;
  5. using namespace httplib;
  6. void ProxyTest(Client& cli, bool basic) {
  7. cli.set_proxy("localhost", basic ? 3128 : 3129);
  8. auto res = cli.Get("/get");
  9. ASSERT_TRUE(res != nullptr);
  10. EXPECT_EQ(407, res->status);
  11. }
  12. TEST(ProxyTest, NoSSLBasic) {
  13. Client cli("httpbin.org");
  14. ProxyTest(cli, true);
  15. }
  16. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  17. TEST(ProxyTest, SSLBasic) {
  18. SSLClient cli("httpbin.org");
  19. ProxyTest(cli, true);
  20. }
  21. TEST(ProxyTest, NoSSLDigest) {
  22. Client cli("httpbin.org");
  23. ProxyTest(cli, false);
  24. }
  25. TEST(ProxyTest, SSLDigest) {
  26. SSLClient cli("httpbin.org");
  27. ProxyTest(cli, false);
  28. }
  29. #endif
  30. // ----------------------------------------------------------------------------
  31. void RedirectTestHTTPBin(Client& cli, const char *path, bool basic) {
  32. cli.set_proxy("localhost", basic ? 3128 : 3129);
  33. if (basic) {
  34. cli.set_proxy_basic_auth("hello", "world");
  35. } else {
  36. cli.set_proxy_digest_auth("hello", "world");
  37. }
  38. cli.set_follow_location(true);
  39. auto res = cli.Get(path);
  40. ASSERT_TRUE(res != nullptr);
  41. EXPECT_EQ(200, res->status);
  42. }
  43. TEST(RedirectTest, HTTPBinNoSSLBasic) {
  44. Client cli("httpbin.org");
  45. RedirectTestHTTPBin(cli, "/redirect/2", true);
  46. }
  47. TEST(RedirectTest, HTTPBinNoSSLDigest) {
  48. Client cli("httpbin.org");
  49. RedirectTestHTTPBin(cli, "/redirect/2", false);
  50. }
  51. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  52. TEST(RedirectTest, HTTPBinSSLBasic) {
  53. SSLClient cli("httpbin.org");
  54. RedirectTestHTTPBin(cli, "/redirect/2", true);
  55. }
  56. TEST(RedirectTest, HTTPBinSSLDigest) {
  57. SSLClient cli("httpbin.org");
  58. RedirectTestHTTPBin(cli, "/redirect/2", false);
  59. }
  60. #endif
  61. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  62. TEST(RedirectTest, YouTubeNoSSLBasic) {
  63. Client cli("youtube.com");
  64. RedirectTestHTTPBin(cli, "/", true);
  65. }
  66. TEST(RedirectTest, YouTubeNoSSLDigest) {
  67. Client cli("youtube.com");
  68. RedirectTestHTTPBin(cli, "/", false);
  69. }
  70. TEST(RedirectTest, YouTubeSSLBasic) {
  71. SSLClient cli("youtube.com");
  72. RedirectTestHTTPBin(cli, "/", true);
  73. }
  74. TEST(RedirectTest, YouTubeSSLDigest) {
  75. SSLClient cli("youtube.com");
  76. RedirectTestHTTPBin(cli, "/", false);
  77. }
  78. #endif
  79. // ----------------------------------------------------------------------------
  80. void BaseAuthTestFromHTTPWatch(Client& cli) {
  81. cli.set_proxy("localhost", 3128);
  82. cli.set_proxy_basic_auth("hello", "world");
  83. {
  84. auto res = cli.Get("/basic-auth/hello/world");
  85. ASSERT_TRUE(res != nullptr);
  86. EXPECT_EQ(401, res->status);
  87. }
  88. {
  89. auto res =
  90. cli.Get("/basic-auth/hello/world",
  91. {make_basic_authentication_header("hello", "world")});
  92. ASSERT_TRUE(res != nullptr);
  93. EXPECT_EQ(res->body,
  94. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  95. EXPECT_EQ(200, res->status);
  96. }
  97. {
  98. cli.set_basic_auth("hello", "world");
  99. auto res = cli.Get("/basic-auth/hello/world");
  100. ASSERT_TRUE(res != nullptr);
  101. EXPECT_EQ(res->body,
  102. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  103. EXPECT_EQ(200, res->status);
  104. }
  105. {
  106. cli.set_basic_auth("hello", "bad");
  107. auto res = cli.Get("/basic-auth/hello/world");
  108. ASSERT_TRUE(res != nullptr);
  109. EXPECT_EQ(401, res->status);
  110. }
  111. {
  112. cli.set_basic_auth("bad", "world");
  113. auto res = cli.Get("/basic-auth/hello/world");
  114. ASSERT_TRUE(res != nullptr);
  115. EXPECT_EQ(401, res->status);
  116. }
  117. }
  118. TEST(BaseAuthTest, NoSSL) {
  119. Client cli("httpbin.org");
  120. BaseAuthTestFromHTTPWatch(cli);
  121. }
  122. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  123. TEST(BaseAuthTest, SSL) {
  124. SSLClient cli("httpbin.org");
  125. BaseAuthTestFromHTTPWatch(cli);
  126. }
  127. // ----------------------------------------------------------------------------
  128. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  129. void DigestAuthTestFromHTTPWatch(Client& cli) {
  130. cli.set_proxy("localhost", 3129);
  131. cli.set_proxy_digest_auth("hello", "world");
  132. {
  133. auto res = cli.Get("/digest-auth/auth/hello/world");
  134. ASSERT_TRUE(res != nullptr);
  135. EXPECT_EQ(401, res->status);
  136. }
  137. {
  138. std::vector<std::string> paths = {
  139. "/digest-auth/auth/hello/world/MD5",
  140. "/digest-auth/auth/hello/world/SHA-256",
  141. "/digest-auth/auth/hello/world/SHA-512",
  142. "/digest-auth/auth-init/hello/world/MD5",
  143. "/digest-auth/auth-int/hello/world/MD5",
  144. };
  145. cli.set_digest_auth("hello", "world");
  146. for (auto path : paths) {
  147. auto res = cli.Get(path.c_str());
  148. ASSERT_TRUE(res != nullptr);
  149. EXPECT_EQ(res->body,
  150. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  151. EXPECT_EQ(200, res->status);
  152. }
  153. cli.set_digest_auth("hello", "bad");
  154. for (auto path : paths) {
  155. auto res = cli.Get(path.c_str());
  156. ASSERT_TRUE(res != nullptr);
  157. EXPECT_EQ(400, res->status);
  158. }
  159. cli.set_digest_auth("bad", "world");
  160. for (auto path : paths) {
  161. auto res = cli.Get(path.c_str());
  162. ASSERT_TRUE(res != nullptr);
  163. EXPECT_EQ(400, res->status);
  164. }
  165. }
  166. }
  167. #endif
  168. TEST(DigestAuthTest, SSL) {
  169. SSLClient cli("httpbin.org");
  170. DigestAuthTestFromHTTPWatch(cli);
  171. }
  172. TEST(DigestAuthTest, NoSSL) {
  173. Client cli("httpbin.org");
  174. DigestAuthTestFromHTTPWatch(cli);
  175. }
  176. #endif