test_proxy.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  37. cli.set_proxy_digest_auth("hello", "world");
  38. #endif
  39. }
  40. cli.set_follow_location(true);
  41. auto res = cli.Get(path);
  42. ASSERT_TRUE(res != nullptr);
  43. EXPECT_EQ(200, res->status);
  44. }
  45. TEST(RedirectTest, HTTPBinNoSSLBasic) {
  46. Client cli("httpbin.org");
  47. RedirectProxyText(cli, "/redirect/2", true);
  48. }
  49. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  50. TEST(RedirectTest, HTTPBinNoSSLDigest) {
  51. Client cli("httpbin.org");
  52. RedirectProxyText(cli, "/redirect/2", false);
  53. }
  54. TEST(RedirectTest, HTTPBinSSLBasic) {
  55. SSLClient cli("httpbin.org");
  56. RedirectProxyText(cli, "/redirect/2", true);
  57. }
  58. TEST(RedirectTest, HTTPBinSSLDigest) {
  59. SSLClient cli("httpbin.org");
  60. RedirectProxyText(cli, "/redirect/2", false);
  61. }
  62. #endif
  63. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  64. TEST(RedirectTest, YouTubeNoSSLBasic) {
  65. Client cli("youtube.com");
  66. RedirectProxyText(cli, "/", true);
  67. }
  68. TEST(RedirectTest, YouTubeNoSSLDigest) {
  69. Client cli("youtube.com");
  70. RedirectProxyText(cli, "/", false);
  71. }
  72. TEST(RedirectTest, YouTubeSSLBasic) {
  73. SSLClient cli("youtube.com");
  74. RedirectProxyText(cli, "/", true);
  75. }
  76. TEST(RedirectTest, YouTubeSSLDigest) {
  77. SSLClient cli("youtube.com");
  78. RedirectProxyText(cli, "/", false);
  79. }
  80. #endif
  81. // ----------------------------------------------------------------------------
  82. void BaseAuthTestFromHTTPWatch(Client& cli) {
  83. cli.set_proxy("localhost", 3128);
  84. cli.set_proxy_basic_auth("hello", "world");
  85. {
  86. auto res = cli.Get("/basic-auth/hello/world");
  87. ASSERT_TRUE(res != nullptr);
  88. EXPECT_EQ(401, res->status);
  89. }
  90. {
  91. auto res =
  92. cli.Get("/basic-auth/hello/world",
  93. {make_basic_authentication_header("hello", "world")});
  94. ASSERT_TRUE(res != nullptr);
  95. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
  96. EXPECT_EQ(200, res->status);
  97. }
  98. {
  99. cli.set_basic_auth("hello", "world");
  100. auto res = cli.Get("/basic-auth/hello/world");
  101. ASSERT_TRUE(res != nullptr);
  102. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
  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. #endif
  128. // ----------------------------------------------------------------------------
  129. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  130. void DigestAuthTestFromHTTPWatch(Client& cli) {
  131. cli.set_proxy("localhost", 3129);
  132. cli.set_proxy_digest_auth("hello", "world");
  133. {
  134. auto res = cli.Get("/digest-auth/auth/hello/world");
  135. ASSERT_TRUE(res != nullptr);
  136. EXPECT_EQ(401, res->status);
  137. }
  138. {
  139. std::vector<std::string> paths = {
  140. "/digest-auth/auth/hello/world/MD5",
  141. "/digest-auth/auth/hello/world/SHA-256",
  142. "/digest-auth/auth/hello/world/SHA-512",
  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("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body);
  150. EXPECT_EQ(200, res->status);
  151. }
  152. cli.set_digest_auth("hello", "bad");
  153. for (auto path : paths) {
  154. auto res = cli.Get(path.c_str());
  155. ASSERT_TRUE(res != nullptr);
  156. EXPECT_EQ(401, res->status);
  157. }
  158. // NOTE: Until httpbin.org fixes issue #46, the following test is commented
  159. // out. Plese see https://httpbin.org/digest-auth/auth/hello/world
  160. // cli.set_digest_auth("bad", "world");
  161. // for (auto path : paths) {
  162. // auto res = cli.Get(path.c_str());
  163. // ASSERT_TRUE(res != nullptr);
  164. // EXPECT_EQ(401, res->status);
  165. // }
  166. }
  167. }
  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
  177. // ----------------------------------------------------------------------------
  178. void KeepAliveTest(Client& cli, bool basic) {
  179. cli.set_proxy("localhost", basic ? 3128 : 3129);
  180. if (basic) {
  181. cli.set_proxy_basic_auth("hello", "world");
  182. } else {
  183. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  184. cli.set_proxy_digest_auth("hello", "world");
  185. #endif
  186. }
  187. cli.set_keep_alive_max_count(4);
  188. cli.set_follow_location(true);
  189. cli.set_digest_auth("hello", "world");
  190. std::vector<Request> requests;
  191. Get(requests, "/get");
  192. Get(requests, "/redirect/2");
  193. std::vector<std::string> paths = {
  194. "/digest-auth/auth/hello/world/MD5",
  195. "/digest-auth/auth/hello/world/SHA-256",
  196. "/digest-auth/auth/hello/world/SHA-512",
  197. "/digest-auth/auth-int/hello/world/MD5",
  198. };
  199. for (auto path : paths) {
  200. Get(requests, path.c_str());
  201. }
  202. {
  203. int count = 100;
  204. while (count--) {
  205. Get(requests, "/get");
  206. }
  207. }
  208. std::vector<Response> responses;
  209. auto ret = cli.send(requests, responses);
  210. ASSERT_TRUE(ret == true);
  211. ASSERT_TRUE(requests.size() == responses.size());
  212. size_t i = 0;
  213. {
  214. auto &res = responses[i++];
  215. EXPECT_EQ(200, res.status);
  216. }
  217. {
  218. auto &res = responses[i++];
  219. EXPECT_EQ(200, res.status);
  220. }
  221. {
  222. int count = static_cast<int>(paths.size());
  223. while (count--) {
  224. auto &res = responses[i++];
  225. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res.body);
  226. EXPECT_EQ(200, res.status);
  227. }
  228. }
  229. for (; i < responses.size(); i++) {
  230. auto &res = responses[i];
  231. EXPECT_EQ(200, res.status);
  232. }
  233. }
  234. TEST(KeepAliveTest, NoSSLWithBasic) {
  235. Client cli("httpbin.org");
  236. KeepAliveTest(cli, true);
  237. }
  238. TEST(KeepAliveTest, SSLWithBasic) {
  239. SSLClient cli("httpbin.org");
  240. KeepAliveTest(cli, true);
  241. }
  242. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  243. TEST(KeepAliveTest, NoSSLWithDigest) {
  244. Client cli("httpbin.org");
  245. KeepAliveTest(cli, false);
  246. }
  247. TEST(KeepAliveTest, SSLWithDigest) {
  248. SSLClient cli("httpbin.org");
  249. KeepAliveTest(cli, false);
  250. }
  251. #endif