#include #include TEST_CASE("Check http status") { auto out = http::code(http::ok); REQUIRE(out.first == 200); REQUIRE(out.second == "OK"); } SCENARIO("http response") { auto response = http::response(); std::string body; std::stringstream ss; GIVEN("we stream our response without setting given any data") { ss << response; THEN("the response will default to 200") REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\n200 OK"); } GIVEN("we stream our data with the response status set to 404") { response.set_status(http::not_found); ss << response; THEN("the default 404 response will be sent instead") { REQUIRE(ss.str() == "HTTP/1.1 404 Not Found\r\nContent-Length: 13\r\n\r\n404 Not Found"); } } GIVEN("we stream our response with the status set to 204 No Content") { response.set_status(http::no_content); ss << response; THEN("the response has no content header set") REQUIRE(ss.str() == "HTTP/1.1 204 No Content\r\n\r\n"); } GIVEN("we stream our response with the body set") { body = "Easter eggs, everywhere! ye"; response.set_body(body); ss << response; THEN("our response contains the appropriate headers") REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Length: 103\r\n\r\nEaster eggs, everywhere! ye"); } GIVEN("we add a header to the resource") { body = "Nothing"; response.set_body(body); response.add_header(http::header("Strict-Error", "on")); ss << response; THEN("our response contains the appropriate headers") REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nStrict-Error: on\r\nContent-Length: 7\r\n\r\nNothing"); } } SCENARIO("Stream http responses with a json response") { auto response = http::response(); std::stringstream ss; GIVEN("we set the response code to 404 and stream our response") { response.set_status(http::not_found); response.set_body({{"code", 404}, {"status", "Not Found"}}); ss << response; THEN("our response is valid json and the resoinse code is 404") REQUIRE(ss.str() == "HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\nContent-Length: 33\r\n\r\n{\"code\":404,\"status\":\"Not Found\"}"); } GIVEN("we stream our response with the body set") { response.set_body(nlohmann::json::object()); ss << response; THEN("our response contains the appropriate headers") REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n{}"); } }