From ff47d4c68ac8368eaeab6a2f4b1131a92551d6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sun, 14 May 2017 21:03:22 +0200 Subject: [PATCH] add boilerplate for torrent resource --- toREST/include/torrent.hpp | 32 ++++++++++++++++++++++++++++++++ toREST/src/main.cxx | 9 +++++++++ toREST/tests/torrent_test.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 toREST/include/torrent.hpp create mode 100644 toREST/tests/torrent_test.cpp diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp new file mode 100644 index 0000000..8b7c8b3 --- /dev/null +++ b/toREST/include/torrent.hpp @@ -0,0 +1,32 @@ +#ifndef _TR_TORRENT_HPP_ +#define _TR_TORRENT_HPP_ + +#include + +namespace tr { +namespace session { +namespace torrents { +namespace id { +template +void get(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); + } +} + +template +void patch(settings opts, torrent_session &session, response resp, request req) { +} +} +} +} +} +#endif diff --git a/toREST/src/main.cxx b/toREST/src/main.cxx index 7f83fa2..d522ac6 100644 --- a/toREST/src/main.cxx +++ b/toREST/src/main.cxx @@ -6,6 +6,7 @@ #include #include #include +#include #include typedef SimpleWeb::Server HttpServer; @@ -56,6 +57,14 @@ int main(int argc, char *argv[]) { tr::session::torrents::del(session, resp, req); }; + http_server.resource["^/session/torrents/([0-9a-fA-F]){40}(\\?.*)?\\/?$"]["GET"] = [&](std::shared_ptr resp, std::shared_ptr req) { + tr::session::torrents::id::get(session, resp, req); + }; + + 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); + }; + std::thread server_thread([&http_server]() { http_server.start(); }); diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp new file mode 100644 index 0000000..b2adedf --- /dev/null +++ b/toREST/tests/torrent_test.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +SCENARIO("We are running a GET /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") { + WHEN("we recive a request") { + tr::session::torrents::id::get(torrent_session, response, request); + THEN("the server should reply with service unavailable") { + CommonResponse::service_unavailable(response); + } + } + } + GIVEN("the server is working properly") { + torrent_session.valid = true; + THEN("the server should reply with service unavailable") { + tr::session::torrents::id::get(torrent_session, response, request); + CommonResponse::ok(response, {}); + } + } +} \ No newline at end of file