From 07b96ad8173e958954bb4e6bb3cc530952c4f96b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Tue, 16 May 2017 20:10:13 +0200 Subject: [PATCH] set download/upload on torrent resource --- toREST/include/torrent.hpp | 40 ++++++++++++++++++++++++- toREST/tests/include/TorrentContext.hpp | 2 ++ toREST/tests/stubs/TorrentContext.cpp | 10 ++++++- toREST/tests/torrent_test.cpp | 2 ++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 62e695f..80e409a 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -90,7 +90,11 @@ void patch(torrent_session &session, response resp, request req) { return respond(http::bad_request); } - if (request_object.find("paused") != request_object.end()) { + auto paused_field = request_object.find("paused") != request_object.end(); + auto up_limit_field = request_object.find("up_limit") != request_object.end(); + auto down_limit_field = request_object.find("down_limit") != request_object.end(); + + if (paused_field) { bool paused = false; const auto obj = request_object.at("paused"); if (obj.is_boolean()) @@ -98,6 +102,8 @@ void patch(torrent_session &session, response resp, request req) { else if (obj.is_string()) { std::string p = obj; paused = (p == "true"); + } else { + return respond(http::bad_request); } if (torrent.status().paused != paused) { if (paused) @@ -108,6 +114,38 @@ void patch(torrent_session &session, response resp, request req) { } } + if (up_limit_field) { + int up_limit = 0; + auto obj = request_object.at("up_limit"); + if (obj.is_string()) { + std::string i = obj; + up_limit = std::stoi(i); + } else if (obj.is_number()) { + up_limit = obj; + } else { + return respond(http::bad_request); + } + torrent.set_upload_limit(up_limit); + } + + if (down_limit_field) { + int down_limit = 0; + auto obj = request_object.at("down_limit"); + if (obj.is_string()) { + std::string i = obj; + down_limit = std::stoi(i); + } else if (obj.is_number()) { + down_limit = obj; + } else { + return respond(http::bad_request); + } + torrent.set_download_limit(down_limit); + } + + if (down_limit_field || up_limit_field || paused_field) { + return respond(http::bad_request); + } + respond(http::accepted); } } diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index 7b80b61..cebbeb1 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -33,6 +33,8 @@ public: bool valid = true; int upload_limit_ = 0; int upload_limit() const; + void set_download_limit(int limit); + void set_upload_limit(int limit); int download_limit_ = 0; int download_limit() const; void pause(int flags = 0); diff --git a/toREST/tests/stubs/TorrentContext.cpp b/toREST/tests/stubs/TorrentContext.cpp index b646cba..6b39c38 100644 --- a/toREST/tests/stubs/TorrentContext.cpp +++ b/toREST/tests/stubs/TorrentContext.cpp @@ -36,4 +36,12 @@ void TestTorrent::pause(int flags) { void TestTorrent::resume() { status_.paused = false; -} \ No newline at end of file +} + +void TestTorrent::set_download_limit(int limit) { + download_limit_ = limit; +} + +void TestTorrent::set_upload_limit(int limit) { + upload_limit_ = limit; +} diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp index 12509c3..f61b98c 100644 --- a/toREST/tests/torrent_test.cpp +++ b/toREST/tests/torrent_test.cpp @@ -70,6 +70,8 @@ SCENARIO("We are running a PATCH /session/torrents/id resource") { tr::session::torrents::id::patch(torrent_session, response, request); CommonResponse::accepted(response); REQUIRE(torrent_session.get_torrents().back().status().paused); + REQUIRE(torrent_session.get_torrents().back().download_limit() == 100); + REQUIRE(torrent_session.get_torrents().back().upload_limit() == 109); } } THEN("the request is valid json, but doesn't have any of the required fields") {