File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -61,6 +61,9 @@ conn->SetTimeout(5);
6161// (this will result in the UA "foo/cool restclient-cpp/VERSION")
6262conn->SetUserAgent("foo/cool");
6363
64+ // enable following of redirects (default is off)
65+ conn->FollowRedirects(true);
66+
6467// set headers
6568RestClient::HeaderFields headers;
6669headers[ "Accept"] = "application/json";
Original file line number Diff line number Diff 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;
Original file line number Diff line number Diff 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) {
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments