Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/cpr/redirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ enum class PostRedirectFlags : uint8_t {
**/
POST_303 = 0x1 << 2,
/**
* Default value.
* Convenience option to enable all flags.
* Same as CURL_REDIR_POST_ALL (https://curl.se/libcurl/c/CURLOPT_POSTREDIR.html).
**/
POST_ALL = POST_301 | POST_302 | POST_303,
/**
* Default value.
* * Convenience option to disable all flags.
**/
NONE = 0x0
Expand Down Expand Up @@ -66,9 +66,9 @@ class Redirect {
bool cont_send_cred{false};
/**
* Flags to control how to act after a redirect for a post request.
* Default: POST_ALL
* Default: NONE (matches libcurl)
**/
PostRedirectFlags post_flags{PostRedirectFlags::POST_ALL};
PostRedirectFlags post_flags{PostRedirectFlags::NONE};

Redirect() = default;
// NOLINTNEXTLINE (google-runtime-int)
Expand Down
22 changes: 20 additions & 2 deletions test/post_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ TEST(UrlEncodedPostTests, PostBodyWithBuffer) {

TEST(PostRedirectTests, TempRedirectTest) {
Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::POST_ALL));
std::string expected_text{
"{\n"
" \"x\": 5\n"
Expand All @@ -740,7 +740,7 @@ TEST(PostRedirectTests, TempRedirectNoneTest) {

TEST(PostRedirectTests, PermRedirectTest) {
Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::POST_ALL));
std::string expected_text{
"{\n"
" \"x\": 5\n"
Expand All @@ -760,6 +760,24 @@ TEST(PostRedirectTests, PermRedirectNoneTest) {
EXPECT_EQ(ErrorCode::OK, response.error.code);
}

TEST(PostRedirectTests, TempRedirectDefaultTest) {
Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
// Default PostRedirectFlags is NONE, so POST should not be preserved on redirect
Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
EXPECT_EQ(405, response.status_code);
EXPECT_EQ(ErrorCode::OK, response.error.code);
}

TEST(PostRedirectTests, PermRedirectDefaultTest) {
Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
// Default PostRedirectFlags is NONE, so POST should not be preserved on redirect
Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
EXPECT_EQ(405, response.status_code);
EXPECT_EQ(ErrorCode::OK, response.error.code);
}

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