diff --git a/untests/CMakeLists.txt b/untests/CMakeLists.txt index 31c8cc6..1ecfbf7 100644 --- a/untests/CMakeLists.txt +++ b/untests/CMakeLists.txt @@ -5,38 +5,6 @@ project(curly.hpp.untests) include(FetchContent) -# -# nlohmann/json -# - -set(JSON_BuildTests OFF CACHE BOOL "" FORCE) - -FetchContent_Declare( - nlohmann_json - GIT_REPOSITORY https://github.com/nlohmann/json) - -FetchContent_GetProperties(nlohmann_json) -if(NOT nlohmann_json_POPULATED) - FetchContent_Populate(nlohmann_json) - add_subdirectory(${nlohmann_json_SOURCE_DIR} ${nlohmann_json_BINARY_DIR}) -endif() - -# -# catchorg/catch2 -# - -set(CATCH_BUILD_TESTING OFF CACHE BOOL "" FORCE) - -FetchContent_Declare( - catchorg_catch2 - GIT_REPOSITORY https://github.com/catchorg/catch2) - -FetchContent_GetProperties(catchorg_catch2) -if(NOT catchorg_catch2_POPULATED) - FetchContent_Populate(catchorg_catch2) - add_subdirectory(${catchorg_catch2_SOURCE_DIR} ${catchorg_catch2_BINARY_DIR}) -endif() - # # coverage # @@ -55,9 +23,7 @@ endif() file(GLOB UNTESTS_SOURCES "*.cpp" "*.hpp") add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES}) - -target_link_libraries(${PROJECT_NAME} - nlohmann_json Catch2 curly.hpp) +target_link_libraries(${PROJECT_NAME} curly.hpp) target_compile_options(${PROJECT_NAME} PRIVATE @@ -68,3 +34,33 @@ target_compile_options(${PROJECT_NAME} -Wall -Wextra -Wpedantic>) add_test(${PROJECT_NAME} ${PROJECT_NAME}) + +# +# catchorg/catch2 +# + +FetchContent_Declare( + catchorg_catch2 + GIT_REPOSITORY https://github.com/catchorg/catch2) + +FetchContent_GetProperties(catchorg_catch2) +if(NOT catchorg_catch2_POPULATED) + FetchContent_Populate(catchorg_catch2) + target_include_directories(${PROJECT_NAME} + PRIVATE ${catchorg_catch2_SOURCE_DIR}/single_include) +endif() + +# +# tencent/rapidjson +# + +FetchContent_Declare( + tencent_rapidjson + GIT_REPOSITORY https://github.com/tencent/rapidjson) + +FetchContent_GetProperties(tencent_rapidjson) +if(NOT tencent_rapidjson_POPULATED) + FetchContent_Populate(tencent_rapidjson) + target_include_directories(${PROJECT_NAME} + PRIVATE ${tencent_rapidjson_SOURCE_DIR}/include) +endif() diff --git a/untests/curly_tests.cpp b/untests/curly_tests.cpp index 08e76e9..9c4458b 100644 --- a/untests/curly_tests.cpp +++ b/untests/curly_tests.cpp @@ -10,8 +10,8 @@ #include #include -#include -using json = nlohmann::json; +#include +namespace json = rapidjson; #include namespace net = curly_hpp; @@ -21,6 +21,14 @@ namespace net = curly_hpp; namespace { + json::Document json_parse(std::string_view data) { + json::Document d; + if ( d.Parse(data.data(), data.size()).HasParseError() ) { + throw std::logic_error("untests: failed to parse json"); + } + return d; + } + class verbose_uploader : public net::upload_handler { public: verbose_uploader() = default; @@ -223,7 +231,7 @@ TEST_CASE("curly") { .header("Custom-Header-2", "custom header value 2") .send(); 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-2"] == "custom header value 2"); } @@ -235,7 +243,7 @@ TEST_CASE("curly") { .method(net::methods::get) .send(); 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["hello"] == "world"); REQUIRE(content_j["world"] == "hello"); } @@ -245,7 +253,7 @@ TEST_CASE("curly") { .method(net::methods::post) .send(); 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["hello"] == "world"); REQUIRE(content_j["world"] == "hello"); } @@ -335,7 +343,7 @@ TEST_CASE("curly") { .header("Content-Type", "application/json") .content(R"({"hello":"world"})") .send().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["data"] == R"({"hello":"world"})"); } { @@ -345,7 +353,7 @@ TEST_CASE("curly") { .header("Content-Type", "application/json") .content(R"({"hello":"world"})") .send().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["data"] == R"({"hello":"world"})"); } { @@ -355,7 +363,7 @@ TEST_CASE("curly") { .header("Content-Type", "application/x-www-form-urlencoded") .content("hello=world&world=hello") .send().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["form"]["hello"] == "world"); REQUIRE(content_j["form"]["world"] == "hello"); }