#include #include using namespace std; TEST_CASE("Check http status"){ auto out=http::http_code(http::ok); REQUIRE(out.first == 200); REQUIRE(out.second == "OK"); // TODO test all } SCENARIO("http response"){ auto response=http::response(); 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"s); } 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"s); } } 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"s); } GIVEN("we stream our response with the body set"){ response.set_body("Easter eggs, everywhere! ye"); 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"s); } } SCENARIO("Stream http responses with a json response"){ auto response=http::json_response(); std::stringstream ss; GIVEN("we stream our response without setting any options"){ ss << response; THEN("our response is valid json and the response code is 200") REQUIRE(ss.str()=="HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 26\r\n\r\n{\"code\":200,\"status\":\"OK\"}"s); } GIVEN("we set the response code to 404 and stream our response"){ response.set_status(http::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\"}"s); } 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\nContent-Type: application/json\r\n\r\n"s); } 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{}"s); } } /* TEST(http_response_handler, header){ ServerTest s; auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nFoo: bar\r\nFoo: bars\r\nContent-Type: application/json\r\n\r\n"); std::shared_ptr resp(new ServerTest::Response(nullptr)); http::request_handler out(req,resp); ASSERT_TRUE(out.header_set("Content-Type")); ASSERT_EQ(out.find_last_header_value("Content-Type"),"application/json"); ASSERT_EQ(out.find_last_header_value("Host"),"localhost:8080"); ASSERT_EQ(out.find_last_header_value("Foo"),"bars"); ASSERT_NE(out.find_last_header_value("Foo"), "bar"); ASSERT_FALSE(out.header_set("Content-Length")); ASSERT_TRUE(out.header_equals("Content-Type","application/json")); } TEST(http_response_handler, json_response){ ServerTest s; auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nContent-Type: application/json\r\n\r\n"); std::shared_ptr resp(new ServerTest::Response(nullptr)); http::request_handler out(req,resp); out.respond(nlohmann::json("{}")); std::stringstream ss; ss << resp->rdbuf(); std::string string_response(ss.str()); ASSERT_EQ(string_response,"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nContent-Type: application/json\r\n\r\n\"{}\""); } TEST(http_response_handler, string_response){ ServerTest s; auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nContent-Type: application/json\r\n\r\n"); std::shared_ptr resp(new ServerTest::Response(nullptr)); http::request_handler out(req,resp); out.respond(std::string("{}")); std::stringstream ss; ss << resp->rdbuf(); std::string string_response(ss.str()); ASSERT_EQ(string_response,"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n{}"); } TEST(http_response_handler, empty_string){ ServerTest s; auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nContent-Type: application/json\r\n\r\n"); std::shared_ptr resp(new ServerTest::Response(nullptr)); http::request_handler out(req,resp); out.respond(std::string("")); std::stringstream ss; ss << resp->rdbuf(); std::string string_response(ss.str()); ASSERT_EQ(string_response,"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); } */