Browse Source

set download/upload on torrent resource

master
Jørgen Lien Sellæg 9 years ago
parent
commit
07b96ad817
  1. 40
      toREST/include/torrent.hpp
  2. 2
      toREST/tests/include/TorrentContext.hpp
  3. 10
      toREST/tests/stubs/TorrentContext.cpp
  4. 2
      toREST/tests/torrent_test.cpp

40
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);
}
}

2
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);

10
toREST/tests/stubs/TorrentContext.cpp

@ -36,4 +36,12 @@ void TestTorrent::pause(int flags) {
void TestTorrent::resume() {
status_.paused = false;
}
}
void TestTorrent::set_download_limit(int limit) {
download_limit_ = limit;
}
void TestTorrent::set_upload_limit(int limit) {
upload_limit_ = limit;
}

2
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") {

Loading…
Cancel
Save