test_proxy.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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(400, res->status);
  157. }
  158. cli.set_digest_auth("bad", "world");
  159. for (auto path : paths) {
  160. auto res = cli.Get(path.c_str());
  161. ASSERT_TRUE(res != nullptr);
  162. EXPECT_EQ(400, res->status);
  163. }
  164. }
  165. }
  166. TEST(DigestAuthTest, SSL) {
  167. SSLClient cli("httpbin.org");
  168. DigestAuthTestFromHTTPWatch(cli);
  169. }
  170. TEST(DigestAuthTest, NoSSL) {
  171. Client cli("httpbin.org");
  172. DigestAuthTestFromHTTPWatch(cli);
  173. }
  174. #endif
  175. // ----------------------------------------------------------------------------
  176. void KeepAliveTest(Client& cli, bool basic) {
  177. cli.set_proxy("localhost", basic ? 3128 : 3129);
  178. if (basic) {
  179. cli.set_proxy_basic_auth("hello", "world");
  180. } else {
  181. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  182. cli.set_proxy_digest_auth("hello", "world");
  183. #endif
  184. }
  185. cli.set_keep_alive_max_count(4);
  186. cli.set_follow_location(true);
  187. cli.set_digest_auth("hello", "world");
  188. std::vector<Request> requests;
  189. Get(requests, "/get");
  190. Get(requests, "/redirect/2");
  191. std::vector<std::string> paths = {
  192. "/digest-auth/auth/hello/world/MD5",
  193. "/digest-auth/auth/hello/world/SHA-256",
  194. "/digest-auth/auth/hello/world/SHA-512",
  195. "/digest-auth/auth-int/hello/world/MD5",
  196. };
  197. for (auto path : paths) {
  198. Get(requests, path.c_str());
  199. }
  200. {
  201. int count = 100;
  202. while (count--) {
  203. Get(requests, "/get");
  204. }
  205. }
  206. std::vector<Response> responses;
  207. auto ret = cli.send(requests, responses);
  208. ASSERT_TRUE(ret == true);
  209. ASSERT_TRUE(requests.size() == responses.size());
  210. size_t i = 0;
  211. {
  212. auto &res = responses[i++];
  213. EXPECT_EQ(200, res.status);
  214. }
  215. {
  216. auto &res = responses[i++];
  217. EXPECT_EQ(200, res.status);
  218. }
  219. {
  220. int count = paths.size();
  221. while (count--) {
  222. auto &res = responses[i++];
  223. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res.body);
  224. EXPECT_EQ(200, res.status);
  225. }
  226. }
  227. for (; i < responses.size(); i++) {
  228. auto &res = responses[i];
  229. EXPECT_EQ(200, res.status);
  230. }
  231. }
  232. TEST(KeepAliveTest, NoSSLWithBasic) {
  233. Client cli("httpbin.org");
  234. KeepAliveTest(cli, true);
  235. }
  236. TEST(KeepAliveTest, SSLWithBasic) {
  237. SSLClient cli("httpbin.org");
  238. KeepAliveTest(cli, true);
  239. }
  240. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  241. TEST(KeepAliveTest, NoSSLWithDigest) {
  242. Client cli("httpbin.org");
  243. KeepAliveTest(cli, false);
  244. }
  245. TEST(KeepAliveTest, SSLWithDigest) {
  246. SSLClient cli("httpbin.org");
  247. KeepAliveTest(cli, false);
  248. }
  249. #endif