From a24e2f3c2f9fac8aecd6153ef9ff2d8a379fb3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sun, 14 May 2017 20:36:41 +0200 Subject: [PATCH] wip: add via torrent url --- toREST/include/torrents.hpp | 22 ++++++++++++++++------ toREST/tests/torrents_test.cpp | 26 +++++++++++++++++++------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 1376396..c0da13b 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -128,18 +128,28 @@ void post(settings opts, torrent_session &session, response resp, request req) { return respond(http::bad_request); } - if (request_object.find("magnet_uri") == request_object.end()) { + auto magnet_uri_itr = request_object.find("magnet_uri"); + auto url_itr = request_object.end(); + + if (magnet_uri_itr == request_object.end() && url_itr == request_object.end()) { return respond(http::bad_request); } - std::string uri = request_object.at("magnet_uri"); - boost::system::error_code ec; - libtorrent::add_torrent_params params; - libtorrent::parse_magnet_uri(uri, params, ec); - if (ec) { + if (magnet_uri_itr != request_object.end() && url_itr != request_object.end()) { return respond(http::bad_request); } + libtorrent::add_torrent_params params; + + if (magnet_uri_itr != request_object.end()) { + std::string uri = request_object.at("magnet_uri"); + boost::system::error_code ec; + libtorrent::parse_magnet_uri(uri, params, ec); + if (ec) { + return respond(http::bad_request); + } + } + if (request_object.find("save_path") != request_object.end()) { boost::filesystem::path save_path = request_object.at("save_path"); if (save_path.is_relative()) diff --git a/toREST/tests/torrents_test.cpp b/toREST/tests/torrents_test.cpp index f3d3de8..f6c6b5c 100644 --- a/toREST/tests/torrents_test.cpp +++ b/toREST/tests/torrents_test.cpp @@ -51,13 +51,14 @@ SCENARIO("We are running a GET /session/torrents resource") { } const std::string torrent_id = "c0b0a90089710812fe8c37385a4cc2978eabf3e8"; +const auto magnet_uri = "magnet:?xt=urn:btih:" + torrent_id + "&dn=Taylor Swift&tr=http://tracker.sout.no"; +const auto torrent_url = "http://sout.no/torrent.torrent"; SCENARIO("We are running a POST /session/torrents resource") { auto torrent_session = TestSession(); auto settings = Config(); auto response = std::make_shared(); auto request = std::make_shared(); - const auto magnet_uri = "magnet:?xt=urn:btih:" + torrent_id + "&dn=Taylor Swift&tr=http://tracker.sout.no"; GIVEN("the server is not working properly") { AND_WHEN("we recive a request") { tr::session::torrents::post(settings, torrent_session, response, request); @@ -70,13 +71,24 @@ SCENARIO("We are running a POST /session/torrents resource") { torrent_session.valid = true; WHEN("we recive a valid request") { GIVEN("we use the default download directory") { - request->content << nlohmann::json::object({{"magnet_uri", magnet_uri}}); - THEN("the server should reply with created") { - tr::session::torrents::post(settings, torrent_session, response, request); - CommonResponse::created(response, request, "/session/torrents/" + torrent_id); - REQUIRE(torrent_session.get_torrents().size() == 1); - REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST"); + AND_WHEN("we fill the magnet uri field"){ + THEN("the server should reply with created") { + request->content << nlohmann::json::object({{"magnet_uri", magnet_uri}}); + tr::session::torrents::post(settings, torrent_session, response, request); + CommonResponse::created(response, request, "/session/torrents/" + torrent_id); + REQUIRE(torrent_session.get_torrents().size() == 1); + REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST"); + } } + // AND_WHEN("we fill the magnet url field"){ + // THEN("the server should reply with created") { + // request->content << nlohmann::json::object({{"url", torrent_url}}); + // tr::session::torrents::post(settings, torrent_session, response, request); + // CommonResponse::created(response, request, "/session/torrents/" + std::string("0000000000000000000000000000000000000000")); + // REQUIRE(torrent_session.get_torrents().size() == 1); + // REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST"); + // } + // } } GIVEN("we specify a absolute save path") { request->content << nlohmann::json::object(