4 changed files with 72 additions and 167 deletions
@ -1,116 +0,0 @@
|
||||
#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 sut; |
||||
std::stringstream ss; |
||||
GIVEN("the session is invalid"){ |
||||
WHEN("we receive a GET request"){ |
||||
sut.get(); |
||||
ss << sut; |
||||
THEN("the response is a 503 Service Unavailable JSON object") |
||||
REQUIRE(ss.str()==service_unavailable_json); |
||||
} |
||||
WHEN("we receive a PATCH request"){ |
||||
sut.patch(); |
||||
ss << sut; |
||||
THEN("the response is a 503 Service Unavailable JSON object") |
||||
REQUIRE(ss.str()==service_unavailable_json); |
||||
} |
||||
WHEN("we receive a POST request"){ |
||||
sut.post(); |
||||
ss << sut; |
||||
THEN("the response is a 405 Method Not Allowed JSON object") |
||||
REQUIRE(ss.str()==method_not_allowed_json); |
||||
} |
||||
WHEN("we receive a DELETE request"){ |
||||
sut.del(); |
||||
ss << sut; |
||||
THEN("the response is a 405 Method Not Allowed JSON object") |
||||
REQUIRE(ss.str()==method_not_allowed_json); |
||||
} |
||||
WHEN("we receive a PUT request"){ |
||||
sut.put(); |
||||
ss << sut; |
||||
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()),[](...){}); |
||||
sut.set_session(session); |
||||
|
||||
WHEN("we receive a GET request"){ |
||||
sut.get(); |
||||
ss << sut; |
||||
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"){ |
||||
sut.patch(nullptr); |
||||
ss << sut; |
||||
THEN("the response is a 400 Bad Request JSON object") |
||||
REQUIRE(ss.str()==bad_request); |
||||
} |
||||
GIVEN("the JSON is valid, but contains no data"){ |
||||
sut.patch({}); |
||||
ss << sut; |
||||
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); |
||||
sut.patch({{"listen_port",1}}); |
||||
ss << sut; |
||||
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"){ |
||||
sut.patch({{"listen_port","1"}}); |
||||
ss << sut; |
||||
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"){ |
||||
sut.patch({{"is_paused","1"}}); |
||||
ss << sut; |
||||
THEN("the response is a 400 Bad Request JSON object") |
||||
REQUIRE(ss.str()==bad_request); |
||||
} |
||||
WHEN("we receive a POST request"){ |
||||
sut.post(); |
||||
ss << sut; |
||||
THEN("the response is a 405 Method Not Allowed JSON object") |
||||
REQUIRE(ss.str()==method_not_allowed_json); |
||||
} |
||||
WHEN("we receive a DELETE request"){ |
||||
sut.del(); |
||||
ss << sut; |
||||
THEN("the response is a 405 Method Not Allowed JSON object") |
||||
REQUIRE(ss.str()==method_not_allowed_json); |
||||
} |
||||
WHEN("we receive a PUT request"){ |
||||
sut.put(); |
||||
ss << sut; |
||||
THEN("the response is a 405 Method Not Allowed JSON object") |
||||
REQUIRE(ss.str()==method_not_allowed_json); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue