test_proxy.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 RedirectProxyText(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. RedirectProxyText(cli, "/redirect/2", true);
  46. }
  47. TEST(RedirectTest, HTTPBinNoSSLDigest) {
  48. Client cli("httpbin.org");
  49. RedirectProxyText(cli, "/redirect/2", false);
  50. }
  51. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  52. TEST(RedirectTest, HTTPBinSSLBasic) {
  53. SSLClient cli("httpbin.org");
  54. RedirectProxyText(cli, "/redirect/2", true);
  55. }
  56. TEST(RedirectTest, HTTPBinSSLDigest) {
  57. SSLClient cli("httpbin.org");
  58. RedirectProxyText(cli, "/redirect/2", false);
  59. }
  60. #endif
  61. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  62. TEST(RedirectTest, YouTubeNoSSLBasic) {
  63. Client cli("youtube.com");
  64. RedirectProxyText(cli, "/", true);
  65. }
  66. TEST(RedirectTest, YouTubeNoSSLDigest) {
  67. Client cli("youtube.com");
  68. RedirectProxyText(cli, "/", false);
  69. }
  70. TEST(RedirectTest, YouTubeSSLBasic) {
  71. SSLClient cli("youtube.com");
  72. RedirectProxyText(cli, "/", true);
  73. }
  74. TEST(RedirectTest, YouTubeSSLDigest) {
  75. SSLClient cli("youtube.com");
  76. RedirectProxyText(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("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
  94. EXPECT_EQ(200, res->status);
  95. }
  96. {
  97. cli.set_basic_auth("hello", "world");
  98. auto res = cli.Get("/basic-auth/hello/world");
  99. ASSERT_TRUE(res != nullptr);
  100. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
  101. EXPECT_EQ(200, res->status);
  102. }
  103. {
  104. cli.set_basic_auth("hello", "bad");
  105. auto res = cli.Get("/basic-auth/hello/world");
  106. ASSERT_TRUE(res != nullptr);
  107. EXPECT_EQ(401, res->status);
  108. }
  109. {
  110. cli.set_basic_auth("bad", "world");
  111. auto res = cli.Get("/basic-auth/hello/world");
  112. ASSERT_TRUE(res != nullptr);
  113. EXPECT_EQ(401, res->status);
  114. }
  115. }
  116. TEST(BaseAuthTest, NoSSL) {
  117. Client cli("httpbin.org");
  118. BaseAuthTestFromHTTPWatch(cli);
  119. }
  120. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  121. TEST(BaseAuthTest, SSL) {
  122. SSLClient cli("httpbin.org");
  123. BaseAuthTestFromHTTPWatch(cli);
  124. }
  125. #endif
  126. // ----------------------------------------------------------------------------
  127. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  128. void DigestAuthTestFromHTTPWatch(Client& cli) {
  129. cli.set_proxy("localhost", 3129);
  130. cli.set_proxy_digest_auth("hello", "world");
  131. {
  132. auto res = cli.Get("/digest-auth/auth/hello/world");
  133. ASSERT_TRUE(res != nullptr);
  134. EXPECT_EQ(401, res->status);
  135. }
  136. {
  137. std::vector<std::string> paths = {
  138. "/digest-auth/auth/hello/world/MD5",
  139. "/digest-auth/auth/hello/world/SHA-256",
  140. "/digest-auth/auth/hello/world/SHA-512",
  141. "/digest-auth/auth-int/hello/world/MD5",
  142. };
  143. cli.set_digest_auth("hello", "world");
  144. for (auto path : paths) {
  145. auto res = cli.Get(path.c_str());
  146. ASSERT_TRUE(res != nullptr);
  147. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
  148. EXPECT_EQ(200, res->status);
  149. }
  150. cli.set_digest_auth("hello", "bad");
  151. for (auto path : paths) {
  152. auto res = cli.Get(path.c_str());
  153. ASSERT_TRUE(res != nullptr);
  154. EXPECT_EQ(400, res->status);
  155. }
  156. cli.set_digest_auth("bad", "world");
  157. for (auto path : paths) {
  158. auto res = cli.Get(path.c_str());
  159. ASSERT_TRUE(res != nullptr);
  160. EXPECT_EQ(400, res->status);
  161. }
  162. }
  163. }
  164. TEST(DigestAuthTest, SSL) {
  165. SSLClient cli("httpbin.org");
  166. DigestAuthTestFromHTTPWatch(cli);
  167. }
  168. TEST(DigestAuthTest, NoSSL) {
  169. Client cli("httpbin.org");
  170. DigestAuthTestFromHTTPWatch(cli);
  171. }
  172. #endif
  173. // ----------------------------------------------------------------------------
  174. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  175. TEST(KeepAliveText, NoSSLWithDigest) {
  176. Client cli("httpbin.org");
  177. cli.set_keep_alive_max_count(4);
  178. cli.set_follow_location(true);
  179. cli.set_digest_auth("hello", "world");
  180. cli.set_proxy("localhost", 3129);
  181. cli.set_proxy_digest_auth("hello", "world");
  182. std::vector<Request> requests;
  183. Get(requests, "/get");
  184. Get(requests, "/redirect/2");
  185. Get(requests, "/digest-auth/auth/hello/world/MD5");
  186. std::vector<Response> responses;
  187. auto ret = cli.send(requests, responses);
  188. ASSERT_TRUE(ret == true);
  189. ASSERT_TRUE(requests.size() == responses.size());
  190. auto i = 0;
  191. {
  192. auto &res = responses[i++];
  193. EXPECT_EQ(200, res.status);
  194. }
  195. {
  196. auto &res = responses[i++];
  197. EXPECT_EQ(200, res.status);
  198. }
  199. {
  200. auto &res = responses[i++];
  201. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res.body);
  202. EXPECT_EQ(200, res.status);
  203. }
  204. }
  205. TEST(KeepAliveText, SSLWithDigest) {
  206. SSLClient cli("httpbin.org");
  207. cli.set_keep_alive_max_count(4);
  208. cli.set_follow_location(true);
  209. cli.set_digest_auth("hello", "world");
  210. cli.set_proxy("localhost", 3129);
  211. cli.set_proxy_digest_auth("hello", "world");
  212. std::vector<Request> requests;
  213. Get(requests, "/get");
  214. Get(requests, "/redirect/2");
  215. Get(requests, "/digest-auth/auth/hello/world/MD5");
  216. std::vector<Response> responses;
  217. auto ret = cli.send(requests, responses);
  218. ASSERT_TRUE(ret == true);
  219. ASSERT_TRUE(requests.size() == responses.size());
  220. auto i = 0;
  221. {
  222. auto &res = responses[i++];
  223. EXPECT_EQ(200, res.status);
  224. }
  225. {
  226. auto &res = responses[i++];
  227. EXPECT_EQ(200, res.status);
  228. }
  229. {
  230. auto &res = responses[i++];
  231. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res.body);
  232. EXPECT_EQ(200, res.status);
  233. }
  234. }
  235. #endif