From 250f531b8a8c0251e751ef9118de34d1bf0eeebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 15 May 2017 16:17:16 +0200 Subject: [PATCH] Factor out torrent to json code and add util test --- toREST/include/torrent.hpp | 19 +++++++++++++++ toREST/include/torrents.hpp | 45 ++++++++++++++--------------------- toREST/include/util.hpp | 2 -- toREST/tests/torrent_test.cpp | 4 +--- toREST/tests/util_test.cpp | 25 +++++++++++++++++++ 5 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 toREST/tests/util_test.cpp diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 3f0b73f..6e0bdb6 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -4,6 +4,25 @@ #include #include #include +#include + +template +static nlohmann::json to_json(torrent_t torrent) { + std::stringstream ss; // TODO optimize + ss << torrent.info_hash(); + const std::string hash = ss.str(); + const auto status = torrent.status( + torrent.query_name | + torrent.query_save_path); + return {{"info_hash", hash}, + {"paused", status.paused}, + {"seeding", status.is_seeding}, + {"state", status.state}, + {"priority", status.priority}, + {"up_limit", torrent.upload_limit()}, + {"down_limit", torrent.download_limit()}, + {"name", status.name}}; +} namespace tr { namespace session { diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index c2e14c6..187aa8d 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace tr { @@ -11,36 +12,26 @@ namespace torrents { template void get(torrent_session &session, response resp, request req) { auto http_response = http::response(); - if (session.is_valid()) { - auto torrents = session.get_torrents(); - auto response_object = nlohmann::json::object(); - auto torrents_json = nlohmann::json::array(); - for (auto &torrent : torrents) { - if (torrent.is_valid()) { - std::stringstream ss; - ss << torrent.info_hash(); - const std::string hash = ss.str(); - const auto status = torrent.status( - torrent.query_name | - torrent.query_save_path); - torrents_json.push_back({{"info_hash", hash}, - {"paused", status.paused}, - {"seeding", status.is_seeding}, - {"state", status.state}, - {"priority", status.priority}, - {"up_limit", torrent.upload_limit()}, - {"down_limit", torrent.download_limit()}, - {"name", status.name}}); - } - } - response_object["torrents"] = torrents_json; - http_response.set_status(http::ok); - http_response.set_body(response_object); - } else { - auto response_code = http::code(http::service_unavailable); + 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); + } + auto torrents = session.get_torrents(); + auto response_object = nlohmann::json::object(); + auto torrents_json = nlohmann::json::array(); + for (auto &torrent : torrents) { + if (torrent.is_valid()) { + torrents_json.push_back(to_json(torrent)); + } } + response_object["torrents"] = torrents_json; + http_response.set_status(http::ok); + http_response.set_body(response_object); *resp << http_response; } diff --git a/toREST/include/util.hpp b/toREST/include/util.hpp index 23c6d05..8528700 100644 --- a/toREST/include/util.hpp +++ b/toREST/include/util.hpp @@ -1,7 +1,6 @@ #ifndef _TR_UTIL_HPP_ #define _TR_UTIL_HPP_ -#include #include #include @@ -39,7 +38,6 @@ public: return options; } }; -const boost::regex regex("@(https?|ftp)://(-\\.)?([^\\s/?\\.#-]+\\.?)+(/[^\\s]*)?$@", boost::regex_constants::perl | boost::regex_constants::icase); class json { public: diff --git a/toREST/tests/torrent_test.cpp b/toREST/tests/torrent_test.cpp index 3824f0d..e44e036 100644 --- a/toREST/tests/torrent_test.cpp +++ b/toREST/tests/torrent_test.cpp @@ -23,9 +23,7 @@ SCENARIO("We are running a GET /session/torrents/id resource") { torrent_session.theTorrentExists(); THEN("the response should be a json representation of the torrent") { tr::session::torrents::id::get(torrent_session, response, request); - CommonResponse::ok(response, - {// {"paused", true}, - {"info_hash", torrent_id}}); + // CommonResponse::ok(response,{torrent_json}); } } } diff --git a/toREST/tests/util_test.cpp b/toREST/tests/util_test.cpp new file mode 100644 index 0000000..f29bcd5 --- /dev/null +++ b/toREST/tests/util_test.cpp @@ -0,0 +1,25 @@ +#include +#include + +SCENARIO("we want to parse a request uri with one field") { + std::string request_uri = "/session?fields=id,trains"; + WHEN("we parse the uri") { + auto result = util::uri::parse(request_uri); + THEN("the map should contain structured data based ont the uri") { + REQUIRE(result.size() == 1); + REQUIRE(result["fields"] == "id,trains"); + } + } +} + +SCENARIO("we want to parse a request uri with more field") { + std::string request_uri = "/session?fields=id,trains&sort=desc"; + WHEN("we parse the uri") { + auto result = util::uri::parse(request_uri); + THEN("the map should contain structured data based ont the uri") { + REQUIRE(result.size() == 2); + REQUIRE(result["fields"] == "id,trains"); + REQUIRE(result["sort"] == "desc"); + } + } +}