proxy_tests.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <string>
4 
5 #include <cpr/cpr.h>
6 
7 // TODO: This uses public servers for proxies and endpoints. This should be replaced with a source
8 // code implementation inside server.cpp
9 
10 #define HTTP_PROXY "104.131.214.38:3128"
11 #define HTTPS_PROXY "104.131.214.38:3128"
12 
13 using namespace cpr;
14 
15 TEST(ProxyTests, SingleProxyTest) {
16  auto url = Url{"http://www.httpbin.org/get"};
17  auto response = cpr::Get(url, Proxies{{"http", HTTP_PROXY}});
18  EXPECT_EQ(url, response.url);
19  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
20  EXPECT_EQ(200, response.status_code);
21  EXPECT_EQ(ErrorCode::OK, response.error.code);
22 }
23 
24 TEST(ProxyTests, MultipleProxyHttpTest) {
25  auto url = Url{"http://www.httpbin.org/get"};
26  auto response = cpr::Get(url, Proxies{{"http", HTTP_PROXY},
27  {"https", HTTPS_PROXY}});
28  EXPECT_EQ(url, response.url);
29  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
30  EXPECT_EQ(200, response.status_code);
31  EXPECT_EQ(ErrorCode::OK, response.error.code);
32 }
33 
34 // TODO: These should be fixed after a source code implementation of an HTTPS proxy
35 #if defined(false)
36 TEST(ProxyTests, ProxyHttpsTest) {
37  auto url = Url{"https://www.httpbin.org/get"};
38  auto response = cpr::Get(url, Proxies{{"https", HTTPS_PROXY}});
39  EXPECT_EQ(url, response.url);
40  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
41  EXPECT_EQ(200, response.status_code);
42  EXPECT_EQ(ErrorCode::OK, response.error.code);
43 }
44 
45 TEST(ProxyTests, MultipleProxyHttpsTest) {
46  auto url = Url{"https://www.httpbin.org/get"};
47  auto response = cpr::Get(url, Proxies{{"http", HTTP_PROXY},
48  {"https", HTTPS_PROXY}});
49  EXPECT_EQ(url, response.url);
50  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
51  EXPECT_EQ(200, response.status_code);
52  EXPECT_EQ(ErrorCode::OK, response.error.code);
53 }
54 #endif
55 
56 TEST(ProxyTests, CopyProxyTest) {
57  auto url = Url{"http://www.httpbin.org/get"};
58  auto proxies = Proxies{{"http", HTTP_PROXY}};
59  auto response = cpr::Get(url, proxies);
60  EXPECT_EQ(url, response.url);
61  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
62  EXPECT_EQ(200, response.status_code);
63  EXPECT_EQ(ErrorCode::OK, response.error.code);
64 }
65 
66 TEST(ProxyTests, ProxySessionTest) {
67  auto url = Url{"http://www.httpbin.org/get"};
68  Session session;
69  session.SetUrl(url);
70  session.SetProxies(Proxies{{"http", HTTP_PROXY}});
71  auto response = session.Get();
72  EXPECT_EQ(url, response.url);
73  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
74  EXPECT_EQ(200, response.status_code);
75  EXPECT_EQ(ErrorCode::OK, response.error.code);
76 }
77 
78 TEST(ProxyTests, ReferenceProxySessionTest) {
79  auto url = Url{"http://www.httpbin.org/get"};
80  auto proxies = Proxies{{"http", HTTP_PROXY}};
81  Session session;
82  session.SetUrl(url);
83  session.SetProxies(proxies);
84  auto response = session.Get();
85  EXPECT_EQ(url, response.url);
86  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
87  EXPECT_EQ(200, response.status_code);
88  EXPECT_EQ(ErrorCode::OK, response.error.code);
89 }
90 
91 int main(int argc, char** argv) {
92  ::testing::InitGoogleTest(&argc, argv);
93  return RUN_ALL_TESTS();
94 }
std::string Url
Definition: cprtypes.h:14
::std::string string
Definition: gtest-port.h:1129
void SetUrl(const Url &url)
Definition: session.cpp:452
TEST(ProxyTests, SingleProxyTest)
Definition: proxy_tests.cpp:15
Response Get()
Definition: session.cpp:498
#define HTTPS_PROXY
Definition: proxy_tests.cpp:11
int main(int argc, char **argv)
Definition: proxy_tests.cpp:91
void SetProxies(Proxies &&proxies)
Definition: session.cpp:464
#define HTTP_PROXY
Definition: proxy_tests.cpp:10
#define EXPECT_EQ(expected, actual)
Definition: gtest.h:2015
Response Get(Ts &&...ts)
Definition: api.h:38
Definition: auth.cpp:3
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2325
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5292


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16