Browse Source

save_path for post torrents resource

master
Jørgen Lien Sellæg 9 years ago
parent
commit
fff0a7f433
  1. 4
      toREST/include/config.hpp
  2. 30
      toREST/include/torrents.hpp
  3. 15
      toREST/tests/include/TorrentContext.hpp
  4. 10
      toREST/tests/stubs/ServerContext.cpp
  5. 2
      toREST/tests/stubs/SessionContext.cpp
  6. 54
      toREST/tests/torrents_test.cpp

4
toREST/include/config.hpp

@ -4,8 +4,8 @@
#include <boost/filesystem.hpp>
struct Config {
const boost::filesystem::path root_dir = "/home/zalox/downloads";
const boost::filesystem::path default_download_dir = root_dir / "toREST";
const boost::filesystem::path root_dir = "/";
const boost::filesystem::path default_download_dir = "toREST";
};
#endif

30
toREST/include/torrents.hpp

@ -3,6 +3,7 @@
#include <http.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <util.hpp>
#include <boost/filesystem.hpp>
namespace tr {
namespace session {
@ -70,28 +71,33 @@ void post(settings opts, torrent_session &session, response resp, request req) {
boost::system::error_code ec;
libtorrent::add_torrent_params params;
libtorrent::parse_magnet_uri(uri, params, ec);
auto magnet = util::uri::parse(uri);
if (ec) {
return respond(http::bad_request);
}
params.save_path = opts.default_download_dir.string();
if (request_object.find("save_path") != request_object.end()) {
boost::filesystem::path save_path = request_object.at("save_path");
if (save_path.is_relative())
params.save_path = (opts.root_dir / save_path).string();
else
return respond(http::bad_request);
} else
params.save_path = boost::filesystem::path(opts.root_dir / opts.default_download_dir).string();
// if (request_object.find("paused") != request_object.end()) {
// }
http_response.add_header({"Location", "/session/torrents/hash"});
session.async_add_torrent(params);
std::stringstream ss;
ss << params.info_hash;
http_response.add_header({"Location", "/session/torrents/" + ss.str()});
return respond(http::created);
// if(ec){
// auto response_code = http::code(http::bad_request);
// http_response.set_body({{"code", response_code.first}, {"status", response_code.second + ec.message()}});
// http_response.set_status(response_code.first);
// }
// if(request_object.find("save_path") != request_object.end())
// params.save_path=request_object.at("save_path");
// else
// params.save_path=opts.default_download_dir.string();
// session.async_add_torrent(params);
// http_response.add_header({"Location",params.info_hash.to_string()});
}

15
toREST/tests/include/TorrentContext.hpp

@ -2,9 +2,15 @@
#define _TR_TEST_TORRENT_CONTEXT_HPP_
#include <string>
#include <libtorrent/magnet_uri.hpp>
class InfoHash {
private:
std::string hash;
public:
std::string to_string() { return "12a"; }
InfoHash(const std::string &h) : hash(h) {}
std::string to_string() { return hash; }
};
class TorrentStatus {
@ -14,13 +20,18 @@ public:
int state = 0;
int priority = 0;
std::string name = "Arch";
std::string save_path;
};
class TestTorrent {
public:
TestTorrent(const libtorrent::add_torrent_params &params) : hash(params.info_hash.to_string()) {
status_.save_path = params.save_path;
}
TestTorrent() : hash("12a") {}
bool is_valid();
InfoHash info_hash();
TorrentStatus status(int type);
TorrentStatus status(int type = 0);
int query_name = 1;
int query_save_path = 2;
bool valid = true;

10
toREST/tests/stubs/ServerContext.cpp

@ -54,6 +54,14 @@ void header_present(std::shared_ptr<TestResponse> response, const std::string &h
REQUIRE(loc_itr->second == content);
}
void header_present(std::shared_ptr<TestResponse> response, const std::string &header) {
const auto loc_itr = response->headers().find(header);
const auto end = response->headers().end();
const auto header_found = loc_itr != end;
REQUIRE(header_found);
REQUIRE(loc_itr->first == header);
}
void is_json_request(std::shared_ptr<TestResponse> response) {
const auto content = response->content();
auto obj = nlohmann::json::parse(content);
@ -90,5 +98,5 @@ void CommonResponse::created(std::shared_ptr<TestResponse> response, std::shared
REQUIRE(response->code() == "201");
REQUIRE(response->message() == "Created");
is_json_request(response);
header_present(response, "Location", "/session/torrents/hash");
header_present(response, "Location", location);
};

2
toREST/tests/stubs/SessionContext.cpp

@ -68,6 +68,6 @@ void TestSession::apply_settings(TestSessionSettings settings) {
};
void TestSession::async_add_torrent(const libtorrent::add_torrent_params &settings) {
TestTorrent torrent;
torrents.emplace_back(settings);
};

54
toREST/tests/torrents_test.cpp

@ -52,6 +52,8 @@ SCENARIO("We are running a POST /session/torrents resource") {
auto settings = Config();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
const std::string magnet_hash = "c0b0a90089710812fe8c37385a4cc2978eabf3e8";
const auto magnet_uri = "magnet:?xt=urn:btih:" + magnet_hash + "&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);
@ -64,30 +66,46 @@ 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:?xt=urn:btih:c0b0a90089710812fe8c37385a4cc2978eabf3e8&dn=Taylor Swift&tr=http://tracker.sout.no"}});
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/hash");
CommonResponse::created(response, request, "/session/torrents/" + magnet_hash);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST");
}
}
GIVEN("the torrent doesn't exist") {
// tr::session::torrents::post(settings, torrent_session, response, request);
// tr::session::torrents::get(torrent_session, response, request);
// CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}});
// }
// }
// GIVEN("we have at least one torrent") {
// auto t_torrent = TestTorrent();
// torrent_session.get_torrents().emplace_back(t_torrent);
// THEN("the server should reply with an array of torrents") {
// tr::session::torrents::get(torrent_session, response, request);
// CommonResponse::ok(response, {{"torrents", {torrent}}});
// };
GIVEN("we specify a absolute save path") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "/music"}});
tr::session::torrents::post(settings, torrent_session, response, request);
THEN("the server should reply with bad request") {
CommonResponse::bad_request(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
}
GIVEN("the torrent exists from before") {
GIVEN("we specify a relative save path") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"}});
tr::session::torrents::post(settings, torrent_session, response, request);
THEN("the server should reply with bad request") {
CommonResponse::created(response, request, "/session/torrents/" + magnet_hash);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
// tr::session::torrents::get(torrent_session, response, request);
// CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}});
// }
// }
// GIVEN("we have at least one torrent") {
// auto t_torrent = TestTorrent();
// torrent_session.get_torrents().emplace_back(t_torrent);
// THEN("the server should reply with an array of torrents") {
// tr::session::torrents::get(torrent_session, response, request);
// CommonResponse::ok(response, {{"torrents", {torrent}}});
// };
}
WHEN("we recive an invalid request") {
GIVEN("the request doesn't contain valid json") {

Loading…
Cancel
Save