mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2025-12-13 03:29:37 +07:00
rename methods to http_method, statuses to req_status
This commit is contained in:
@@ -67,7 +67,7 @@ net::perform();
|
||||
```cpp
|
||||
// makes a GET request and async send it
|
||||
auto request = net::request_builder()
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.url("http://www.httpbin.org/get")
|
||||
.send();
|
||||
|
||||
@@ -97,7 +97,7 @@ std::cout << "Body content: " << response.content.as_string_view() << std::endl;
|
||||
|
||||
```cpp
|
||||
auto request = net::request_builder()
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.url("http://www.httpbin.org/post")
|
||||
.header("Content-Type", "application/json")
|
||||
.content(R"({"hello" : "world"})")
|
||||
@@ -217,7 +217,7 @@ private:
|
||||
};
|
||||
|
||||
net::request_builder()
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.url("https://httpbin.org/anything")
|
||||
.uploader<file_uploader>("image.jpeg")
|
||||
.send().wait();
|
||||
|
||||
@@ -39,11 +39,11 @@ namespace curly_hpp
|
||||
using time_ms_t = std::chrono::milliseconds;
|
||||
using time_point_t = std::chrono::steady_clock::time_point;
|
||||
|
||||
enum class methods {
|
||||
put,
|
||||
get,
|
||||
head,
|
||||
post
|
||||
enum class http_method {
|
||||
PUT,
|
||||
GET,
|
||||
HEAD,
|
||||
POST
|
||||
};
|
||||
|
||||
class upload_handler {
|
||||
@@ -173,16 +173,16 @@ namespace curly_hpp
|
||||
|
||||
namespace curly_hpp
|
||||
{
|
||||
enum class req_status {
|
||||
done,
|
||||
empty,
|
||||
failed,
|
||||
timeout,
|
||||
pending,
|
||||
canceled
|
||||
};
|
||||
|
||||
class request final {
|
||||
public:
|
||||
enum class statuses {
|
||||
done,
|
||||
empty,
|
||||
failed,
|
||||
timeout,
|
||||
pending,
|
||||
canceled
|
||||
};
|
||||
public:
|
||||
class internal_state;
|
||||
using internal_state_ptr = std::shared_ptr<internal_state>;
|
||||
@@ -190,18 +190,18 @@ namespace curly_hpp
|
||||
request(internal_state_ptr);
|
||||
|
||||
bool cancel() noexcept;
|
||||
statuses status() const noexcept;
|
||||
req_status status() const noexcept;
|
||||
|
||||
bool is_done() const noexcept;
|
||||
bool is_pending() const noexcept;
|
||||
|
||||
statuses wait() const noexcept;
|
||||
statuses wait_for(time_ms_t ms) const noexcept;
|
||||
statuses wait_until(time_point_t tp) const noexcept;
|
||||
req_status wait() const noexcept;
|
||||
req_status wait_for(time_ms_t ms) const noexcept;
|
||||
req_status wait_until(time_point_t tp) const noexcept;
|
||||
|
||||
statuses wait_callback() const noexcept;
|
||||
statuses wait_callback_for(time_ms_t ms) const noexcept;
|
||||
statuses wait_callback_until(time_point_t tp) const noexcept;
|
||||
req_status wait_callback() const noexcept;
|
||||
req_status wait_callback_for(time_ms_t ms) const noexcept;
|
||||
req_status wait_callback_until(time_point_t tp) const noexcept;
|
||||
|
||||
response take();
|
||||
const std::string& get_error() const noexcept;
|
||||
@@ -223,12 +223,12 @@ namespace curly_hpp
|
||||
request_builder(const request_builder&) = delete;
|
||||
request_builder& operator=(const request_builder&) = delete;
|
||||
|
||||
explicit request_builder(methods m) noexcept;
|
||||
explicit request_builder(http_method m) noexcept;
|
||||
explicit request_builder(std::string u) noexcept;
|
||||
explicit request_builder(methods m, std::string u) noexcept;
|
||||
explicit request_builder(http_method m, std::string u) noexcept;
|
||||
|
||||
request_builder& url(std::string u) noexcept;
|
||||
request_builder& method(methods m) noexcept;
|
||||
request_builder& method(http_method m) noexcept;
|
||||
request_builder& header(std::string key, std::string value);
|
||||
|
||||
request_builder& verbose(bool v) noexcept;
|
||||
@@ -245,7 +245,7 @@ namespace curly_hpp
|
||||
request_builder& downloader(downloader_uptr d) noexcept;
|
||||
|
||||
const std::string& url() const noexcept;
|
||||
methods method() const noexcept;
|
||||
http_method method() const noexcept;
|
||||
const headers_t& headers() const noexcept;
|
||||
|
||||
bool verbose() const noexcept;
|
||||
@@ -288,7 +288,7 @@ namespace curly_hpp
|
||||
}
|
||||
private:
|
||||
std::string url_;
|
||||
methods method_{methods::get};
|
||||
http_method method_{http_method::GET};
|
||||
headers_t headers_;
|
||||
bool verbose_{false};
|
||||
bool verification_{false};
|
||||
|
||||
@@ -295,18 +295,18 @@ namespace curly_hpp
|
||||
curl_easy_setopt(curlh_.get(), CURLOPT_VERBOSE, breq_.verbose() ? 1l : 0l);
|
||||
|
||||
switch ( breq_.method() ) {
|
||||
case methods::put:
|
||||
case http_method::PUT:
|
||||
curl_easy_setopt(curlh_.get(), CURLOPT_UPLOAD, 1l);
|
||||
curl_easy_setopt(curlh_.get(), CURLOPT_INFILESIZE_LARGE,
|
||||
static_cast<curl_off_t>(breq_.uploader()->size()));
|
||||
break;
|
||||
case methods::get:
|
||||
case http_method::GET:
|
||||
curl_easy_setopt(curlh_.get(), CURLOPT_HTTPGET, 1l);
|
||||
break;
|
||||
case methods::head:
|
||||
case http_method::HEAD:
|
||||
curl_easy_setopt(curlh_.get(), CURLOPT_NOBODY, 1l);
|
||||
break;
|
||||
case methods::post:
|
||||
case http_method::POST:
|
||||
curl_easy_setopt(curlh_.get(), CURLOPT_POST, 1l);
|
||||
curl_easy_setopt(curlh_.get(), CURLOPT_POSTFIELDSIZE_LARGE,
|
||||
static_cast<curl_off_t>(breq_.uploader()->size()));
|
||||
@@ -355,7 +355,7 @@ namespace curly_hpp
|
||||
|
||||
bool done() noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
if ( status_ != statuses::pending ) {
|
||||
if ( status_ != req_status::pending ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -365,7 +365,7 @@ namespace curly_hpp
|
||||
CURLINFO_RESPONSE_CODE,
|
||||
&http_code) || !http_code )
|
||||
{
|
||||
status_ = statuses::failed;
|
||||
status_ = req_status::failed;
|
||||
cvar_.notify_all();
|
||||
return false;
|
||||
}
|
||||
@@ -377,12 +377,12 @@ namespace curly_hpp
|
||||
response_.uploader = std::move(breq_.uploader());
|
||||
response_.downloader = std::move(breq_.downloader());
|
||||
} catch (...) {
|
||||
status_ = statuses::failed;
|
||||
status_ = req_status::failed;
|
||||
cvar_.notify_all();
|
||||
return false;
|
||||
}
|
||||
|
||||
status_ = statuses::done;
|
||||
status_ = req_status::done;
|
||||
error_.clear();
|
||||
|
||||
cvar_.notify_all();
|
||||
@@ -391,21 +391,21 @@ namespace curly_hpp
|
||||
|
||||
bool fail(CURLcode err) noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
if ( status_ != statuses::pending ) {
|
||||
if ( status_ != req_status::pending ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ( err ) {
|
||||
case CURLE_OPERATION_TIMEDOUT:
|
||||
status_ = statuses::timeout;
|
||||
status_ = req_status::timeout;
|
||||
break;
|
||||
case CURLE_READ_ERROR:
|
||||
case CURLE_WRITE_ERROR:
|
||||
case CURLE_ABORTED_BY_CALLBACK:
|
||||
status_ = statuses::canceled;
|
||||
status_ = req_status::canceled;
|
||||
break;
|
||||
default:
|
||||
status_ = statuses::failed;
|
||||
status_ = req_status::failed;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -421,54 +421,54 @@ namespace curly_hpp
|
||||
|
||||
bool cancel() noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
if ( status_ != statuses::pending ) {
|
||||
if ( status_ != req_status::pending ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
status_ = statuses::canceled;
|
||||
status_ = req_status::canceled;
|
||||
error_.clear();
|
||||
|
||||
cvar_.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
statuses status() const noexcept {
|
||||
req_status status() const noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
return status_;
|
||||
}
|
||||
|
||||
bool is_done() const noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
return status_ == statuses::done;
|
||||
return status_ == req_status::done;
|
||||
}
|
||||
|
||||
bool is_pending() const noexcept {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
return status_ == statuses::pending;
|
||||
return status_ == req_status::pending;
|
||||
}
|
||||
|
||||
statuses wait(bool wait_callback) const noexcept {
|
||||
req_status wait(bool wait_callback) const noexcept {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cvar_.wait(lock, [this, wait_callback](){
|
||||
return (status_ != statuses::pending)
|
||||
return (status_ != req_status::pending)
|
||||
&& (!wait_callback || callbacked_);
|
||||
});
|
||||
return status_;
|
||||
}
|
||||
|
||||
statuses wait_for(time_ms_t ms, bool wait_callback) const noexcept {
|
||||
req_status wait_for(time_ms_t ms, bool wait_callback) const noexcept {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cvar_.wait_for(lock, ms, [this, wait_callback](){
|
||||
return (status_ != statuses::pending)
|
||||
return (status_ != req_status::pending)
|
||||
&& (!wait_callback || callbacked_);
|
||||
});
|
||||
return status_;
|
||||
}
|
||||
|
||||
statuses wait_until(time_point_t tp, bool wait_callback) const noexcept {
|
||||
req_status wait_until(time_point_t tp, bool wait_callback) const noexcept {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cvar_.wait_until(lock, tp, [this, wait_callback](){
|
||||
return (status_ != statuses::pending)
|
||||
return (status_ != req_status::pending)
|
||||
&& (!wait_callback || callbacked_);
|
||||
});
|
||||
return status_;
|
||||
@@ -477,19 +477,19 @@ namespace curly_hpp
|
||||
response take() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cvar_.wait(lock, [this](){
|
||||
return status_ != statuses::pending;
|
||||
return status_ != req_status::pending;
|
||||
});
|
||||
if ( status_ != statuses::done ) {
|
||||
if ( status_ != req_status::done ) {
|
||||
throw exception("curly_hpp: response is unavailable");
|
||||
}
|
||||
status_ = statuses::empty;
|
||||
status_ = req_status::empty;
|
||||
return std::move(response_);
|
||||
}
|
||||
|
||||
const std::string& get_error() const noexcept {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cvar_.wait(lock, [this](){
|
||||
return status_ != statuses::pending;
|
||||
return status_ != req_status::pending;
|
||||
});
|
||||
return error_;
|
||||
}
|
||||
@@ -513,7 +513,7 @@ namespace curly_hpp
|
||||
callback_exception_ = std::current_exception();
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
assert(!callbacked_ && status_ != statuses::pending);
|
||||
assert(!callbacked_ && status_ != req_status::pending);
|
||||
callbacked_ = true;
|
||||
cvar_.notify_all();
|
||||
}
|
||||
@@ -615,7 +615,7 @@ namespace curly_hpp
|
||||
bool callbacked_{false};
|
||||
std::exception_ptr callback_exception_{nullptr};
|
||||
private:
|
||||
statuses status_{statuses::pending};
|
||||
req_status status_{req_status::pending};
|
||||
std::string error_{"Unknown error"};
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
@@ -634,7 +634,7 @@ namespace curly_hpp
|
||||
return state_->cancel();
|
||||
}
|
||||
|
||||
request::statuses request::status() const noexcept {
|
||||
req_status request::status() const noexcept {
|
||||
return state_->status();
|
||||
}
|
||||
|
||||
@@ -646,27 +646,27 @@ namespace curly_hpp
|
||||
return state_->is_pending();
|
||||
}
|
||||
|
||||
request::statuses request::wait() const noexcept {
|
||||
req_status request::wait() const noexcept {
|
||||
return state_->wait(false);
|
||||
}
|
||||
|
||||
request::statuses request::wait_for(time_ms_t ms) const noexcept {
|
||||
req_status request::wait_for(time_ms_t ms) const noexcept {
|
||||
return state_->wait_for(ms, false);
|
||||
}
|
||||
|
||||
request::statuses request::wait_until(time_point_t tp) const noexcept {
|
||||
req_status request::wait_until(time_point_t tp) const noexcept {
|
||||
return state_->wait_until(tp, false);
|
||||
}
|
||||
|
||||
request::statuses request::wait_callback() const noexcept {
|
||||
req_status request::wait_callback() const noexcept {
|
||||
return state_->wait(true);
|
||||
}
|
||||
|
||||
request::statuses request::wait_callback_for(time_ms_t ms) const noexcept {
|
||||
req_status request::wait_callback_for(time_ms_t ms) const noexcept {
|
||||
return state_->wait_for(ms, true);
|
||||
}
|
||||
|
||||
request::statuses request::wait_callback_until(time_point_t tp) const noexcept {
|
||||
req_status request::wait_callback_until(time_point_t tp) const noexcept {
|
||||
return state_->wait_until(tp, true);
|
||||
}
|
||||
|
||||
@@ -691,13 +691,13 @@ namespace curly_hpp
|
||||
|
||||
namespace curly_hpp
|
||||
{
|
||||
request_builder::request_builder(methods m) noexcept
|
||||
request_builder::request_builder(http_method m) noexcept
|
||||
: method_(m) {}
|
||||
|
||||
request_builder::request_builder(std::string u) noexcept
|
||||
: url_(std::move(u)) {}
|
||||
|
||||
request_builder::request_builder(methods m, std::string u) noexcept
|
||||
request_builder::request_builder(http_method m, std::string u) noexcept
|
||||
: url_(std::move(u))
|
||||
, method_(m) {}
|
||||
|
||||
@@ -706,7 +706,7 @@ namespace curly_hpp
|
||||
return *this;
|
||||
}
|
||||
|
||||
request_builder& request_builder::method(methods m) noexcept {
|
||||
request_builder& request_builder::method(http_method m) noexcept {
|
||||
method_ = m;
|
||||
return *this;
|
||||
}
|
||||
@@ -775,7 +775,7 @@ namespace curly_hpp
|
||||
return url_;
|
||||
}
|
||||
|
||||
methods request_builder::method() const noexcept {
|
||||
http_method request_builder::method() const noexcept {
|
||||
return method_;
|
||||
}
|
||||
|
||||
|
||||
@@ -86,33 +86,33 @@ TEST_CASE("curly") {
|
||||
SECTION("wait") {
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/delay/1").send();
|
||||
REQUIRE(req.status() == net::request::statuses::pending);
|
||||
REQUIRE(req.wait() == net::request::statuses::done);
|
||||
REQUIRE(req.status() == net::request::statuses::done);
|
||||
REQUIRE(req.status() == net::req_status::pending);
|
||||
REQUIRE(req.wait() == net::req_status::done);
|
||||
REQUIRE(req.status() == net::req_status::done);
|
||||
auto resp = req.take();
|
||||
REQUIRE(resp.http_code() == 200u);
|
||||
REQUIRE(req.status() == net::request::statuses::empty);
|
||||
REQUIRE(req.status() == net::req_status::empty);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/delay/2").send();
|
||||
REQUIRE(req.wait_for(net::time_sec_t(1)) == net::request::statuses::pending);
|
||||
REQUIRE(req.wait_for(net::time_sec_t(5)) == net::request::statuses::done);
|
||||
REQUIRE(req.wait_for(net::time_sec_t(1)) == net::req_status::pending);
|
||||
REQUIRE(req.wait_for(net::time_sec_t(5)) == net::req_status::done);
|
||||
REQUIRE(req.take().http_code() == 200u);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/delay/2").send();
|
||||
REQUIRE(req.wait_until(net::time_point_t::clock::now() + net::time_sec_t(1))
|
||||
== net::request::statuses::pending);
|
||||
== net::req_status::pending);
|
||||
REQUIRE(req.wait_until(net::time_point_t::clock::now() + net::time_sec_t(5))
|
||||
== net::request::statuses::done);
|
||||
== net::req_status::done);
|
||||
REQUIRE(req.take().http_code() == 200u);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("error") {
|
||||
auto req = net::request_builder("|||").send();
|
||||
REQUIRE(req.wait() == net::request::statuses::failed);
|
||||
REQUIRE(req.status() == net::request::statuses::failed);
|
||||
REQUIRE(req.wait() == net::req_status::failed);
|
||||
REQUIRE(req.status() == net::req_status::failed);
|
||||
REQUIRE_FALSE(req.get_error().empty());
|
||||
}
|
||||
|
||||
@@ -120,21 +120,21 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/delay/1").send();
|
||||
REQUIRE(req.cancel());
|
||||
REQUIRE(req.status() == net::request::statuses::canceled);
|
||||
REQUIRE(req.status() == net::req_status::canceled);
|
||||
REQUIRE(req.get_error().empty());
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/status/200").send();
|
||||
REQUIRE(req.wait() == net::request::statuses::done);
|
||||
REQUIRE(req.wait() == net::req_status::done);
|
||||
REQUIRE_FALSE(req.cancel());
|
||||
REQUIRE(req.status() == net::request::statuses::done);
|
||||
REQUIRE(req.status() == net::req_status::done);
|
||||
REQUIRE(req.get_error().empty());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("is_done/is_pending") {
|
||||
{
|
||||
auto req = net::request_builder(net::methods::get)
|
||||
auto req = net::request_builder(net::http_method::GET)
|
||||
.url("https://httpbin.org/delay/1")
|
||||
.send();
|
||||
REQUIRE_FALSE(req.is_done());
|
||||
@@ -144,7 +144,7 @@ TEST_CASE("curly") {
|
||||
REQUIRE_FALSE(req.is_pending());
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder(net::methods::post, "http://www.httpbin.org/post")
|
||||
auto req = net::request_builder(net::http_method::POST, "http://www.httpbin.org/post")
|
||||
.url("https://httpbin.org/delay/2")
|
||||
.request_timeout(net::time_sec_t(1))
|
||||
.send();
|
||||
@@ -161,22 +161,22 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/status/204").send();
|
||||
auto resp = req.take();
|
||||
REQUIRE(req.status() == net::request::statuses::empty);
|
||||
REQUIRE(req.status() == net::req_status::empty);
|
||||
REQUIRE(resp.http_code() == 204u);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/delay/2").send();
|
||||
REQUIRE(req.cancel());
|
||||
REQUIRE_THROWS_AS(req.take(), net::exception);
|
||||
REQUIRE(req.status() == net::request::statuses::canceled);
|
||||
REQUIRE(req.status() == net::req_status::canceled);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/delay/2")
|
||||
.response_timeout(net::time_sec_t(0))
|
||||
.send();
|
||||
REQUIRE(req.wait() == net::request::statuses::timeout);
|
||||
REQUIRE(req.wait() == net::req_status::timeout);
|
||||
REQUIRE_THROWS_AS(req.take(), net::exception);
|
||||
REQUIRE(req.status() == net::request::statuses::timeout);
|
||||
REQUIRE(req.status() == net::req_status::timeout);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,75 +184,75 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req0 = net::request_builder()
|
||||
.url("https://httpbin.org/put")
|
||||
.method(net::methods::put)
|
||||
.method(net::http_method::PUT)
|
||||
.send();
|
||||
REQUIRE(req0.take().http_code() == 200u);
|
||||
|
||||
auto req1 = net::request_builder()
|
||||
.url("https://httpbin.org/put")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
REQUIRE(req1.take().http_code() == 405u);
|
||||
|
||||
auto req2 = net::request_builder()
|
||||
.url("https://httpbin.org/put")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.send();
|
||||
REQUIRE(req2.take().http_code() == 405u);
|
||||
|
||||
auto req3 = net::request_builder()
|
||||
.url("https://httpbin.org/put")
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.send();
|
||||
REQUIRE(req3.take().http_code() == 405u);
|
||||
}
|
||||
{
|
||||
auto req0 = net::request_builder()
|
||||
.url("https://httpbin.org/get")
|
||||
.method(net::methods::put)
|
||||
.method(net::http_method::PUT)
|
||||
.send();
|
||||
REQUIRE(req0.take().http_code() == 405u);
|
||||
|
||||
auto req1 = net::request_builder()
|
||||
.url("https://httpbin.org/get")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
REQUIRE(req1.take().http_code() == 200u);
|
||||
|
||||
auto req2 = net::request_builder()
|
||||
.url("https://httpbin.org/get")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.send();
|
||||
REQUIRE(req2.take().http_code() == 200u);
|
||||
|
||||
auto req3 = net::request_builder()
|
||||
.url("https://httpbin.org/get")
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.send();
|
||||
REQUIRE(req3.take().http_code() == 405u);
|
||||
}
|
||||
{
|
||||
auto req0 = net::request_builder()
|
||||
.url("https://httpbin.org/post")
|
||||
.method(net::methods::put)
|
||||
.method(net::http_method::PUT)
|
||||
.send();
|
||||
REQUIRE(req0.take().http_code() == 405u);
|
||||
|
||||
auto req1 = net::request_builder()
|
||||
.url("https://httpbin.org/post")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
REQUIRE(req1.take().http_code() == 405u);
|
||||
|
||||
auto req2 = net::request_builder()
|
||||
.url("https://httpbin.org/post")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.send();
|
||||
REQUIRE(req2.take().http_code() == 405u);
|
||||
|
||||
auto req3 = net::request_builder()
|
||||
.url("https://httpbin.org/post")
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.send();
|
||||
REQUIRE(req3.take().http_code() == 200u);
|
||||
}
|
||||
@@ -262,28 +262,28 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/status/200")
|
||||
.method(net::methods::put)
|
||||
.method(net::http_method::PUT)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 200u);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/status/201")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 201u);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/status/202")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 202u);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/status/203")
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 203u);
|
||||
}
|
||||
@@ -307,7 +307,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/response-headers?hello=world&world=hello")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
const auto resp = req.take();
|
||||
const auto content_j = json_parse(resp.content.as_string_view());
|
||||
@@ -317,7 +317,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/response-headers?hello=world&world=hello")
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.send();
|
||||
const auto resp = req.take();
|
||||
const auto content_j = json_parse(resp.content.as_string_copy());
|
||||
@@ -340,14 +340,14 @@ TEST_CASE("curly") {
|
||||
.url("https://httpbin.org/delay/10")
|
||||
.request_timeout(net::time_sec_t(0))
|
||||
.send();
|
||||
REQUIRE(req0.wait() == net::request::statuses::timeout);
|
||||
REQUIRE(req0.wait() == net::req_status::timeout);
|
||||
REQUIRE_FALSE(req0.get_error().empty());
|
||||
|
||||
auto req1 = net::request_builder()
|
||||
.url("https://httpbin.org/delay/10")
|
||||
.response_timeout(net::time_sec_t(0))
|
||||
.send();
|
||||
REQUIRE(req1.wait() == net::request::statuses::timeout);
|
||||
REQUIRE(req1.wait() == net::req_status::timeout);
|
||||
REQUIRE_FALSE(req1.get_error().empty());
|
||||
}
|
||||
{
|
||||
@@ -355,14 +355,14 @@ TEST_CASE("curly") {
|
||||
.url("https://httpbin.org/delay/10")
|
||||
.request_timeout(net::time_sec_t(1))
|
||||
.send();
|
||||
REQUIRE(req0.wait() == net::request::statuses::timeout);
|
||||
REQUIRE(req0.wait() == net::req_status::timeout);
|
||||
REQUIRE_FALSE(req0.get_error().empty());
|
||||
|
||||
auto req1 = net::request_builder()
|
||||
.url("https://httpbin.org/delay/10")
|
||||
.response_timeout(net::time_sec_t(1))
|
||||
.send();
|
||||
REQUIRE(req1.wait() == net::request::statuses::timeout);
|
||||
REQUIRE(req1.wait() == net::req_status::timeout);
|
||||
REQUIRE_FALSE(req1.get_error().empty());
|
||||
}
|
||||
}
|
||||
@@ -371,7 +371,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto resp = net::request_builder()
|
||||
.url("https://httpbin.org/image/png")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send().take();
|
||||
REQUIRE(resp.http_code() == 200u);
|
||||
REQUIRE(resp.headers.count("Content-Type"));
|
||||
@@ -384,7 +384,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto resp = net::request_builder()
|
||||
.url("https://httpbin.org/image/jpeg")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send().take();
|
||||
REQUIRE(resp.http_code() == 200u);
|
||||
REQUIRE(resp.headers.count("Content-Type"));
|
||||
@@ -401,21 +401,21 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/redirect/2")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 200u);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/absolute-redirect/2")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 200u);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/relative-redirect/2")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 200u);
|
||||
}
|
||||
@@ -424,7 +424,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/redirect/3")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.redirections(0)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 302u);
|
||||
@@ -432,23 +432,23 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/redirect/3")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.redirections(1)
|
||||
.send();
|
||||
REQUIRE(req.wait() == net::request::statuses::failed);
|
||||
REQUIRE(req.wait() == net::req_status::failed);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/redirect/3")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.redirections(2)
|
||||
.send();
|
||||
REQUIRE(req.wait() == net::request::statuses::failed);
|
||||
REQUIRE(req.wait() == net::req_status::failed);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder()
|
||||
.url("https://httpbin.org/redirect/3")
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.redirections(3)
|
||||
.send();
|
||||
REQUIRE(req.take().http_code() == 200u);
|
||||
@@ -460,7 +460,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto resp = net::request_builder()
|
||||
.url("https://httpbin.org/anything")
|
||||
.method(net::methods::put)
|
||||
.method(net::http_method::PUT)
|
||||
.header("Content-Type", "application/json")
|
||||
.content(R"({"hello":"world"})")
|
||||
.send().take();
|
||||
@@ -470,7 +470,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto resp = net::request_builder()
|
||||
.url("https://httpbin.org/anything")
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.header("Content-Type", "application/json")
|
||||
.content(R"({"hello":"world"})")
|
||||
.send().take();
|
||||
@@ -480,7 +480,7 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto resp = net::request_builder()
|
||||
.url("https://httpbin.org/anything")
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.content("hello=world&world=hello")
|
||||
.send().take();
|
||||
@@ -493,53 +493,53 @@ TEST_CASE("curly") {
|
||||
SECTION("ssl_verification") {
|
||||
{
|
||||
auto req0 = net::request_builder("https://expired.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(true)
|
||||
.send();
|
||||
REQUIRE(req0.wait() == net::request::statuses::failed);
|
||||
REQUIRE(req0.wait() == net::req_status::failed);
|
||||
|
||||
auto req1 = net::request_builder("https://wrong.host.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(true)
|
||||
.send();
|
||||
REQUIRE(req1.wait() == net::request::statuses::failed);
|
||||
REQUIRE(req1.wait() == net::req_status::failed);
|
||||
|
||||
auto req2 = net::request_builder("https://self-signed.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(true)
|
||||
.send();
|
||||
REQUIRE(req2.wait() == net::request::statuses::failed);
|
||||
REQUIRE(req2.wait() == net::req_status::failed);
|
||||
|
||||
auto req3 = net::request_builder("https://untrusted-root.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(true)
|
||||
.send();
|
||||
REQUIRE(req3.wait() == net::request::statuses::failed);
|
||||
REQUIRE(req3.wait() == net::req_status::failed);
|
||||
}
|
||||
{
|
||||
auto req0 = net::request_builder("https://expired.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(false)
|
||||
.send();
|
||||
REQUIRE(req0.wait() == net::request::statuses::done);
|
||||
REQUIRE(req0.wait() == net::req_status::done);
|
||||
|
||||
auto req1 = net::request_builder("https://wrong.host.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(false)
|
||||
.send();
|
||||
REQUIRE(req1.wait() == net::request::statuses::done);
|
||||
REQUIRE(req1.wait() == net::req_status::done);
|
||||
|
||||
auto req2 = net::request_builder("https://self-signed.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(false)
|
||||
.send();
|
||||
REQUIRE(req2.wait() == net::request::statuses::done);
|
||||
REQUIRE(req2.wait() == net::req_status::done);
|
||||
|
||||
auto req3 = net::request_builder("https://untrusted-root.badssl.com")
|
||||
.method(net::methods::head)
|
||||
.method(net::http_method::HEAD)
|
||||
.verification(false)
|
||||
.send();
|
||||
REQUIRE(req3.wait() == net::request::statuses::done);
|
||||
REQUIRE(req3.wait() == net::req_status::done);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -547,18 +547,18 @@ TEST_CASE("curly") {
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/anything")
|
||||
.verbose(true)
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.uploader<canceled_uploader>()
|
||||
.send();
|
||||
REQUIRE(req.wait() == net::request::statuses::canceled);
|
||||
REQUIRE(req.wait() == net::req_status::canceled);
|
||||
}
|
||||
{
|
||||
auto req = net::request_builder("https://httpbin.org/anything")
|
||||
.verbose(true)
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.downloader<canceled_downloader>()
|
||||
.send();
|
||||
REQUIRE(req.wait() == net::request::statuses::canceled);
|
||||
REQUIRE(req.wait() == net::req_status::canceled);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -570,10 +570,10 @@ TEST_CASE("curly") {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
++call_once;
|
||||
REQUIRE(request.is_done());
|
||||
REQUIRE(request.status() == net::request::statuses::done);
|
||||
REQUIRE(request.status() == net::req_status::done);
|
||||
REQUIRE(request.take().http_code() == 200u);
|
||||
}).send();
|
||||
REQUIRE(req.wait_callback() == net::request::statuses::empty);
|
||||
REQUIRE(req.wait_callback() == net::req_status::empty);
|
||||
REQUIRE_FALSE(req.get_callback_exception());
|
||||
REQUIRE(call_once.load() == 1u);
|
||||
}
|
||||
@@ -584,10 +584,10 @@ TEST_CASE("curly") {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
++call_once;
|
||||
REQUIRE_FALSE(request.is_done());
|
||||
REQUIRE(request.status() == net::request::statuses::failed);
|
||||
REQUIRE(request.status() == net::req_status::failed);
|
||||
REQUIRE_FALSE(request.get_error().empty());
|
||||
}).send();
|
||||
REQUIRE(req.wait_callback() == net::request::statuses::failed);
|
||||
REQUIRE(req.wait_callback() == net::req_status::failed);
|
||||
REQUIRE_FALSE(req.get_callback_exception());
|
||||
REQUIRE(call_once.load() == 1u);
|
||||
}
|
||||
@@ -599,10 +599,10 @@ TEST_CASE("curly") {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
++call_once;
|
||||
REQUIRE_FALSE(request.is_done());
|
||||
REQUIRE(request.status() == net::request::statuses::timeout);
|
||||
REQUIRE(request.status() == net::req_status::timeout);
|
||||
REQUIRE_FALSE(request.get_error().empty());
|
||||
}).send();
|
||||
REQUIRE(req.wait_callback() == net::request::statuses::timeout);
|
||||
REQUIRE(req.wait_callback() == net::req_status::timeout);
|
||||
REQUIRE_FALSE(req.get_callback_exception());
|
||||
REQUIRE(call_once.load() == 1u);
|
||||
}
|
||||
@@ -613,11 +613,11 @@ TEST_CASE("curly") {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
++call_once;
|
||||
REQUIRE_FALSE(request.is_done());
|
||||
REQUIRE(request.status() == net::request::statuses::canceled);
|
||||
REQUIRE(request.status() == net::req_status::canceled);
|
||||
REQUIRE(request.get_error().empty());
|
||||
}).send();
|
||||
REQUIRE(req.cancel());
|
||||
REQUIRE(req.wait_callback() == net::request::statuses::canceled);
|
||||
REQUIRE(req.wait_callback() == net::req_status::canceled);
|
||||
REQUIRE_FALSE(req.get_callback_exception());
|
||||
REQUIRE(call_once.load() == 1u);
|
||||
}
|
||||
@@ -631,7 +631,7 @@ TEST_CASE("curly") {
|
||||
throw std::logic_error("my_logic_error");
|
||||
}
|
||||
}).send();
|
||||
REQUIRE(req.wait_callback() == net::request::statuses::empty);
|
||||
REQUIRE(req.wait_callback() == net::req_status::empty);
|
||||
REQUIRE(req.get_callback_exception());
|
||||
try {
|
||||
std::rethrow_exception(req.get_callback_exception());
|
||||
@@ -647,7 +647,7 @@ TEST_CASE("curly_examples") {
|
||||
SECTION("Get Requests") {
|
||||
// makes a GET request and async send it
|
||||
auto request = net::request_builder()
|
||||
.method(net::methods::get)
|
||||
.method(net::http_method::GET)
|
||||
.url("http://www.httpbin.org/get")
|
||||
.send();
|
||||
|
||||
@@ -675,7 +675,7 @@ TEST_CASE("curly_examples") {
|
||||
|
||||
SECTION("Post Requests") {
|
||||
auto request = net::request_builder()
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.url("http://www.httpbin.org/post")
|
||||
.header("Content-Type", "application/json")
|
||||
.content(R"({"hello" : "world"})")
|
||||
@@ -785,7 +785,7 @@ TEST_CASE("curly_examples") {
|
||||
};
|
||||
|
||||
net::request_builder()
|
||||
.method(net::methods::post)
|
||||
.method(net::http_method::POST)
|
||||
.url("https://httpbin.org/anything")
|
||||
.uploader<file_uploader>("image.jpeg")
|
||||
.send().take();
|
||||
|
||||
Reference in New Issue
Block a user