Skip to content

Commit 2dc309c

Browse files
committed
Add wrap_response
1 parent 513414c commit 2dc309c

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

simple_http.hpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct Tiny {
5050
}
5151

5252
[[nodiscard]]
53-
std::string toString() const {
53+
std::string to_string() const {
5454
std::ostringstream ss;
5555
ss << value();
5656
return ss.str();
@@ -96,18 +96,18 @@ using QueryParameters = std::vector<std::pair<QueryParameterKey, QueryParameterV
9696

9797
inline static const std::string WHITESPACE = "\n\t\f\v\r ";
9898

99-
inline static std::string leftTrim(const std::string &candidate) {
99+
inline static std::string left_trim(const std::string &candidate) {
100100
size_t start = candidate.find_first_not_of(WHITESPACE);
101101
return (start == std::string::npos) ? "" : candidate.substr(start);
102102
}
103103

104-
inline static std::string rightTrim(const std::string &candidate) {
104+
inline static std::string right_trim(const std::string &candidate) {
105105
size_t end = candidate.find_last_not_of(WHITESPACE);
106106
return (end == std::string::npos) ? "" : candidate.substr(0, end + 1);
107107
}
108108

109109
inline static std::string trim(const std::string &candidate) {
110-
return rightTrim(leftTrim(candidate));
110+
return right_trim(left_trim(candidate));
111111
}
112112

113113
inline static std::vector<std::string> vec(const std::string& candidate, const char separator) {
@@ -354,6 +354,12 @@ inline static Predicate<HttpStatusCode> server_error() {
354354
return between_inclusive(INTERNAL_SERVER_ERROR, NETWORK_AUTHENTICATION_REQUIRED);
355355
}
356356

357+
template<class A>
358+
A wrap_response(std::optional<HttpResponse> response,
359+
std::function<A(const std::optional<HttpResponse> &response)> wrapperFn) {
360+
return wrapperFn(response);
361+
}
362+
357363
struct Client final {
358364
Client() : error_callback_(NoopErrorCallback), debug_(false), verify_(true) {}
359365
explicit Client(ErrorCallback error_callback) : error_callback_(std::move(error_callback)), debug_(false), verify_(true) {}

test/integration_tests.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,21 @@ TEST_CASE("Integration Tests")
131131

132132
CHECK(keys["get"] == "ok");
133133
}
134+
135+
SECTION("Wrap Response")
136+
{
137+
auto maybe_response = client.get(url.with_path_segments({SimpleHttp::PathSegment{"get"}}),
138+
SimpleHttp::successful());
139+
140+
REQUIRE(maybe_response);
141+
142+
std::function<std::string(const std::optional<SimpleHttp::HttpResponse> &response)> capture_get = [&maybe_response](const auto &response) {
143+
auto keys = nlohmann::json::parse(maybe_response.value().body.value());
144+
return keys["get"];
145+
};
146+
147+
auto wrapped = SimpleHttp::wrap_response<std::string>(maybe_response, capture_get);
148+
149+
CHECK(wrapped == "ok");
150+
}
134151
}

test/unit_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ TEST_CASE("Trimming") {
8181
{
8282
std::string candidate = "\n test";
8383
CHECK(candidate != "test");
84-
CHECK(SimpleHttp::leftTrim(candidate) == "test");
84+
CHECK(SimpleHttp::left_trim(candidate) == "test");
8585
}
8686

8787
SECTION("Right Trim")
8888
{
8989
std::string candidate = "test \r\n";
9090
CHECK(candidate != "test");
91-
CHECK(SimpleHttp::rightTrim(candidate) == "test");
91+
CHECK(SimpleHttp::right_trim(candidate) == "test");
9292
}
9393

9494
SECTION("Trim")

0 commit comments

Comments
 (0)