Browse Source

Refactor torrent representation in tests

master
Jørgen Lien Sellæg 9 years ago
parent
commit
7cbb6a5253
  1. 9
      toREST/include/torrent.hpp
  2. 8
      toREST/include/torrents.hpp
  3. 1
      toREST/tests/include/ServerContext.hpp
  4. 13
      toREST/tests/include/SessionContext.hpp
  5. 14
      toREST/tests/include/TorrentContext.hpp
  6. 11
      toREST/tests/session_test.cpp
  7. 11
      toREST/tests/stubs/ServerContext.cpp
  8. 11
      toREST/tests/stubs/SessionContext.cpp
  9. 28
      toREST/tests/torrent_test.cpp
  10. 35
      toREST/tests/torrents_test.cpp

9
toREST/include/torrent.hpp

@ -1,7 +1,9 @@
#ifndef _TR_TORRENT_HPP_
#define _TR_TORRENT_HPP_
#include <boost/filesystem.hpp>
#include <http.hpp>
#include <util.hpp>
namespace tr {
namespace session {
@ -20,6 +22,13 @@ void get(torrent_session &session, response resp, request req) {
if (!session.is_valid()) {
return respond(http::service_unavailable);
}
std::string hash = req->path.substr(18, req->path.size() - 18);
http_response.set_body(nlohmann::json::object(
{{"info_hash", hash}}));
http_response.set_status(http::ok);
*resp << http_response;
}
template <class settings, class torrent_session, class request, class response>

8
toREST/include/torrents.hpp

@ -17,15 +17,19 @@ void get(torrent_session &session, response resp, request req) {
auto torrents_json = nlohmann::json::array();
for (auto &torrent : torrents) {
if (torrent.is_valid()) {
const auto hash = torrent.info_hash().to_string();
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({{"hash", hash},
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}});
}
}

1
toREST/tests/include/ServerContext.hpp

@ -9,6 +9,7 @@
class TestRequest {
public:
std::stringstream content;
std::string path;
};
class TestResponse : public std::stringstream {

13
toREST/tests/include/SessionContext.hpp

@ -42,6 +42,19 @@ public:
void remove_torrent(const TestTorrent &torrent, int options);
void apply_settings(TestSessionSettings settings);
void async_add_torrent(const libtorrent::add_torrent_params &params);
void theTorrentExists();
};
const auto magnet_uri = "magnet:?xt=urn:btih:" + torrent_id + "&dn=Taylor Swift&tr=http://tracker.sout.no";
const nlohmann::json session_json = {
{"dht_enabled", true},
{"down_limit", 0},
{"paused", false},
{"port", 0},
{"torrents", 0},
{"up_limit", 0}};
#endif

14
toREST/tests/include/TorrentContext.hpp

@ -1,6 +1,7 @@
#ifndef _TR_TEST_TORRENT_CONTEXT_HPP_
#define _TR_TEST_TORRENT_CONTEXT_HPP_
#include <json.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <string>
@ -33,4 +34,17 @@ public:
TorrentStatus status_;
};
const auto torrent_url = "http://sout.no/torrent.torrent";
const std::string torrent_id = "c0b0a90089710812fe8c37385a4cc2978eabf3e8";
const nlohmann::json torrent_json = {
{"info_hash", torrent_id},
{"paused", false},
{"seeding", false},
{"state", 0},
{"priority", 0},
{"up_limit", 0},
{"down_limit", 0},
{"name", "Arch"}};
#endif

11
toREST/tests/session_test.cpp

@ -3,9 +3,6 @@
#include <SessionContext.hpp>
#include <session.hpp>
const nlohmann::json ok_data = {{"dht_enabled", true}, {"down_limit", 0}, {"paused", false}, {"port", 0}, {"torrents", 0}, {"up_limit", 0}};
const nlohmann::json ok_data_paused = {{"dht_enabled", true}, {"down_limit", 0}, {"paused", true}, {"port", 0}, {"torrents", 0}, {"up_limit", 0}};
SCENARIO("We are running a GET /session resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
@ -20,17 +17,19 @@ SCENARIO("We are running a GET /session resource") {
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
auto data = session_json;
WHEN("the server is paused") {
torrent_session.paused = true;
tr::session::get(torrent_session, response, request);
data.at("paused") = true;
THEN("the server should reply with the paused field set to true") {
CommonResponse::ok(response, ok_data_paused);
tr::session::get(torrent_session, response, request);
CommonResponse::ok(response, data);
}
}
WHEN("the server is not paused") {
tr::session::get(torrent_session, response, request);
THEN("the server should reply with the paused field set to true") {
CommonResponse::ok(response, ok_data);
CommonResponse::ok(response, session_json);
}
}
}

11
toREST/tests/stubs/ServerContext.cpp

@ -62,9 +62,12 @@ void header_present(std::shared_ptr<TestResponse> response, const std::string &h
REQUIRE(loc_itr->first == header);
}
void is_json_response(std::shared_ptr<TestResponse> response) {
void is_json_response(std::shared_ptr<TestResponse> response, nlohmann::json data = nullptr) {
const auto content = response->content();
auto obj = nlohmann::json::parse(content);
nlohmann::json obj = nlohmann::json::parse(response->content());
if (data != nullptr) {
REQUIRE(data == obj);
}
REQUIRE(obj.is_object());
header_present(response, "Content-Type", "application/json");
header_present(response, "Content-Length", std::to_string(content.length()));
@ -77,11 +80,9 @@ void CommonResponse::service_unavailable(std::shared_ptr<TestResponse> response)
};
void CommonResponse::ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data) {
std::string data_dump(data.dump());
REQUIRE(response->code() == "200");
REQUIRE(response->message() == "OK");
REQUIRE(response->content() == data_dump);
is_json_response(response);
is_json_response(response, data);
};
void CommonResponse::no_content(std::shared_ptr<TestResponse> response) {

11
toREST/tests/stubs/SessionContext.cpp

@ -3,6 +3,17 @@
#include <Catch/catch.hpp>
#include <vector>
void TestSession::theTorrentExists() {
torrents_.emplace_back();
libtorrent::sha1_hash sha1_hash;
std::stringstream ss;
ss << torrent_id;
ss >> sha1_hash;
torrents_.back().hash_ = sha1_hash;
torrents_.back().valid = true;
REQUIRE(get_torrents().size() == 1);
}
void TestSessionSettings::set_int(int type, int value) {
if (type == download_rate_limit) {
download_rate_limit_ = value;

28
toREST/tests/torrent_test.cpp

@ -3,23 +3,31 @@
#include <TorrentContext.hpp>
#include <torrent.hpp>
SCENARIO("We are running a GET /session/torrents/id resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
WHEN("we recive a request") {
GIVEN("the server is not working properly and we recive a reqest") {
THEN("the server should reply with service unavailable") {
tr::session::torrents::id::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
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, {});
auto torrent = TestTorrent();
torrent_session.valid = true;
GIVEN("we have the server is working properly and we recive a request") {
WHEN("the request path is a proper id") {
request->path = "/session/torrents/" + torrent_id;
GIVEN("we have the torrent") {
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}});
}
}
}
}
}

35
toREST/tests/torrents_test.cpp

@ -4,16 +4,6 @@
#include <SessionContext.hpp>
#include <torrents.hpp>
using namespace std;
const nlohmann::json torrent = {
{"hash", "12a12a12a12a12a12a12"},
{"paused", false},
{"seeding", false},
{"state", 0},
{"priority", 0},
{"name", "Arch"}};
SCENARIO("We are running a GET /session/torrents resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
@ -36,24 +26,16 @@ SCENARIO("We are running a GET /session/torrents resource") {
}
}
GIVEN("we have at least one torrent") {
auto t_torrent = TestTorrent();
libtorrent::sha1_hash sha;
sha.assign("12a12a12a12a12a12a12");
t_torrent.hash_ = sha;
torrent_session.get_torrents().emplace_back(t_torrent);
torrent_session.theTorrentExists();
THEN("the server should reply with an array of torrents") {
tr::session::torrents::get(torrent_session, response, request);
CommonResponse::ok(response, {{"torrents", {torrent}}});
CommonResponse::ok(response, {{"torrents", {torrent_json}}});
};
}
}
}
}
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();
@ -187,17 +169,6 @@ SCENARIO("We are running a POST /session/torrents resource") {
}
}
void theTorrentExists(TestSession &torrent_session) {
torrent_session.torrents_.emplace_back();
libtorrent::sha1_hash sha1_hash;
std::stringstream ss;
ss << torrent_id;
ss >> sha1_hash;
torrent_session.torrents_[0].hash_ = sha1_hash;
torrent_session.torrents_[0].valid = true;
REQUIRE(torrent_session.get_torrents().size() == 1);
};
SCENARIO("We are running a DELETE /session/torrents resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
@ -236,7 +207,7 @@ SCENARIO("We are running a DELETE /session/torrents resource") {
}
GIVEN("fields are correct") {
GIVEN("the torrent exists") {
theTorrentExists(torrent_session);
torrent_session.theTorrentExists();
THEN("the server should reply no content") {
request->content << nlohmann::json({{"info_hash", torrent_id}});
tr::session::torrents::del(torrent_session, response, request);

Loading…
Cancel
Save