Skip to content

Commit

Permalink
Added custom curl request support.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Oct 25, 2023
1 parent f65b0ee commit 601eda9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
23 changes: 16 additions & 7 deletions include/roar/curl/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,58 +342,67 @@ namespace Roar::Curl
* @brief Finishes and performs the request as a get request.
*
* @param url The target to send this request to.
* @return Request& Returned for chaining.
* @return A response object.
*/
Response get(std::string const& url);

/**
* @brief Finishes and performs the request as a put request.
*
* @param url The target to send this request to.
* @return Request& Returned for chaining.
* @return A response object.
*/
Response put(std::string const& url);

/**
* @brief Finishes and performs the request as a post request.
*
* @param url The target to send this request to.
* @return Request& Returned for chaining.
* @return A response object.
*/
Response post(std::string const& url);

/**
* @brief Finishes and performs the request as a delete request.
*
* @param url The target to send this request to.
* @return Request& Returned for chaining.
* @return A response object.
*/
Response delete_(std::string const& url);

/**
* @brief Finishes and performs the request as a options request.
*
* @param url The target to send this request to.
* @return Request& Returned for chaining.
* @return A response object.
*/
Response options(std::string const& url);

/**
* @brief Finishes and performs the request as a head request.
*
* @param url The target to send this request to.
* @return Request& Returned for chaining.
* @return A response object.
*/
Response head(std::string const& url);

/**
* @brief Finishes and performs the request as a patch request.
*
* @param url The target to send this request to.
* @return Request& Returned for chaining.
* @return A response object.
*/
Response patch(std::string const& url);

/**
* @brief For requests outside the common verbs. (Or for put requests without body).
*
* @param request A custom request verb
* @param url The target to send this request to.
* @return A response object.
*/
Response custom(std::string const& request, std::string const& url);

/**
* @brief Returns the underlying curl instance.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/roar/curl/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ namespace Roar::Curl
this->url(url);
return perform();
}
Response Request::custom(std::string const& request, std::string const& url)
{
check(curl_easy_setopt(instance_, CURLOPT_CUSTOMREQUEST, request.c_str()));
this->url(url);
return perform();
}
Request& Request::verbose(bool enable)
{
check(curl_easy_setopt(instance_, CURLOPT_VERBOSE, enable ? 1L : 0L));
Expand Down
13 changes: 13 additions & 0 deletions test/curl/test_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,17 @@ namespace Roar::Tests
EXPECT_GE(headers.size(), 1);
EXPECT_EQ(headers["Content-Type"], "text/plain");
}

TEST_F(CurlRequestTests, CanPerformCustomRequest)
{
std::string body;
const auto result = Request{}.sink(body).custom("GET", url("/index.txt"));
EXPECT_EQ(body, "Hello");
}

TEST_F(CurlRequestTests, CanPerformBodylessPutViaCustomRequest)
{
const auto result = Request{}.custom("PUT", url("/putHereNothing"));
EXPECT_EQ(result.code(), boost::beast::http::status::no_content);
}
}
7 changes: 7 additions & 0 deletions test/util/common_listeners.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace Roar::Tests
ROAR_GET(ab)("/a/b");
ROAR_GET(anything)(R"(\/([^\/]+)\/(.+))"_rgx);
ROAR_PUT(putHere)("/putHere");
ROAR_PUT(putHereNothing)("/putHereNothing");
ROAR_POST(postHere)("/postHere");
ROAR_DELETE(deleteHere)("/deleteHere");
ROAR_OPTIONS(optionsHere)("/optionsHere");
Expand Down Expand Up @@ -92,6 +93,7 @@ namespace Roar::Tests
(),
(roar_index,
roar_putHere,
roar_putHereNothing,
roar_postHere,
roar_deleteHere,
roar_optionsHere,
Expand Down Expand Up @@ -128,6 +130,11 @@ namespace Roar::Tests
.commit();
});
}
inline void SimpleRoutes::putHereNothing(Session& session, EmptyBodyRequest&& req)
{
using namespace boost::beast::http;
session.send<empty_body>(req)->contentType("text/plain").status(status::no_content).commit();
}
inline void SimpleRoutes::postHere(Session& session, EmptyBodyRequest&& req)
{
return putHere(session, std::move(req));
Expand Down

0 comments on commit 601eda9

Please sign in to comment.