test_proxy.cc 6.9 KB

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