Browse Source

Can now choose to remove files and/or torrent

master
Jørgen Lien Sellæg 9 years ago
parent
commit
faddd752d8
  1. 16
      toREST/include/torrents.hpp
  2. 2
      toREST/tests/include/SessionContext.hpp
  3. 16
      toREST/tests/stubs/SessionContext.cpp
  4. 37
      toREST/tests/torrents_test.cpp

16
toREST/include/torrents.hpp

@ -74,6 +74,20 @@ void del(torrent_session &session, response resp, request req) {
return respond(http::bad_request); 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; std::string sha1_hash_in = obj;
libtorrent::sha1_hash sha1_hash; libtorrent::sha1_hash sha1_hash;
std::stringstream ss; std::stringstream ss;
@ -82,7 +96,7 @@ void del(torrent_session &session, response resp, request req) {
auto handle = session.find_torrent(sha1_hash); auto handle = session.find_torrent(sha1_hash);
if (handle.is_valid()) { if (handle.is_valid()) {
session.remove_torrent(handle); session.remove_torrent(handle, options);
} else { } else {
return respond(http::bad_request); return respond(http::bad_request);
} }

2
toREST/tests/include/SessionContext.hpp

@ -39,7 +39,7 @@ public:
bool is_dht_running() const; bool is_dht_running() const;
std::vector<TestTorrent> &get_torrents(); std::vector<TestTorrent> &get_torrents();
TestTorrent find_torrent(const libtorrent::sha1_hash &hash); 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 apply_settings(TestSessionSettings settings);
void async_add_torrent(const libtorrent::add_torrent_params &params); void async_add_torrent(const libtorrent::add_torrent_params &params);
}; };

16
toREST/tests/stubs/SessionContext.cpp

@ -76,15 +76,17 @@ TestTorrent TestSession::find_torrent(const libtorrent::sha1_hash &hash) {
return res; return res;
} }
void TestSession::remove_torrent(const TestTorrent &torrent) { void TestSession::remove_torrent(const TestTorrent &torrent, int options = -1) {
auto itr = torrents_.end(); if (options > -1) {
for (itr = torrents_.begin(); itr != torrents_.end(); itr++) { auto itr = torrents_.end();
if (itr->info_hash() == torrent.info_hash()) { for (itr = torrents_.begin(); itr != torrents_.end(); itr++) {
break; 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) { void TestSession::apply_settings(TestSessionSettings settings) {

37
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") { SCENARIO("We are running a DELETE /session/torrents resource") {
auto torrent_session = TestSession(); auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>(); auto response = std::make_shared<TestResponse>();
@ -193,17 +204,29 @@ SCENARIO("We are running a DELETE /session/torrents resource") {
} }
} }
GIVEN("fields are correct") { GIVEN("fields are correct") {
request->content << nlohmann::json({{"info_hash", torrent_id}});
GIVEN("the torrent exists") { GIVEN("the torrent exists") {
torrent_session.torrents_.emplace_back(); theTorrentExists(torrent_session);
libtorrent::sha1_hash sha1_hash;
std::stringstream ss;
ss << torrent_id;
ss >> sha1_hash;
torrent_session.torrents_[0].hash_ = sha1_hash;
THEN("the server should reply no content") { THEN("the server should reply no content") {
request->content << nlohmann::json({{"info_hash", torrent_id}});
tr::session::torrents::del(torrent_session, response, request); tr::session::torrents::del(torrent_session, response, request);
CommonResponse::no_content(response); 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") { GIVEN("the torrent doesn't exist") {

Loading…
Cancel
Save