Skip to content

Commit

Permalink
Updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-shklover committed Oct 21, 2024
1 parent 7a3456c commit 6c645c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cpr/session.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cpr/session.h"

#include <array>
#include <atomic>
#include <cassert>
#include <cstdint>
Expand Down Expand Up @@ -182,7 +183,7 @@ void Session::prepareCommonShared() {
}
// handle NO_PROXY override passed through Proxies object
// Example: Proxies{"no_proxy": ""} will override environment variable definition with an empty list
const std::string no_proxy[] = {"no_proxy", "NO_PROXY"};
const std::array<std::string, 2> no_proxy{"no_proxy", "NO_PROXY"};
for (const auto& item : no_proxy) {
if (proxies_.has(item)) {
curl_easy_setopt(curl_->handle, CURLOPT_NOPROXY, proxies_[item].c_str());
Expand Down
52 changes: 36 additions & 16 deletions test/proxy_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <gtest/gtest.h>

#include <stdlib.h>
#include <string>
#include <sstream>

#include "cpr/cpr.h"

Expand Down Expand Up @@ -86,24 +88,42 @@ TEST(ProxyTests, ReferenceProxySessionTest) {
EXPECT_EQ(ErrorCode::OK, response.error.code);
}

if 0
TEST(ProxyTests, NoProxyTest) {
std::putenv("NO_PROXY", "www.httpbin.org");
Url url{"http://www.httpbin.org/get"};
Proxies proxies{{"http", HTTP_PROXY, "no_proxy", ""}};
Session session;
session.SetUrl(url);
session.SetProxies(proxies);
Response response = session.Get();
EXPECT_EQ(url, response.url);
EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
EXPECT_EQ(200, response.status_code);
EXPECT_EQ(ErrorCode::OK, response.error.code);
// TODO: check that access was performed through the proxy
// json body = json.parse(response.text);
// EXPECT_EQ(body["origin"], HTTP_PROXY.domain());
setenv("NO_PROXY", "httpbin.org", 1);
try {
Url url{"http://www.httpbin.org/get"};
Proxies proxies{{"http", HTTP_PROXY}, {"no_proxy", ""}};
Session session;
session.SetUrl(url);
session.SetProxies(proxies);
Response response = session.Get();
EXPECT_EQ(url, response.url);
EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
EXPECT_EQ(200, response.status_code);
EXPECT_EQ(ErrorCode::OK, response.error.code);

// check that access was performed through the proxy
std::string proxy_ip = HTTP_PROXY;
proxy_ip = proxy_ip.substr(0, proxy_ip.find(':'));

// find "origin": "ip" in response:
bool found = false;
std::istringstream body(response.text);
std::string line;
while (std::getline(body, line)) {
// example: "origin": "123.456.789.123"
if (line.find("\"origin\":") != std::string::npos) {
found = line.find(proxy_ip) != std::string::npos;
break;
}
}
EXPECT_TRUE(found);
} catch (...) {
unsetenv("NO_PROXY");
throw;
}
unsetenv("NO_PROXY");
}
#endif

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit 6c645c7

Please sign in to comment.