From e285e768d01a36a73f658ff6c6c7e564e14d4bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 15 May 2017 17:11:58 +0200 Subject: [PATCH] prototype patch resource on torrent --- toREST/include/torrent.hpp | 15 +++++++++++++-- toREST/src/main.cxx | 2 +- toREST/tests/torrent_test.cpp | 22 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 35eac56..1019097 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -54,8 +54,19 @@ void get(torrent_session &session, response resp, request req) { *resp << http_response; } -template -void patch(settings opts, torrent_session &session, response resp, request req) { +template +void patch(torrent_session &session, response resp, request req) { + auto http_response = http::response(); + const auto respond = [&](http::status status) { + const auto response_code = http::code(status); + http_response.set_body({{"code", response_code.first}, {"status", response_code.second}}); + http_response.set_status(response_code.first); + *resp << http_response; + }; + + if (!session.is_valid()) { + return respond(http::service_unavailable); + } } } } diff --git a/toREST/src/main.cxx b/toREST/src/main.cxx index d522ac6..f50127c 100644 --- a/toREST/src/main.cxx +++ b/toREST/src/main.cxx @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) { }; http_server.resource["^/session/torrents/([0-9a-fA-F]){40}(\\?.*)?\\/?$"]["PATCH"] = [&](std::shared_ptr resp, std::shared_ptr req) { - tr::session::torrents::id::patch(options, session, resp, req); + tr::session::torrents::id::patch(session, resp, req); }; std::thread server_thread([&http_server]() { diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp index b6c0e00..c1ad580 100644 --- a/toREST/tests/torrent_test.cpp +++ b/toREST/tests/torrent_test.cpp @@ -25,7 +25,27 @@ SCENARIO("We are running a GET /session/torrents/id resource") { tr::session::torrents::id::get(torrent_session, response, request); CommonResponse::ok(response,{torrent_json}); } + WHEN("The torrent isn't valid"){ + torrent_session.torrents_.back().valid = false; + THEN("the server should reply with bad request"){ + tr::session::torrents::id::get(torrent_session, response, request); + CommonResponse::bad_request(response); + } + } } } } -} \ No newline at end of file +} + + +SCENARIO("We are running a PATCH /session/torrents/id resource") { + auto torrent_session = TestSession(); + auto response = std::make_shared(); + auto request = std::make_shared(); + GIVEN("the server is not working properly and we recive a reqest") { + THEN("the server should reply with service unavailable") { + tr::session::torrents::id::patch(torrent_session, response, request); + CommonResponse::service_unavailable(response); + } + } +}