Skip to content

Commit dbcdd2f

Browse files
authored
Merge pull request #1173 from simue/feature/delete-header
Getter function for Session::header_ to enable the user to read back all headers set and delete select ones
2 parents c8a803c + a9466f7 commit dbcdd2f

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Contributing to C++ Requests
22

3-
Please fork this repository and contribute back using [pull requests](https://github.com/whoshuu/cpr/pulls). Features can be requested using [issues](https://github.com/whoshuu/cpr/issues). All code, comments, and critiques are greatly appreciated.
3+
Please fork this repository and contribute back using [pull requests](https://github.com/libcpr/cpr/pulls). Features can be requested using [issues](https://github.com/libcpr/cpr/issues). All code, comments, and critiques are greatly appreciated.
44

55
## Formatting
66

7-
To avoid unproductive debates on formatting, this project uses `clang-format` to ensure a consistent style across all source files. Currently, `clang-format` 3.8 is the version of `clang-format` we use. The format file can be found [here](https://github.com/whoshuu/cpr/blob/master/.clang-format). To install `clang-format` on Ubuntu, run this:
7+
To avoid unproductive debates on formatting, this project uses `clang-format` to ensure a consistent style across all source files. Currently, `clang-format` 3.8 is the version of `clang-format` we use. The format file can be found [here](https://github.com/libcpr/cpr/blob/master/.clang-format). To install `clang-format` on Ubuntu, run this:
88

99
```
1010
apt-get install clang-format-3.8
@@ -21,7 +21,7 @@ Note that `brew` might install a later version of `clang-format`, but it should
2121
To run `clang-format` on every source file, run this in the root directory:
2222

2323
```
24-
./format-check.sh
24+
./scripts/run_clang_format.sh cpr include/cpr
2525
```
2626

2727
This should indicate which files need formatting and also show a diff of the requested changes. More specific usage instructions can be found on the official [LLVM website](http://releases.llvm.org/3.8.0/tools/clang/docs/ClangFormat.html).

cpr/error.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "cpr/error.h"
2-
#include <curl/curlver.h>
3-
#include <unordered_map>
42
#include <cstdint>
53
#include <curl/curl.h>
4+
#include <curl/curlver.h>
5+
#include <unordered_map>
66

77
namespace cpr {
8-
static const std::unordered_map<std::int32_t, ErrorCode> curl_error_map = { // NOLINT - (needed because of static init)
8+
// NOLINTNEXTLINE - (needed because of static init)
9+
static const std::unordered_map<std::int32_t, ErrorCode> curl_error_map = {
910
{CURLE_OK, ErrorCode::OK},
1011
{CURLE_UNSUPPORTED_PROTOCOL, ErrorCode::UNSUPPORTED_PROTOCOL},
1112
{CURLE_FAILED_INIT, ErrorCode::FAILED_INIT},

cpr/session.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,14 @@ void Session::UpdateHeader(const Header& header) {
349349
}
350350
}
351351

352+
Header& Session::GetHeader() {
353+
return header_;
354+
}
355+
356+
const Header& Session::GetHeader() const {
357+
return header_;
358+
}
359+
352360
void Session::SetTimeout(const Timeout& timeout) {
353361
curl_easy_setopt(curl_->handle, CURLOPT_TIMEOUT_MS, timeout.Milliseconds());
354362
}

include/cpr/async_wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "cpr/response.h"
99

1010
namespace cpr {
11-
enum class [[nodiscard]] CancellationResult { failure, success, invalid_operation };
11+
enum class [[nodiscard]] CancellationResult{failure, success, invalid_operation};
1212

1313
/**
1414
* A class template intended to wrap results of async operations (instances of std::future<T>)

include/cpr/session.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class Session : public std::enable_shared_from_this<Session> {
6666
void SetParameters(Parameters&& parameters);
6767
void SetHeader(const Header& header);
6868
void UpdateHeader(const Header& header);
69+
[[nodiscard]] Header& GetHeader();
70+
[[nodiscard]] const Header& GetHeader() const;
6971
void SetTimeout(const Timeout& timeout);
7072
void SetConnectTimeout(const ConnectTimeout& timeout);
7173
void SetAuth(const Authentication& auth);

include/cpr/timeout.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ class Timeout {
1111
// Template constructor to accept any chrono duration type and convert it to milliseconds
1212
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
1313
template <typename Rep, typename Period>
14-
Timeout(const std::chrono::duration<Rep, Period>& duration)
15-
: ms{std::chrono::duration_cast<std::chrono::milliseconds>(duration)} {}
14+
Timeout(const std::chrono::duration<Rep, Period>& duration) : ms{std::chrono::duration_cast<std::chrono::milliseconds>(duration)} {}
1615

1716
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
1817
Timeout(const std::int32_t& milliseconds) : Timeout{std::chrono::milliseconds(milliseconds)} {}

test/session_tests.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,27 @@ TEST(MultipleGetTests, HeaderChangeMultipleGetTest) {
262262
EXPECT_EQ(200, response.status_code);
263263
EXPECT_EQ(ErrorCode::OK, response.error.code);
264264
}
265-
session.SetHeader(Header{{"key", "value"}});
265+
session.SetHeader(Header{{"key", "value"}, {"lorem", "ipsum"}});
266266
{
267267
Response response = session.Get();
268268
std::string expected_text{"Header reflect GET"};
269269
EXPECT_EQ(expected_text, response.text);
270270
EXPECT_EQ(url, response.url);
271271
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
272272
EXPECT_EQ(std::string{"value"}, response.header["key"]);
273+
EXPECT_EQ(std::string{"ipsum"}, response.header["lorem"]);
274+
EXPECT_EQ(200, response.status_code);
275+
EXPECT_EQ(ErrorCode::OK, response.error.code);
276+
}
277+
Header& headerMap = session.GetHeader();
278+
headerMap.erase("key");
279+
{
280+
Response response = session.Get();
281+
std::string expected_text{"Header reflect GET"};
282+
EXPECT_EQ(expected_text, response.text);
283+
EXPECT_EQ(url, response.url);
284+
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
285+
EXPECT_EQ(std::string{"ipsum"}, response.header["lorem"]);
273286
EXPECT_EQ(200, response.status_code);
274287
EXPECT_EQ(ErrorCode::OK, response.error.code);
275288
}

0 commit comments

Comments
 (0)