From b34eb6fb9b628310689b9602590cc577a239ff18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sat, 13 May 2017 23:17:20 +0200 Subject: [PATCH] wip: torrents delete route, also some refactoring --- toREST/include/torrents.hpp | 36 +++++++++++++++++++++++--- toREST/tests/torrents_test.cpp | 47 +++++++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index da31362..3f2e2bc 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -39,6 +39,36 @@ void get(torrent_session &session, response resp, request req) { } *resp << http_response; } + +template +void del(torrent_session &session, response resp, request req) { + auto http_response = http::response(); + const auto respond = [&](http::status status) { + const auto response_code = http::code(status); + http_response.set_body({{"code", response_code.first}, {"status", response_code.second}}); + http_response.set_status(response_code.first); + *resp << http_response; + }; + + if (!session.is_valid()) { + return respond(http::service_unavailable); + } + + auto request_object = util::json::parse(req->content); + + if (request_object.is_null()) { + return respond(http::bad_request); + } + + if (!request_object.is_object()) { + return respond(http::bad_request); + } + + if (request_object.find("info_hash") == request_object.end()) { + return respond(http::bad_request); + } +} + template void post(settings opts, torrent_session &session, response resp, request req) { auto http_response = http::response(); @@ -84,7 +114,7 @@ void post(settings opts, torrent_session &session, response resp, request req) { } else params.save_path = boost::filesystem::path(opts.root_dir / opts.default_download_dir).string(); - params.flags = params.flags & ~libtorrent::add_torrent_params::flag_paused; + params.flags &= ~libtorrent::add_torrent_params::flag_paused; if (request_object.find("paused") != request_object.end()) { bool paused = false; @@ -122,10 +152,10 @@ void post(settings opts, torrent_session &session, response resp, request req) { } params.download_limit = down_limit; } - + if (request_object.find("name") != request_object.end()) { auto obj = request_object.at("name"); - if(obj.is_string()) + if (obj.is_string()) params.name = obj; } diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index 4d2233d..df2ed81 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -47,13 +47,48 @@ SCENARIO("We are running a GET /session/torrents resource") { } } +const std::string torrent_id = "c0b0a90089710812fe8c37385a4cc2978eabf3e8"; + + +SCENARIO("We are running a DELETE /session/torrents 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::torrents::del(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("we recive an invalid request") { + GIVEN("the request doesn't contain valid json") { + request->content << "Not valid"; + THEN("the server should reply with bad request") { + tr::session::torrents::del(torrent_session, response, request); + CommonResponse::bad_request(response); + } + } + GIVEN("it is valid json but are missing requred field") { + request->content << nlohmann::json({{"up_speed", "100"}}); + THEN("the server should reply with bad request") { + tr::session::torrents::del(torrent_session, response, request); + CommonResponse::bad_request(response); + } + } + } + } +} + SCENARIO("We are running a POST /session/torrents resource") { auto torrent_session = TestSession(); auto settings = Config(); auto response = std::make_shared(); auto request = std::make_shared(); - const std::string magnet_hash = "c0b0a90089710812fe8c37385a4cc2978eabf3e8"; - const auto magnet_uri = "magnet:?xt=urn:btih:" + magnet_hash + "&dn=Taylor Swift&tr=http://tracker.sout.no"; + const auto magnet_uri = "magnet:?xt=urn:btih:" + torrent_id + "&dn=Taylor Swift&tr=http://tracker.sout.no"; GIVEN("the server is not working properly") { AND_WHEN("we recive a request") { tr::session::torrents::post(settings, torrent_session, response, request); @@ -69,7 +104,7 @@ SCENARIO("We are running a POST /session/torrents resource") { request->content << nlohmann::json::object({{"magnet_uri", magnet_uri}}); THEN("the server should reply with created") { tr::session::torrents::post(settings, torrent_session, response, request); - CommonResponse::created(response, request, "/session/torrents/" + magnet_hash); + CommonResponse::created(response, request, "/session/torrents/" + torrent_id); REQUIRE(torrent_session.get_torrents().size() == 1); REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST"); } @@ -90,7 +125,7 @@ SCENARIO("We are running a POST /session/torrents resource") { {"save_path", "music"}}); THEN("the server should reply created") { tr::session::torrents::post(settings, torrent_session, response, request); - CommonResponse::created(response, request, "/session/torrents/" + magnet_hash); + CommonResponse::created(response, request, "/session/torrents/" + torrent_id); REQUIRE(torrent_session.get_torrents().size() == 1); REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused); REQUIRE(torrent_session.get_torrents()[0].upload_limit() == -1); @@ -105,7 +140,7 @@ SCENARIO("We are running a POST /session/torrents resource") { {"paused", "true"}}); THEN("the server should reply with bad request") { tr::session::torrents::post(settings, torrent_session, response, request); - CommonResponse::created(response, request, "/session/torrents/" + magnet_hash); + CommonResponse::created(response, request, "/session/torrents/" + torrent_id); REQUIRE(torrent_session.get_torrents().size() == 1); REQUIRE(torrent_session.get_torrents()[0].status().paused); REQUIRE(torrent_session.get_torrents()[0].upload_limit() == -1); @@ -123,7 +158,7 @@ SCENARIO("We are running a POST /session/torrents resource") { {"name", name}}); THEN("the server should reply with bad request") { tr::session::torrents::post(settings, torrent_session, response, request); - CommonResponse::created(response, request, "/session/torrents/" + magnet_hash); + CommonResponse::created(response, request, "/session/torrents/" + torrent_id); REQUIRE(torrent_session.get_torrents().size() == 1); REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused); REQUIRE(torrent_session.get_torrents()[0].status().name == name);