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