Browse Source

wip: add via torrent url

master
Jørgen Lien Sellæg 9 years ago
parent
commit
a24e2f3c2f
  1. 14
      toREST/include/torrents.hpp
  2. 16
      toREST/tests/torrents_test.cpp

14
toREST/include/torrents.hpp

@ -128,17 +128,27 @@ 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);
}
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::add_torrent_params params;
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");

16
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<TestResponse>();
auto request = std::make_shared<TestRequest>();
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,14 +71,25 @@ 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}});
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(
{{"magnet_uri", magnet_uri},

Loading…
Cancel
Save