proxy_tests.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <gtest/gtest.h>
  2. #include <string>
  3. #include <cpr/cpr.h>
  4. // TODO: This uses public servers for proxies and endpoints. This should be replaced with a source
  5. // code implementation inside server.cpp
  6. #define HTTP_PROXY "51.159.4.98:80"
  7. #define HTTPS_PROXY "51.104.53.182:8000"
  8. using namespace cpr;
  9. TEST(ProxyTests, SingleProxyTest) {
  10. Url url{"http://www.httpbin.org/get"};
  11. Response response = cpr::Get(url, Proxies{{"http", HTTP_PROXY}});
  12. EXPECT_EQ(url, response.url);
  13. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  14. EXPECT_EQ(200, response.status_code);
  15. EXPECT_EQ(ErrorCode::OK, response.error.code);
  16. }
  17. TEST(ProxyTests, MultipleProxyHttpTest) {
  18. Url url{"http://www.httpbin.org/get"};
  19. Response response = cpr::Get(url, Proxies{{"http", HTTP_PROXY}, {"https", HTTPS_PROXY}});
  20. EXPECT_EQ(url, response.url);
  21. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  22. EXPECT_EQ(200, response.status_code);
  23. EXPECT_EQ(ErrorCode::OK, response.error.code);
  24. }
  25. // TODO: These should be fixed after a source code implementation of an HTTPS proxy
  26. #if defined(false)
  27. TEST(ProxyTests, ProxyHttpsTest) {
  28. Url url{"https://www.httpbin.org/get"};
  29. Response response = cpr::Get(url, Proxies{{"https", HTTPS_PROXY}});
  30. EXPECT_EQ(url, response.url);
  31. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  32. EXPECT_EQ(200, response.status_code);
  33. EXPECT_EQ(ErrorCode::OK, response.error.code);
  34. }
  35. TEST(ProxyTests, MultipleProxyHttpsTest) {
  36. Url url{"https://www.httpbin.org/get"};
  37. Response response = cpr::Get(url, Proxies{{"http", HTTP_PROXY}, {"https", HTTPS_PROXY}});
  38. EXPECT_EQ(url, response.url);
  39. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  40. EXPECT_EQ(200, response.status_code);
  41. EXPECT_EQ(ErrorCode::OK, response.error.code);
  42. }
  43. #endif
  44. TEST(ProxyTests, CopyProxyTest) {
  45. Url url{"http://www.httpbin.org/get"};
  46. Proxies proxies{{"http", HTTP_PROXY}};
  47. Response response = cpr::Get(url, proxies);
  48. EXPECT_EQ(url, response.url);
  49. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  50. EXPECT_EQ(200, response.status_code);
  51. EXPECT_EQ(ErrorCode::OK, response.error.code);
  52. }
  53. TEST(ProxyTests, ProxySessionTest) {
  54. Url url{"http://www.httpbin.org/get"};
  55. Session session;
  56. session.SetUrl(url);
  57. session.SetProxies(Proxies{{"http", HTTP_PROXY}});
  58. Response response = session.Get();
  59. EXPECT_EQ(url, response.url);
  60. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  61. EXPECT_EQ(200, response.status_code);
  62. EXPECT_EQ(ErrorCode::OK, response.error.code);
  63. }
  64. TEST(ProxyTests, ReferenceProxySessionTest) {
  65. Url url{"http://www.httpbin.org/get"};
  66. Proxies proxies{{"http", HTTP_PROXY}};
  67. Session session;
  68. session.SetUrl(url);
  69. session.SetProxies(proxies);
  70. Response response = session.Get();
  71. EXPECT_EQ(url, response.url);
  72. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  73. EXPECT_EQ(200, response.status_code);
  74. EXPECT_EQ(ErrorCode::OK, response.error.code);
  75. }
  76. int main(int argc, char** argv) {
  77. ::testing::InitGoogleTest(&argc, argv);
  78. return RUN_ALL_TESTS();
  79. }