change json parser

This commit is contained in:
2019-06-26 12:17:06 +07:00
parent b6b611c236
commit 2f84281d05
2 changed files with 47 additions and 43 deletions

View File

@@ -5,38 +5,6 @@ project(curly.hpp.untests)
include(FetchContent) 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 # coverage
# #
@@ -55,9 +23,7 @@ endif()
file(GLOB UNTESTS_SOURCES "*.cpp" "*.hpp") file(GLOB UNTESTS_SOURCES "*.cpp" "*.hpp")
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES}) add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
target_link_libraries(${PROJECT_NAME} curly.hpp)
target_link_libraries(${PROJECT_NAME}
nlohmann_json Catch2 curly.hpp)
target_compile_options(${PROJECT_NAME} target_compile_options(${PROJECT_NAME}
PRIVATE PRIVATE
@@ -68,3 +34,33 @@ target_compile_options(${PROJECT_NAME}
-Wall -Wextra -Wpedantic>) -Wall -Wextra -Wpedantic>)
add_test(${PROJECT_NAME} ${PROJECT_NAME}) 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()

View File

@@ -10,8 +10,8 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <rapidjson/document.h>
using json = nlohmann::json; namespace json = rapidjson;
#include <curly.hpp/curly.hpp> #include <curly.hpp/curly.hpp>
namespace net = curly_hpp; namespace net = curly_hpp;
@@ -21,6 +21,14 @@ namespace net = curly_hpp;
namespace 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 { class verbose_uploader : public net::upload_handler {
public: public:
verbose_uploader() = default; verbose_uploader() = default;
@@ -223,7 +231,7 @@ TEST_CASE("curly") {
.header("Custom-Header-2", "custom header value 2") .header("Custom-Header-2", "custom header value 2")
.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");
} }
@@ -235,7 +243,7 @@ TEST_CASE("curly") {
.method(net::methods::get) .method(net::methods::get)
.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["hello"] == "world"); REQUIRE(content_j["hello"] == "world");
REQUIRE(content_j["world"] == "hello"); REQUIRE(content_j["world"] == "hello");
} }
@@ -245,7 +253,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_view());
REQUIRE(content_j["hello"] == "world"); REQUIRE(content_j["hello"] == "world");
REQUIRE(content_j["world"] == "hello"); REQUIRE(content_j["world"] == "hello");
} }
@@ -335,7 +343,7 @@ TEST_CASE("curly") {
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.content(R"({"hello":"world"})") .content(R"({"hello":"world"})")
.send().get(); .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"})"); REQUIRE(content_j["data"] == R"({"hello":"world"})");
} }
{ {
@@ -345,7 +353,7 @@ TEST_CASE("curly") {
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.content(R"({"hello":"world"})") .content(R"({"hello":"world"})")
.send().get(); .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"})"); REQUIRE(content_j["data"] == R"({"hello":"world"})");
} }
{ {
@@ -355,7 +363,7 @@ TEST_CASE("curly") {
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.content("hello=world&world=hello") .content("hello=world&world=hello")
.send().get(); .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"]["hello"] == "world");
REQUIRE(content_j["form"]["world"] == "hello"); REQUIRE(content_j["form"]["world"] == "hello");
} }