#include #include #include #include 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(); auto response = std::make_shared(); auto request = std::make_shared(); GIVEN("the server is not working properly") { AND_WHEN("we recive a request") { tr::session::get(torrent_session, response, request); THEN("the server should reply with service unavailable") { CommonResponse::service_unavailable(response); } } } GIVEN("the server is working properly") { torrent_session.valid = true; WHEN("the server is paused") { torrent_session.paused = true; tr::session::get(torrent_session, response, request); 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); } } } } SCENARIO("We recive a PATCH request on the /session resource") { auto torrent_session = TestSession(); auto response = std::make_shared(); auto request = std::make_shared(); GIVEN("the server is not working properly") { tr::session::patch(torrent_session, response, request); THEN("the server should reply with service unavailable") { CommonResponse::service_unavailable(response); } } GIVEN("the server is working properly") { torrent_session.valid = true; GIVEN("the server recives invalid request") { request->content << "Not valid json"; tr::session::patch(torrent_session, response, request); THEN("the server should respond with bad request") { CommonResponse::bad_request(response); } } GIVEN("the server recives a valid request") { const nlohmann::json obj({ {"dht_enabled", false}, {"paused", true}, {"port", 1}, {"down_limit", 100}, {"up_limit", 100}, }); request->content << obj; REQUIRE_FALSE(torrent_session.paused); REQUIRE(torrent_session.settings_.enable_dht_); REQUIRE(torrent_session.listen_port() == 0); REQUIRE(torrent_session.settings_.download_rate_limit_ == 0); REQUIRE(torrent_session.settings_.upload_rate_limit_ == 0); tr::session::patch(torrent_session, response, request); CommonResponse::accepted(response); REQUIRE(torrent_session.paused); REQUIRE_FALSE(torrent_session.settings_.enable_dht_); REQUIRE(torrent_session.listen_port() == 1); REQUIRE(torrent_session.settings_.download_rate_limit_ == 100); REQUIRE(torrent_session.settings_.upload_rate_limit_ == 100); } } }