diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 011e6cd..396f64b 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -84,6 +84,21 @@ 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; + + if (request_object.find("paused") != request_object.end()) { + bool paused = false; + auto obj = request_object.at("paused"); + if (obj.is_boolean()) + paused = request_object.at("paused"); + else if (obj.is_string()) { + std::string p = obj; + paused = p == "true"; + } + if (paused) + params.flags |= libtorrent::add_torrent_params::flag_paused; + } + session.async_add_torrent(params); std::stringstream ss; diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 6ae0902..3fe53bc 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -26,6 +26,8 @@ public: class TestTorrent { public: TestTorrent(const libtorrent::add_torrent_params ¶ms) : hash(params.info_hash.to_string()) { + auto paused = (params.flags & libtorrent::add_torrent_params::flag_paused) == libtorrent::add_torrent_params::flag_paused; + status_.paused = paused; status_.save_path = params.save_path; } TestTorrent() : hash("12a") {} diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index 1508730..0f2b8d6 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -89,9 +89,23 @@ SCENARIO("We are running a POST /session/torrents resource") { {{"magnet_uri", magnet_uri}, {"save_path", "music"}}); tr::session::torrents::post(settings, torrent_session, response, request); + THEN("the server should reply created") { + CommonResponse::created(response, request, "/session/torrents/" + magnet_hash); + REQUIRE(torrent_session.get_torrents().size() == 1); + REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused); + REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music"); + } + } + GIVEN("we specify the torrent field to be paused") { + request->content << nlohmann::json::object( + {{"magnet_uri", magnet_uri}, + {"save_path", "music"}, + {"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); REQUIRE(torrent_session.get_torrents().size() == 1); + REQUIRE(torrent_session.get_torrents()[0].status().paused); REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music"); } }