proxy_auth_tests.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <gtest/gtest.h>
  2. #include <string>
  3. #include "cpr/cpr.h"
  4. #include "httpServer.hpp"
  5. // TODO: This requires a local proxy server (squid). This should be replaced with a source
  6. // code implementation.
  7. #define HTTP_PROXY "127.0.0.1:3128"
  8. #define HTTPS_PROXY "127.0.0.1:3128"
  9. #define PROXY_USER "u$er"
  10. #define PROXY_PASS "p@ss"
  11. using namespace cpr;
  12. static HttpServer* server = new HttpServer();
  13. // TODO: These should be fixed after a source code implementation of a proxy
  14. #if defined(false)
  15. TEST(ProxyAuthTests, SingleProxyTest) {
  16. Url url{server->GetBaseUrl() + "/hello.html"};
  17. Response response = cpr::Get(url, Proxies{{"http", HTTP_PROXY}}, ProxyAuthentication{{"http", EncodedAuthentication{PROXY_USER, PROXY_PASS}}});
  18. std::string expected_text{"Hello world!"};
  19. EXPECT_EQ(expected_text, response.text);
  20. EXPECT_EQ(url, response.url);
  21. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  22. EXPECT_EQ(200, response.status_code);
  23. EXPECT_EQ(ErrorCode::OK, response.error.code);
  24. }
  25. TEST(ProxyAuthTests, MultipleProxyHttpTest) {
  26. Url url{server->GetBaseUrl() + "/hello.html"};
  27. Response response = cpr::Get(url, Proxies{{"https", HTTPS_PROXY}, {"http", HTTP_PROXY}}, ProxyAuthentication{{"http", EncodedAuthentication{PROXY_USER, PROXY_PASS}}, {"https", EncodedAuthentication{PROXY_USER, PROXY_PASS}}});
  28. std::string expected_text{"Hello world!"};
  29. EXPECT_EQ(expected_text, response.text);
  30. EXPECT_EQ(url, response.url);
  31. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  32. EXPECT_EQ(200, response.status_code);
  33. EXPECT_EQ(ErrorCode::OK, response.error.code);
  34. }
  35. TEST(ProxyAuthTests, CopyProxyTest) {
  36. Url url{server->GetBaseUrl() + "/hello.html"};
  37. Proxies proxies{{"http", HTTP_PROXY}};
  38. ProxyAuthentication proxy_auth{{"http", EncodedAuthentication{PROXY_USER, PROXY_PASS}}};
  39. Response response = cpr::Get(url, proxies, proxy_auth);
  40. std::string expected_text{"Hello world!"};
  41. EXPECT_EQ(expected_text, response.text);
  42. EXPECT_EQ(url, response.url);
  43. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  44. EXPECT_EQ(200, response.status_code);
  45. EXPECT_EQ(ErrorCode::OK, response.error.code);
  46. }
  47. TEST(ProxyAuthTests, ProxySessionTest) {
  48. Url url{server->GetBaseUrl() + "/hello.html"};
  49. Session session;
  50. session.SetUrl(url);
  51. session.SetProxies(Proxies{{"http", HTTP_PROXY}});
  52. session.SetProxyAuth(ProxyAuthentication{{"http", EncodedAuthentication{PROXY_USER, PROXY_PASS}}});
  53. Response response = session.Get();
  54. std::string expected_text{"Hello world!"};
  55. EXPECT_EQ(expected_text, response.text);
  56. EXPECT_EQ(url, response.url);
  57. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  58. EXPECT_EQ(200, response.status_code);
  59. EXPECT_EQ(ErrorCode::OK, response.error.code);
  60. }
  61. TEST(ProxyAuthTests, ReferenceProxySessionTest) {
  62. Url url{server->GetBaseUrl() + "/hello.html"};
  63. Proxies proxies{{"http", HTTP_PROXY}};
  64. ProxyAuthentication proxy_auth{{"http", EncodedAuthentication{PROXY_USER, PROXY_PASS}}};
  65. Session session;
  66. session.SetUrl(url);
  67. session.SetProxies(proxies);
  68. session.SetProxyAuth(proxy_auth);
  69. Response response = session.Get();
  70. std::string expected_text{"Hello world!"};
  71. EXPECT_EQ(expected_text, response.text);
  72. EXPECT_EQ(url, response.url);
  73. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  74. EXPECT_EQ(200, response.status_code);
  75. EXPECT_EQ(ErrorCode::OK, response.error.code);
  76. }
  77. #endif
  78. int main(int argc, char** argv) {
  79. ::testing::InitGoogleTest(&argc, argv);
  80. ::testing::AddGlobalTestEnvironment(server);
  81. return RUN_ALL_TESTS();
  82. }