|
1 | 1 | #include <gtest/gtest.h> |
2 | 2 |
|
| 3 | +#include <chrono> |
| 4 | +#include <stdlib.h> |
3 | 5 | #include <string> |
| 6 | +#include <sstream> |
4 | 7 |
|
5 | 8 | #include "cpr/cpr.h" |
6 | 9 |
|
7 | 10 | // TODO: This uses public servers for proxies and endpoints. This should be replaced with a source |
8 | 11 | // code implementation inside server.cpp |
9 | 12 |
|
| 13 | +// NOTES: |
| 14 | +// * For no-proxy testing need to run the tests with direct connection to the internet |
| 15 | +// * List of free proxies for testing can be found at https://proxy-list.org/english/index.php |
| 16 | +// Example: #define HTTP_PROXY "http://162.223.90.130:80" |
10 | 17 | #define HTTP_PROXY "51.159.4.98:80" |
11 | 18 | #define HTTPS_PROXY "51.104.53.182:8000" |
12 | 19 |
|
| 20 | + |
13 | 21 | using namespace cpr; |
14 | 22 |
|
15 | 23 | TEST(ProxyTests, SingleProxyTest) { |
@@ -79,13 +87,57 @@ TEST(ProxyTests, ReferenceProxySessionTest) { |
79 | 87 | Session session; |
80 | 88 | session.SetUrl(url); |
81 | 89 | session.SetProxies(proxies); |
| 90 | + session.SetTimeout(std::chrono::seconds(10)); |
82 | 91 | Response response = session.Get(); |
83 | 92 | EXPECT_EQ(url, response.url); |
84 | 93 | EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); |
85 | 94 | EXPECT_EQ(200, response.status_code); |
86 | 95 | EXPECT_EQ(ErrorCode::OK, response.error.code); |
87 | 96 | } |
88 | 97 |
|
| 98 | +TEST(ProxyTests, NoProxyTest) { |
| 99 | + setenv("NO_PROXY", "httpbin.org", 1); |
| 100 | + try { |
| 101 | + Url url{"http://www.httpbin.org/get"}; |
| 102 | + Proxies proxies{{"http", HTTP_PROXY}, {"no_proxy", ""}}; |
| 103 | + Session session; |
| 104 | + session.SetUrl(url); |
| 105 | + session.SetProxies(proxies); |
| 106 | + session.SetTimeout(std::chrono::seconds(10)); |
| 107 | + Response response = session.Get(); |
| 108 | + EXPECT_EQ(url, response.url); |
| 109 | + EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); |
| 110 | + EXPECT_EQ(200, response.status_code); |
| 111 | + EXPECT_EQ(ErrorCode::OK, response.error.code); |
| 112 | + |
| 113 | + // check that access was performed through the proxy |
| 114 | + std::string proxy_ip = HTTP_PROXY; |
| 115 | + if (proxy_ip[0] == 'h') { |
| 116 | + // drop protocol: |
| 117 | + proxy_ip = proxy_ip.substr(proxy_ip.find(':') + 3); |
| 118 | + } |
| 119 | + // drop port: |
| 120 | + proxy_ip = proxy_ip.substr(0, proxy_ip.find(':')); |
| 121 | + |
| 122 | + // find "origin": "ip" in response: |
| 123 | + bool found = false; |
| 124 | + std::istringstream body(response.text); |
| 125 | + std::string line; |
| 126 | + while (std::getline(body, line)) { |
| 127 | + // example: "origin": "123.456.789.123" |
| 128 | + if (line.find("\"origin\":") != std::string::npos) { |
| 129 | + found = line.find(proxy_ip) != std::string::npos; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + EXPECT_TRUE(found); |
| 134 | + } catch (...) { |
| 135 | + unsetenv("NO_PROXY"); |
| 136 | + throw; |
| 137 | + } |
| 138 | + unsetenv("NO_PROXY"); |
| 139 | +} |
| 140 | + |
89 | 141 | int main(int argc, char** argv) { |
90 | 142 | ::testing::InitGoogleTest(&argc, argv); |
91 | 143 | return RUN_ALL_TESTS(); |
|
0 commit comments