From 3cddfc26f1389b910d2d5babd09929a098727b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 12 May 2017 13:41:13 +0200 Subject: [PATCH] up and down speed can be set on add --- toREST/include/torrents.hpp | 26 ++++++++++++++++++++++++- toREST/tests/include/TorrentContext.hpp | 10 ++++++++++ toREST/tests/torrents_test.cpp | 16 +++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 396f64b..9ff758b 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -99,11 +99,35 @@ void post(settings opts, torrent_session &session, response resp, request req) { params.flags |= libtorrent::add_torrent_params::flag_paused; } - session.async_add_torrent(params); + if (request_object.find("up_speed") != request_object.end()) { + int up_limit = 0; + auto obj = request_object.at("up_speed"); + if (obj.is_string()) { + std::string i = obj; + up_limit = std::stoi(i); + } else if (obj.is_number()) { + up_limit = obj; + } + params.upload_limit = up_limit; + } + + if (request_object.find("down_speed") != request_object.end()) { + int down_limit = 0; + auto obj = request_object.at("down_speed"); + if (obj.is_string()) { + std::string i = obj; + down_limit = std::stoi(i); + } else if (obj.is_number()) { + down_limit = obj; + } + params.download_limit = down_limit; + } std::stringstream ss; ss << params.info_hash; + session.async_add_torrent(params); + http_response.add_header({"Location", "/session/torrents/" + ss.str()}); return respond(http::created); } diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 3fe53bc..a40afa7 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -29,6 +29,8 @@ public: 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; + upload_limit_ = params.upload_limit; + download_limit_ = params.download_limit; } TestTorrent() : hash("12a") {} bool is_valid(); @@ -37,6 +39,14 @@ public: int query_name = 1; int query_save_path = 2; bool valid = true; + int upload_limit_ = 0; + int upload_limit(){ + return upload_limit_; + } + int download_limit_ = 0; + int download_limit(){ + return download_limit_; + } InfoHash hash; TorrentStatus status_; }; diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index 0f2b8d6..3e408e3 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -109,6 +109,22 @@ SCENARIO("We are running a POST /session/torrents resource") { REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music"); } } + GIVEN("we specify up an ddown speed") { + request->content << nlohmann::json::object( + {{"magnet_uri", magnet_uri}, + {"save_path", "music"}, + {"up_speed", "100"}, + {"down_speed", "100"}}); + 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_FALSE(torrent_session.get_torrents()[0].status().paused); + REQUIRE(torrent_session.get_torrents()[0].upload_limit() == 100); + REQUIRE(torrent_session.get_torrents()[0].download_limit() == 100); + REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music"); + } + } } WHEN("we recive an invalid request") { GIVEN("the request doesn't contain valid json") {