# 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 \n Content-Type: application/json \r \n Content-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 \n Content-Type: application/json \r \n Content-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 \n Content-Type: application/json \r \n Content-Length: 45 \r \n \r \n { \" code \" :500, \" status \" : \" Internal Server Error \" } " ;
const std : : string not_found = " HTTP/1.1 404 Not Found \r \n Content-Type: application/json \r \n Content-Length: 36 \r \n \r \n { \" code \" :404, \" status \" : \" Not Found \" } " ;
const std : : string ok_nullptr_json = " HTTP/1.1 200 OK \r \n Content-Type: application/json \r \n Content-Length: 4 \r \n \r \n null " ;
const std : : string bad_request = " HTTP/1.1 400 Bad Request \r \n Content-Type: application/json \r \n Content-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 ) ;
}
}
}
}