Skip to content

Commit fda9c94

Browse files
committed
implement FollowRedirects
1 parent 7bec775 commit fda9c94

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ conn->SetTimeout(5);
6161
// (this will result in the UA "foo/cool restclient-cpp/VERSION")
6262
conn->SetUserAgent("foo/cool");
6363

64+
// enable following of redirects (default is off)
65+
conn->FollowRedirects(true);
66+
6467
// set headers
6568
RestClient::HeaderFields headers;
6669
headers["Accept"] = "application/json";

include/restclient-cpp/connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class Connection {
123123
std::string baseUrl;
124124
RestClient::HeaderFields headerFields;
125125
int timeout;
126+
bool followRedirects;
126127
struct {
127128
std::string username;
128129
std::string password;

source/connection.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ RestClient::Connection::GetHeaders() {
8989
return this->headerFields;
9090
}
9191

92+
/**
93+
* @brief configure whether to follow redirects on this connection
94+
*
95+
* @param follow - boolean whether to follow redirects
96+
*/
97+
void
98+
RestClient::Connection::FollowRedirects(bool follow) {
99+
this->followRedirects = follow;
100+
}
101+
92102
/**
93103
* @brief set custom user agent for connection. This gets prepended to the
94104
* default restclient-cpp/RESTCLIENT_VERSION string
@@ -203,6 +213,10 @@ RestClient::Connection::performCurlRequest(const std::string& uri) {
203213
// dont want to get a sig alarm on timeout
204214
curl_easy_setopt(this->curlHandle, CURLOPT_NOSIGNAL, 1);
205215
}
216+
// set follow redirect
217+
if (this->followRedirects == true) {
218+
curl_easy_setopt(this->curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
219+
}
206220
res = curl_easy_perform(this->curlHandle);
207221
if (res != CURLE_OK) {
208222
if (res == CURLE_OPERATION_TIMEDOUT) {

test/test_connection.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,22 @@ TEST_F(ConnectionTest, TestGetInfo)
153153
EXPECT_EQ(0, info.lastRequest.redirectTime);
154154
EXPECT_EQ(0, info.lastRequest.redirectCount);
155155
}
156+
157+
TEST_F(ConnectionTest, TestFollowRedirect)
158+
{
159+
RestClient::Response res = conn->get("/redirect/2");
160+
EXPECT_EQ(302, res.code);
161+
conn->FollowRedirects(true);
162+
res = conn->get("/redirect/2");
163+
EXPECT_EQ(200, res.code);
164+
}
165+
166+
TEST_F(ConnectionTest, TestGetInfoFromRedirect)
167+
{
168+
conn->FollowRedirects(true);
169+
RestClient::Response res = conn->get("/redirect/2");
170+
EXPECT_EQ(200, res.code);
171+
RestClient::Connection::Info info = conn->GetInfo();
172+
EXPECT_NE(0, info.lastRequest.redirectTime);
173+
EXPECT_NE(0, info.lastRequest.redirectCount);
174+
}

0 commit comments

Comments
 (0)