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