Skip to content

Commit

Permalink
impl assertion test
Browse files Browse the repository at this point in the history
  • Loading branch information
raquentin committed Dec 9, 2024
1 parent 1ba1027 commit 6a482f9
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 23 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ set(CMAKE_EXPORT_COMPILE_COMMAND ON)
include_directories(include)
add_definitions(-DSOME_DEFINITION)
add_subdirectory(src)

enable_testing()
add_subdirectory(tests)

include(FetchContent)
Expand All @@ -27,11 +29,15 @@ FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip)

FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG e69e5f977d458f2650bb346dadf2ad30c5320281) # 10.2.1
FetchContent_MakeAvailable(fmt)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest CLI11 json curl)

enable_testing()
FetchContent_MakeAvailable(googletest CLI11 json curl fmt)
14 changes: 14 additions & 0 deletions examples/errors/json_missing_key.raq
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request]
POST https://jsonplaceholder.typicode.com/posts

[headers]
Content-Type: application/json

[body type="json"]
{
"title": "foo"
}

[assertions]
json_field: title ^foo$
json_field: other ^bar$
13 changes: 13 additions & 0 deletions examples/errors/json_value_mismatch.raq
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[request]
POST https://jsonplaceholder.typicode.com/posts

[headers]
Content-Type: application/json

[body type="json"]
{
"title": "bar"
}

[assertions]
json_field: title ^foo$
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ set(SOURCE_FILES parser.cpp request.cpp assertions.cpp response.cpp)

add_library(raquestlib ${SOURCE_FILES})

target_link_libraries(raquestlib CURL::libcurl nlohmann_json::nlohmann_json)
target_link_libraries(raquestlib CURL::libcurl nlohmann_json::nlohmann_json
fmt::fmt)

add_executable(raquest main.cpp)
target_link_libraries(raquest raquestlib)
Expand Down
12 changes: 6 additions & 6 deletions src/assertions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Assertions::validate(const Response &response) const {
std::optional<FailedAssertion>
Assertions::validate_status_code(const Response &response) const {
int status_code = response.get_status_code();
if (std::find(expected_status_codes.begin(), expected_status_codes.end(),
if (!expected_status_codes.empty() &&
std::find(expected_status_codes.begin(), expected_status_codes.end(),
status_code) == expected_status_codes.end()) {
std::stringstream ss;
ss << "Expected one of these status codes: ";
Expand Down Expand Up @@ -90,11 +91,10 @@ Assertions::validate_json_fields(const Response &response) const {
}

if (!std::regex_match(val, pattern.get_regex())) {
return FailedAssertion(FailedAssertionType::UnexpectedJsonField,
"Expected json field '" + key +
"' to match pattern '" +
pattern.get_pattern() +
"' but it didn't, it was '" + val + "'");
return FailedAssertion(
FailedAssertionType::UnexpectedJsonField,
"Expected json field '" + key + "' to match pattern '" +
pattern.get_pattern() + "' but it didn't, it was '" + val + "'");
}
}
return std::nullopt;
Expand Down
1 change: 0 additions & 1 deletion src/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ void Request::set_method(const std::string &method) { this->method = method; }
void Request::set_url(const std::string &url) { this->url = url; }

void Request::add_header(const std::string &key, const std::string &value) {
// TODO: smart validation of key and value mapping against stds
headers.push_back(key + ": " + value);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
set(TEST_SOURCE_FILES tautology_test.cpp parser_test.cpp)
set(TEST_SOURCE_FILES tautology_test.cpp assertions_test.cpp)

add_executable(raquest_test ${TEST_SOURCE_FILES})

target_link_libraries(raquest_test gtest gtest_main raquestlib)
target_link_libraries(raquest_test gtest gtest_main gmock raquestlib pthread)

add_test(NAME test COMMAND raquest_test)

Expand Down
111 changes: 111 additions & 0 deletions tests/assertions_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "assertions.hpp"
#include "response.hpp"
#include <gtest/gtest.h>

class AssertionsTestFixture : public ::testing::Test {
protected:
Assertions assertions;

AssertionsTestFixture() : assertions(Assertions::create()) {}
};

TEST_F(AssertionsTestFixture, SuccessfulValidation) {
assertions.status_codes({200, 201})
.header("Content-Type", "application/json")
.header("Authorization", "Bearer exampletoken12345")
.json_field("title", "^foo$")
.json_field("age", "^30$")
.json_field("isActive", "^true$")
.json_field("description", "^here is the description$");

Response response;
response.parse_status_code(201);
response.parse_header("Content-Type: application/json");
response.parse_header("Authorization: Bearer exampletoken12345");
response.set_body(R"({
"title": "foo",
"age": "30",
"isActive": "true",
"description": "here is the description"
})");

std::optional<FailedAssertion> result = assertions.validate(response);

EXPECT_FALSE(result.has_value())
<< "Validation should pass but failed with: " << result->message;
}

TEST_F(AssertionsTestFixture, UnexpectedStatusCode) {
assertions.status_codes({200, 201});

Response response;
response.parse_status_code(404);

std::optional<FailedAssertion> result = assertions.validate(response);

ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->type, FailedAssertionType::UnexpectedStatusCode);
EXPECT_EQ(result->message,
"Expected one of these status codes: 200 201 but got 404");
}

TEST_F(AssertionsTestFixture, MissingHeader) {
assertions.header("Authorization", "Bearer exampletoken12345");

Response response;
response.parse_status_code(200);

std::optional<FailedAssertion> result = assertions.validate(response);

ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->type, FailedAssertionType::MissingHeader);
EXPECT_EQ(result->message,
"Expected header 'Authorization' to be present but it wasn't");
}

TEST_F(AssertionsTestFixture, UnexpectedHeaderValue) {
assertions.header("Content-Type", "application/json");

Response response;
response.parse_status_code(200);
response.parse_header("Content-Type: text/html");

std::optional<FailedAssertion> result = assertions.validate(response);

ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->type, FailedAssertionType::UnexpectedHeader);
EXPECT_EQ(result->message, "Expected header 'Content-Type' to have value "
"'application/json' but got 'text/html'");
}

TEST_F(AssertionsTestFixture, MissingJsonField) {
assertions.json_field("age", "^30$");

Response response;
response.parse_status_code(200);
response.set_body(R"({
"title": "foo",
})"); // "age" field is missing

std::optional<FailedAssertion> result = assertions.validate(response);

ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->type, FailedAssertionType::MissingJsonField);
EXPECT_EQ(result->message,
"Expected json field 'age' to be present but it wasn't");
}

TEST_F(AssertionsTestFixture, JsonFieldPatternMismatch) {
assertions.json_field("title", "^foo$");

Response response;
response.set_body(R"({
"title": "bar"
})");

std::optional<FailedAssertion> result = assertions.validate(response);

ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->type, FailedAssertionType::UnexpectedJsonField);
EXPECT_EQ(result->message, "Expected json field 'title' to match pattern '^foo$' but it didn't, it was 'bar'");
}
4 changes: 0 additions & 4 deletions tests/parser_test.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions tests/tautology_test.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#include <gtest/gtest.h>
#include <nlohmann/json.hpp>

TEST(Tautology, Addition) {
EXPECT_EQ(1 + 1, 2);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 6a482f9

Please sign in to comment.