Skip to content

Commit

Permalink
Add wrap_response
Browse files Browse the repository at this point in the history
  • Loading branch information
abedra committed Jul 7, 2021
1 parent 513414c commit 2dc309c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
14 changes: 10 additions & 4 deletions simple_http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct Tiny {
}

[[nodiscard]]
std::string toString() const {
std::string to_string() const {
std::ostringstream ss;
ss << value();
return ss.str();
Expand Down Expand Up @@ -96,18 +96,18 @@ using QueryParameters = std::vector<std::pair<QueryParameterKey, QueryParameterV

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

inline static std::string leftTrim(const std::string &candidate) {
inline static std::string left_trim(const std::string &candidate) {
size_t start = candidate.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : candidate.substr(start);
}

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

inline static std::string trim(const std::string &candidate) {
return rightTrim(leftTrim(candidate));
return right_trim(left_trim(candidate));
}

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

template<class A>
A wrap_response(std::optional<HttpResponse> response,
std::function<A(const std::optional<HttpResponse> &response)> wrapperFn) {
return wrapperFn(response);
}

struct Client final {
Client() : error_callback_(NoopErrorCallback), debug_(false), verify_(true) {}
explicit Client(ErrorCallback error_callback) : error_callback_(std::move(error_callback)), debug_(false), verify_(true) {}
Expand Down
17 changes: 17 additions & 0 deletions test/integration_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,21 @@ TEST_CASE("Integration Tests")

CHECK(keys["get"] == "ok");
}

SECTION("Wrap Response")
{
auto maybe_response = client.get(url.with_path_segments({SimpleHttp::PathSegment{"get"}}),
SimpleHttp::successful());

REQUIRE(maybe_response);

std::function<std::string(const std::optional<SimpleHttp::HttpResponse> &response)> capture_get = [&maybe_response](const auto &response) {
auto keys = nlohmann::json::parse(maybe_response.value().body.value());
return keys["get"];
};

auto wrapped = SimpleHttp::wrap_response<std::string>(maybe_response, capture_get);

CHECK(wrapped == "ok");
}
}
4 changes: 2 additions & 2 deletions test/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ TEST_CASE("Trimming") {
{
std::string candidate = "\n test";
CHECK(candidate != "test");
CHECK(SimpleHttp::leftTrim(candidate) == "test");
CHECK(SimpleHttp::left_trim(candidate) == "test");
}

SECTION("Right Trim")
{
std::string candidate = "test \r\n";
CHECK(candidate != "test");
CHECK(SimpleHttp::rightTrim(candidate) == "test");
CHECK(SimpleHttp::right_trim(candidate) == "test");
}

SECTION("Trim")
Expand Down

0 comments on commit 2dc309c

Please sign in to comment.