From 601eda94a4a59308a1cb12ae3a554d0d359c6aad Mon Sep 17 00:00:00 2001 From: "Tim.Ebbeke" Date: Wed, 25 Oct 2023 14:18:36 +0200 Subject: [PATCH] Added custom curl request support. --- include/roar/curl/request.hpp | 23 ++++++++++++++++------- src/roar/curl/request.cpp | 6 ++++++ test/curl/test_request.hpp | 13 +++++++++++++ test/util/common_listeners.hpp | 7 +++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/include/roar/curl/request.hpp b/include/roar/curl/request.hpp index ae823962..4d439958 100644 --- a/include/roar/curl/request.hpp +++ b/include/roar/curl/request.hpp @@ -342,7 +342,7 @@ 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); @@ -350,7 +350,7 @@ namespace Roar::Curl * @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); @@ -358,7 +358,7 @@ namespace Roar::Curl * @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); @@ -366,7 +366,7 @@ namespace Roar::Curl * @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); @@ -374,7 +374,7 @@ namespace Roar::Curl * @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); @@ -382,7 +382,7 @@ namespace Roar::Curl * @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); @@ -390,10 +390,19 @@ namespace Roar::Curl * @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. */ diff --git a/src/roar/curl/request.cpp b/src/roar/curl/request.cpp index 34d0b9d4..c05a63f7 100644 --- a/src/roar/curl/request.cpp +++ b/src/roar/curl/request.cpp @@ -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)); diff --git a/test/curl/test_request.hpp b/test/curl/test_request.hpp index 31d576a3..adcc1865 100644 --- a/test/curl/test_request.hpp +++ b/test/curl/test_request.hpp @@ -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); + } } \ No newline at end of file diff --git a/test/util/common_listeners.hpp b/test/util/common_listeners.hpp index 0423f61a..87116af2 100644 --- a/test/util/common_listeners.hpp +++ b/test/util/common_listeners.hpp @@ -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"); @@ -92,6 +93,7 @@ namespace Roar::Tests (), (roar_index, roar_putHere, + roar_putHereNothing, roar_postHere, roar_deleteHere, roar_optionsHere, @@ -128,6 +130,11 @@ namespace Roar::Tests .commit(); }); } + inline void SimpleRoutes::putHereNothing(Session& session, EmptyBodyRequest&& req) + { + using namespace boost::beast::http; + session.send(req)->contentType("text/plain").status(status::no_content).commit(); + } inline void SimpleRoutes::postHere(Session& session, EmptyBodyRequest&& req) { return putHere(session, std::move(req));