diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 24dca93..1376396 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -74,6 +74,20 @@ void del(torrent_session &session, response resp, request req) { return respond(http::bad_request); } + int options = 0; + + if (request_object.find("remove_files") != request_object.end()) { + bool remove_files = false; + auto obj = request_object.at("remove_files"); + if (obj.is_boolean()) + remove_files = request_object.at("remove_files"); + else if (obj.is_string()) { + std::string p = obj; + remove_files = p == "true"; + } + options = remove_files; + } + std::string sha1_hash_in = obj; libtorrent::sha1_hash sha1_hash; std::stringstream ss; @@ -82,7 +96,7 @@ void del(torrent_session &session, response resp, request req) { auto handle = session.find_torrent(sha1_hash); if (handle.is_valid()) { - session.remove_torrent(handle); + session.remove_torrent(handle, options); } else { return respond(http::bad_request); } diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index 088654d..bfbb5a8 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -39,7 +39,7 @@ public: bool is_dht_running() const; std::vector &get_torrents(); TestTorrent find_torrent(const libtorrent::sha1_hash &hash); - void remove_torrent(const TestTorrent &torrent); + void remove_torrent(const TestTorrent &torrent, int options); void apply_settings(TestSessionSettings settings); void async_add_torrent(const libtorrent::add_torrent_params ¶ms); }; diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index 01c6110..7b2dca7 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -76,15 +76,17 @@ TestTorrent TestSession::find_torrent(const libtorrent::sha1_hash &hash) { return res; } -void TestSession::remove_torrent(const TestTorrent &torrent) { - auto itr = torrents_.end(); - for (itr = torrents_.begin(); itr != torrents_.end(); itr++) { - if (itr->info_hash() == torrent.info_hash()) { - break; +void TestSession::remove_torrent(const TestTorrent &torrent, int options = -1) { + if (options > -1) { + auto itr = torrents_.end(); + for (itr = torrents_.begin(); itr != torrents_.end(); itr++) { + if (itr->info_hash() == torrent.info_hash()) { + break; + } } + if (itr != torrents_.end()) + torrents_.erase(itr); } - if (itr != torrents_.end()) - torrents_.erase(itr); } void TestSession::apply_settings(TestSessionSettings settings) { diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index 38d7afe..1c86f8f 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -156,6 +156,17 @@ SCENARIO("We are running a POST /session/torrents resource") { } } +void theTorrentExists(TestSession &torrent_session) { + torrent_session.torrents_.emplace_back(); + libtorrent::sha1_hash sha1_hash; + std::stringstream ss; + ss << torrent_id; + ss >> sha1_hash; + torrent_session.torrents_[0].hash_ = sha1_hash; + torrent_session.torrents_[0].valid = true; + REQUIRE(torrent_session.get_torrents().size() == 1); +}; + SCENARIO("We are running a DELETE /session/torrents resource") { auto torrent_session = TestSession(); auto response = std::make_shared(); @@ -193,17 +204,29 @@ SCENARIO("We are running a DELETE /session/torrents resource") { } } GIVEN("fields are correct") { - request->content << nlohmann::json({{"info_hash", torrent_id}}); GIVEN("the torrent exists") { - torrent_session.torrents_.emplace_back(); - libtorrent::sha1_hash sha1_hash; - std::stringstream ss; - ss << torrent_id; - ss >> sha1_hash; - torrent_session.torrents_[0].hash_ = sha1_hash; + theTorrentExists(torrent_session); THEN("the server should reply no content") { + request->content << nlohmann::json({{"info_hash", torrent_id}}); tr::session::torrents::del(torrent_session, response, request); CommonResponse::no_content(response); + REQUIRE(torrent_session.get_torrents().size() == 0); + } + GIVEN("we want to remove files") { + THEN("the should reply with no content") { + request->content << nlohmann::json({{"info_hash", torrent_id}, + {"remove_files", true}}); + tr::session::torrents::del(torrent_session, response, request); + CommonResponse::no_content(response); + REQUIRE(torrent_session.get_torrents().size() == 0); + } + THEN("we should reply with no content") { + request->content << nlohmann::json({{"info_hash", torrent_id}, + {"remove_files", "true"}}); + tr::session::torrents::del(torrent_session, response, request); + CommonResponse::no_content(response); + REQUIRE(torrent_session.get_torrents().size() == 0); + } } } GIVEN("the torrent doesn't exist") {