You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 lines
4.6 KiB

#include <Catch/fakeit.hpp>
#include <session.hpp>
using namespace fakeit;
const std::string method_not_allowed_json ="HTTP/1.1 405 Method Not Allowed\r\nContent-Type: application/json\r\nContent-Length: 42\r\n\r\n{\"code\":405,\"status\":\"Method Not Allowed\"}";
const std::string service_unavailable_json="HTTP/1.1 503 Service Unavailable\r\nContent-Type: application/json\r\nContent-Length: 43\r\n\r\n{\"code\":503,\"status\":\"Service Unavailable\"}";
const std::string internal_server_error_json="HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nContent-Length: 45\r\n\r\n{\"code\":500,\"status\":\"Internal Server Error\"}";
const std::string not_found ="HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\nContent-Length: 36\r\n\r\n{\"code\":404,\"status\":\"Not Found\"}";
const std::string ok_nullptr_json ="HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 4\r\n\r\nnull";
const std::string bad_request ="HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: 35\r\n\r\n{\"code\":400,\"status\":\"Bad Request\"}";
using namespace session;
SCENARIO("test of responses by session_manager on /v1/session"){
session::resource sr;
std::stringstream ss;
GIVEN("the session is invalid"){
WHEN("we receive a GET request"){
sr.get();
ss << sr;
THEN("the response is a 503 Service Unavailable JSON object")
REQUIRE(ss.str()==service_unavailable_json);
}
WHEN("we receive a PATCH request"){
sr.patch();
ss << sr;
THEN("the response is a 503 Service Unavailable JSON object")
REQUIRE(ss.str()==service_unavailable_json);
}
WHEN("we receive a POST request"){
sr.post();
ss << sr;
THEN("the response is a 405 Method Not Allowed JSON object")
REQUIRE(ss.str()==method_not_allowed_json);
}
WHEN("we receive a DELETE request"){
sr.del();
ss << sr;
THEN("the response is a 405 Method Not Allowed JSON object")
REQUIRE(ss.str()==method_not_allowed_json);
}
WHEN("we receive a PUT request"){
sr.put();
ss << sr;
THEN("the response is a 405 Method Not Allowed JSON object")
REQUIRE(ss.str()==method_not_allowed_json);
}
}
GIVEN("the session is valid"){
Mock<basic_manager> mock_session;
When(Method(mock_session,basic_manager::is_valid)).AlwaysReturn(true);
When(Method(mock_session,basic_manager::get_json)).AlwaysReturn(nullptr);
auto session=std::shared_ptr<basic_manager>(&(mock_session.get()),[](...){});
sr.set_session(session);
WHEN("we receive a GET request"){
sr.get();
ss << sr;
THEN("the response is a 200 OK JSON object ")
REQUIRE(ss.str()==ok_nullptr_json);
}
WHEN("we receive a PATCH request with invalid data"){
GIVEN("the JSON is invalid"){
sr.patch(nullptr);
ss << sr;
THEN("the response is a 400 Bad Request JSON object")
REQUIRE(ss.str()==bad_request);
}
GIVEN("the JSON is valid, but contains no data"){
sr.patch({});
ss << sr;
THEN("the response is a 400 Bad Request JSON object")
REQUIRE(ss.str()==bad_request);
}
GIVEN("the JSON is valid, but the port number is to low"){
When(Method(mock_session,basic_manager::patch)).AlwaysReturn(false);
sr.patch({{"listen_port",1}});
ss << sr;
THEN("the response is a 500 Internal Server Error JSON object")
REQUIRE(ss.str()==internal_server_error_json);
}
GIVEN("the JSON is valid, but listen_port is a string"){
sr.patch({{"listen_port","1"}});
ss << sr;
THEN("the response is a 400 Bad Request JSON object")
REQUIRE(ss.str()==bad_request);
}
GIVEN("the JSON is valid, but is_paused is a string"){
sr.patch({{"is_paused","1"}});
ss << sr;
THEN("the response is a 400 Bad Request JSON object")
REQUIRE(ss.str()==bad_request);
}
WHEN("we receive a POST request"){
sr.post();
ss << sr;
THEN("the response is a 405 Method Not Allowed JSON object")
REQUIRE(ss.str()==method_not_allowed_json);
}
WHEN("we receive a DELETE request"){
sr.del();
ss << sr;
THEN("the response is a 405 Method Not Allowed JSON object")
REQUIRE(ss.str()==method_not_allowed_json);
}
WHEN("we receive a PUT request"){
sr.put();
ss << sr;
THEN("the response is a 405 Method Not Allowed JSON object")
REQUIRE(ss.str()==method_not_allowed_json);
}
}
}
}