|
|
|
|
@ -1,23 +1,10 @@
|
|
|
|
|
#include <Catch/fakeit.hpp> |
|
|
|
|
#include <session.hpp> |
|
|
|
|
#include <ServerContext.hpp> |
|
|
|
|
#include <SessionContext.hpp> |
|
|
|
|
#include <session.hpp> |
|
|
|
|
|
|
|
|
|
const std::string bad_request_json = "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: 35\r\n\r\n{\"code\":400,\"status\":\"Bad Request\"}"; |
|
|
|
|
const std::string ok_data = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 85\r\n\r\n{\"dht_enabled\":true,\"down_limit\":0,\"paused\":false,\"port\":0,\"torrents\":0,\"up_limit\":0}"; |
|
|
|
|
const std::string ok_data_paused = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 84\r\n\r\n{\"dht_enabled\":true,\"down_limit\":0,\"paused\":true,\"port\":0,\"torrents\":0,\"up_limit\":0}"; |
|
|
|
|
const std::string accepted = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 32\r\n\r\n{\"code\":202,\"status\":\"Accepted\"}"; |
|
|
|
|
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 auto reply_is_service_unavailable = [](std::shared_ptr<TestResponse> response) { |
|
|
|
|
REQUIRE(response->string() == service_unavailable_json); |
|
|
|
|
REQUIRE(response->code() == "503"); |
|
|
|
|
REQUIRE(response->message() == "Service Unavailable"); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const auto reply_is_200_ok = [](std::shared_ptr<TestResponse> response) { |
|
|
|
|
REQUIRE(response->code() == "200"); |
|
|
|
|
REQUIRE(response->message() == "OK"); |
|
|
|
|
}; |
|
|
|
|
const nlohmann::json ok_data = {{"dht_enabled", true}, {"down_limit", 0}, {"paused", false}, {"port", 0}, {"torrents", 0}, {"up_limit", 0}}; |
|
|
|
|
const nlohmann::json ok_data_paused = {{"dht_enabled", true}, {"down_limit", 0}, {"paused", true}, {"port", 0}, {"torrents", 0}, {"up_limit", 0}}; |
|
|
|
|
|
|
|
|
|
SCENARIO("We are running a GET /session resource") { |
|
|
|
|
auto torrent_session = TestSession(); |
|
|
|
|
@ -27,18 +14,23 @@ SCENARIO("We are running a GET /session resource") {
|
|
|
|
|
AND_WHEN("we recive a request") { |
|
|
|
|
tr::session::get(torrent_session, response, request); |
|
|
|
|
THEN("the server should reply with service unavailable") { |
|
|
|
|
reply_is_service_unavailable(response); |
|
|
|
|
CommonResponse::service_unavailable(response); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
GIVEN("the server is working properly") { |
|
|
|
|
torrent_session.valid = true; |
|
|
|
|
AND_WHEN("the session is paused the paused field is set to true") { |
|
|
|
|
WHEN("the server is paused") { |
|
|
|
|
torrent_session.paused = true; |
|
|
|
|
tr::session::get(torrent_session, response, request); |
|
|
|
|
THEN("the server should reply with resource data") { |
|
|
|
|
REQUIRE(response->string() == ok_data_paused); |
|
|
|
|
reply_is_200_ok(response); |
|
|
|
|
THEN("the server should reply with the paused field set to true") { |
|
|
|
|
CommonResponse::ok(response, ok_data_paused); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
WHEN("the server is not paused") { |
|
|
|
|
tr::session::get(torrent_session, response, request); |
|
|
|
|
THEN("the server should reply with the paused field set to true") { |
|
|
|
|
CommonResponse::ok(response, ok_data); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -51,7 +43,7 @@ SCENARIO("We recive a PATCH request on the /session resource") {
|
|
|
|
|
GIVEN("the server is not working properly") { |
|
|
|
|
tr::session::patch(torrent_session, response, request); |
|
|
|
|
THEN("the server should reply with service unavailable") { |
|
|
|
|
reply_is_service_unavailable(response); |
|
|
|
|
CommonResponse::service_unavailable(response); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
GIVEN("the server is working properly") { |
|
|
|
|
@ -60,9 +52,7 @@ SCENARIO("We recive a PATCH request on the /session resource") {
|
|
|
|
|
request->content << "Not valid json"; |
|
|
|
|
tr::session::patch(torrent_session, response, request); |
|
|
|
|
THEN("the server should respond with bad request") { |
|
|
|
|
REQUIRE(response->string() == bad_request_json); |
|
|
|
|
REQUIRE(response->code() == "400"); |
|
|
|
|
REQUIRE(response->message() == "Bad Request"); |
|
|
|
|
CommonResponse::bad_request(response); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
GIVEN("the server recives a valid request") { |
|
|
|
|
@ -80,7 +70,7 @@ SCENARIO("We recive a PATCH request on the /session resource") {
|
|
|
|
|
REQUIRE(torrent_session.settings_.download_rate_limit_ == 0); |
|
|
|
|
REQUIRE(torrent_session.settings_.upload_rate_limit_ == 0); |
|
|
|
|
tr::session::patch(torrent_session, response, request); |
|
|
|
|
REQUIRE(response->string() == accepted); |
|
|
|
|
CommonResponse::accepted(response); |
|
|
|
|
REQUIRE(torrent_session.paused); |
|
|
|
|
REQUIRE_FALSE(torrent_session.settings_.enable_dht_); |
|
|
|
|
REQUIRE(torrent_session.listen_port() == 1); |
|
|
|
|
|