mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2025-12-16 14:11:17 +07:00
more unit tests
This commit is contained in:
@@ -390,9 +390,19 @@ namespace curly_hpp
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
status_ = (err == CURLE_OPERATION_TIMEDOUT)
|
switch ( err ) {
|
||||||
? statuses::timeout
|
case CURLE_OPERATION_TIMEDOUT:
|
||||||
: statuses::failed;
|
status_ = statuses::timeout;
|
||||||
|
break;
|
||||||
|
case CURLE_READ_ERROR:
|
||||||
|
case CURLE_WRITE_ERROR:
|
||||||
|
case CURLE_ABORTED_BY_CALLBACK:
|
||||||
|
status_ = statuses::canceled;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
status_ = statuses::failed;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
error_ = curl_easy_strerror(err);
|
error_ = curl_easy_strerror(err);
|
||||||
|
|||||||
@@ -5,5 +5,4 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#define CATCH_CONFIG_MAIN
|
#define CATCH_CONFIG_MAIN
|
||||||
#define CATCH_CONFIG_FAST_COMPILE
|
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
* Copyright (C) 2019, by Matvey Cherevko (blackmatov@gmail.com)
|
* Copyright (C) 2019, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#define CATCH_CONFIG_FAST_COMPILE
|
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <utility>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
@@ -29,29 +29,29 @@ namespace
|
|||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
class verbose_uploader : public net::upload_handler {
|
class canceled_uploader : public net::upload_handler {
|
||||||
public:
|
public:
|
||||||
verbose_uploader() = default;
|
canceled_uploader() = default;
|
||||||
|
|
||||||
std::size_t size() const override {
|
std::size_t size() const override {
|
||||||
return 0;
|
return 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t read(char* dst, std::size_t size) override {
|
std::size_t read(char* dst, std::size_t size) override {
|
||||||
(void)dst;
|
(void)dst;
|
||||||
std::cout << "---------- ** UPLOAD (" << size << ") ** ---------- " << std::endl;
|
(void)size;
|
||||||
return size;
|
throw std::exception();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class verbose_downloader : public net::download_handler {
|
class canceled_downloader : public net::download_handler {
|
||||||
public:
|
public:
|
||||||
verbose_downloader() = default;
|
canceled_downloader() = default;
|
||||||
|
|
||||||
std::size_t write(const char* src, std::size_t size) override {
|
std::size_t write(const char* src, std::size_t size) override {
|
||||||
(void)src;
|
(void)src;
|
||||||
std::cout << "---------- ** DOWNLOAD (" << size << ") ** ---------- " << std::endl;
|
(void)size;
|
||||||
return size;
|
throw std::exception();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -229,11 +229,13 @@ TEST_CASE("curly") {
|
|||||||
.url("https://httpbin.org/headers")
|
.url("https://httpbin.org/headers")
|
||||||
.header("Custom-Header-1", "custom_header_value_1")
|
.header("Custom-Header-1", "custom_header_value_1")
|
||||||
.header("Custom-Header-2", "custom header value 2")
|
.header("Custom-Header-2", "custom header value 2")
|
||||||
|
.header("Custom-Header-3", std::string())
|
||||||
.send();
|
.send();
|
||||||
const auto resp = req.get();
|
const auto resp = req.get();
|
||||||
const auto content_j = json_parse(resp.content.as_string_view());
|
const auto content_j = json_parse(resp.content.as_string_view());
|
||||||
REQUIRE(content_j["headers"]["Custom-Header-1"] == "custom_header_value_1");
|
REQUIRE(content_j["headers"]["Custom-Header-1"] == "custom_header_value_1");
|
||||||
REQUIRE(content_j["headers"]["Custom-Header-2"] == "custom header value 2");
|
REQUIRE(content_j["headers"]["Custom-Header-2"] == "custom header value 2");
|
||||||
|
REQUIRE(content_j["headers"]["Custom-Header-3"] == "");
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("response_inspection") {
|
SECTION("response_inspection") {
|
||||||
@@ -253,7 +255,7 @@ TEST_CASE("curly") {
|
|||||||
.method(net::methods::post)
|
.method(net::methods::post)
|
||||||
.send();
|
.send();
|
||||||
const auto resp = req.get();
|
const auto resp = req.get();
|
||||||
const auto content_j = json_parse(resp.content.as_string_view());
|
const auto content_j = json_parse(resp.content.as_string_copy());
|
||||||
REQUIRE(content_j["hello"] == "world");
|
REQUIRE(content_j["hello"] == "world");
|
||||||
REQUIRE(content_j["world"] == "hello");
|
REQUIRE(content_j["world"] == "hello");
|
||||||
}
|
}
|
||||||
@@ -296,7 +298,9 @@ TEST_CASE("curly") {
|
|||||||
REQUIRE(resp.headers.count("Content-Type"));
|
REQUIRE(resp.headers.count("Content-Type"));
|
||||||
REQUIRE(resp.headers.at("Content-Type") == "image/png");
|
REQUIRE(resp.headers.at("Content-Type") == "image/png");
|
||||||
REQUIRE(untests::png_data_length == resp.content.size());
|
REQUIRE(untests::png_data_length == resp.content.size());
|
||||||
REQUIRE(!std::memcmp(resp.content.data().data(), untests::png_data, untests::png_data_length));
|
REQUIRE(!std::memcmp(
|
||||||
|
std::move(resp.content).data().data(),
|
||||||
|
untests::png_data, untests::png_data_length));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto resp = net::request_builder()
|
auto resp = net::request_builder()
|
||||||
@@ -307,31 +311,69 @@ TEST_CASE("curly") {
|
|||||||
REQUIRE(resp.headers.count("Content-Type"));
|
REQUIRE(resp.headers.count("Content-Type"));
|
||||||
REQUIRE(resp.headers.at("Content-Type") == "image/jpeg");
|
REQUIRE(resp.headers.at("Content-Type") == "image/jpeg");
|
||||||
REQUIRE(untests::jpeg_data_length == resp.content.size());
|
REQUIRE(untests::jpeg_data_length == resp.content.size());
|
||||||
REQUIRE(!std::memcmp(resp.content.data().data(), untests::jpeg_data, untests::jpeg_data_length));
|
REQUIRE(!std::memcmp(
|
||||||
|
std::as_const(resp.content).data().data(),
|
||||||
|
untests::jpeg_data, untests::jpeg_data_length));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("redirects") {
|
SECTION("redirects") {
|
||||||
{
|
{
|
||||||
auto req = net::request_builder()
|
{
|
||||||
.url("https://httpbin.org/redirect/2")
|
auto req = net::request_builder()
|
||||||
.method(net::methods::get)
|
.url("https://httpbin.org/redirect/2")
|
||||||
.send();
|
.method(net::methods::get)
|
||||||
REQUIRE(req.get().code() == 200u);
|
.send();
|
||||||
|
REQUIRE(req.get().code() == 200u);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto req = net::request_builder()
|
||||||
|
.url("https://httpbin.org/absolute-redirect/2")
|
||||||
|
.method(net::methods::get)
|
||||||
|
.send();
|
||||||
|
REQUIRE(req.get().code() == 200u);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto req = net::request_builder()
|
||||||
|
.url("https://httpbin.org/relative-redirect/2")
|
||||||
|
.method(net::methods::get)
|
||||||
|
.send();
|
||||||
|
REQUIRE(req.get().code() == 200u);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto req = net::request_builder()
|
{
|
||||||
.url("https://httpbin.org/absolute-redirect/2")
|
auto req = net::request_builder()
|
||||||
.method(net::methods::get)
|
.url("https://httpbin.org/redirect/3")
|
||||||
.send();
|
.method(net::methods::get)
|
||||||
REQUIRE(req.get().code() == 200u);
|
.redirections(0)
|
||||||
}
|
.send();
|
||||||
{
|
REQUIRE(req.get().code() == 302u);
|
||||||
auto req = net::request_builder()
|
}
|
||||||
.url("https://httpbin.org/relative-redirect/2")
|
{
|
||||||
.method(net::methods::get)
|
auto req = net::request_builder()
|
||||||
.send();
|
.url("https://httpbin.org/redirect/3")
|
||||||
REQUIRE(req.get().code() == 200u);
|
.method(net::methods::get)
|
||||||
|
.redirections(1)
|
||||||
|
.send();
|
||||||
|
REQUIRE(req.wait() == net::request::statuses::failed);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto req = net::request_builder()
|
||||||
|
.url("https://httpbin.org/redirect/3")
|
||||||
|
.method(net::methods::get)
|
||||||
|
.redirections(2)
|
||||||
|
.send();
|
||||||
|
REQUIRE(req.wait() == net::request::statuses::failed);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto req = net::request_builder()
|
||||||
|
.url("https://httpbin.org/redirect/3")
|
||||||
|
.method(net::methods::get)
|
||||||
|
.redirections(3)
|
||||||
|
.send();
|
||||||
|
REQUIRE(req.get().code() == 200u);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -421,6 +463,23 @@ TEST_CASE("curly") {
|
|||||||
REQUIRE(req3.wait() == net::request::statuses::done);
|
REQUIRE(req3.wait() == net::request::statuses::done);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("canceled_handlers") {
|
||||||
|
{
|
||||||
|
auto req = net::request_builder("https://httpbin.org/anything")
|
||||||
|
.method(net::methods::post)
|
||||||
|
.uploader<canceled_uploader>()
|
||||||
|
.send();
|
||||||
|
REQUIRE(req.wait() == net::request::statuses::canceled);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto req = net::request_builder("https://httpbin.org/anything")
|
||||||
|
.method(net::methods::get)
|
||||||
|
.downloader<canceled_downloader>()
|
||||||
|
.send();
|
||||||
|
REQUIRE(req.wait() == net::request::statuses::canceled);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("curly_examples") {
|
TEST_CASE("curly_examples") {
|
||||||
|
|||||||
Reference in New Issue
Block a user