1
0

test_proxy.cc 7.6 KB

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