2
0

test_proxy.cc 7.3 KB

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