|
|
|
|
#include <Catch/fakeit.hpp>
|
|
|
|
|
#include <fakeserver.hpp>
|
|
|
|
|
#include <session.hpp>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
class TestTorrent {};
|
|
|
|
|
class TestRequest {
|
|
|
|
|
public:
|
|
|
|
|
std::stringstream content;
|
|
|
|
|
};
|
|
|
|
|
class TestResponse : public std::stringstream {
|
|
|
|
|
public:
|
|
|
|
|
std::string buffer;
|
|
|
|
|
auto string() {
|
|
|
|
|
buffer = str();
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
auto message() {
|
|
|
|
|
if (buffer.empty())
|
|
|
|
|
string();
|
|
|
|
|
auto msg = buffer.substr(13, buffer.find('\r') - 13);
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
auto code() {
|
|
|
|
|
if (buffer.empty())
|
|
|
|
|
string();
|
|
|
|
|
return buffer.substr(9, 3);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestSessionSettings {
|
|
|
|
|
public:
|
|
|
|
|
int download_rate_limit = 1;
|
|
|
|
|
int upload_rate_limit = 2;
|
|
|
|
|
int listen_interfaces = 3;
|
|
|
|
|
int enable_dht = 4;
|
|
|
|
|
bool enable_dht_ = true;
|
|
|
|
|
int download_rate_limit_ = 0;
|
|
|
|
|
int upload_rate_limit_ = 0;
|
|
|
|
|
std::string listen_interfaces_ = "0.0.0.0:0";
|
|
|
|
|
void set_int(int type, int value) {
|
|
|
|
|
if (type == download_rate_limit) {
|
|
|
|
|
download_rate_limit_ = value;
|
|
|
|
|
}
|
|
|
|
|
if (type == upload_rate_limit) {
|
|
|
|
|
upload_rate_limit_ = value;
|
|
|
|
|
}
|
|
|
|
|
if (type == enable_dht) {
|
|
|
|
|
enable_dht_ = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void set_bool(int type, int value) {
|
|
|
|
|
set_int(type, value);
|
|
|
|
|
}
|
|
|
|
|
int get_int(int type) const {
|
|
|
|
|
if (type == download_rate_limit) {
|
|
|
|
|
return download_rate_limit_;
|
|
|
|
|
}
|
|
|
|
|
if (type == upload_rate_limit) {
|
|
|
|
|
return upload_rate_limit_;
|
|
|
|
|
}
|
|
|
|
|
if (type == enable_dht) {
|
|
|
|
|
return enable_dht_;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
bool get_bool(int type) { return get_int(type); }
|
|
|
|
|
std::string get_str(int type) {
|
|
|
|
|
if (type == listen_interfaces) {
|
|
|
|
|
return listen_interfaces_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void set_str(int type, std::string value) {
|
|
|
|
|
if (type == listen_interfaces) {
|
|
|
|
|
listen_interfaces_ = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestTorrentSession {
|
|
|
|
|
public:
|
|
|
|
|
bool valid = false;
|
|
|
|
|
bool paused = false;
|
|
|
|
|
TestSessionSettings settings_;
|
|
|
|
|
bool is_valid() {
|
|
|
|
|
return valid;
|
|
|
|
|
}
|
|
|
|
|
TestSessionSettings get_settings() {
|
|
|
|
|
return settings_;
|
|
|
|
|
}
|
|
|
|
|
bool is_paused() {
|
|
|
|
|
return paused;
|
|
|
|
|
}
|
|
|
|
|
void pause() { paused = true; }
|
|
|
|
|
void resume() { paused = false; }
|
|
|
|
|
int listen_port() {
|
|
|
|
|
return std::stoi(settings_.listen_interfaces_.substr(8, settings_.listen_interfaces_.size() - 8));
|
|
|
|
|
}
|
|
|
|
|
bool is_dht_running() {
|
|
|
|
|
return settings_.enable_dht_;
|
|
|
|
|
}
|
|
|
|
|
std::vector<TestTorrent> get_torrents() const {
|
|
|
|
|
return std::vector<TestTorrent>();
|
|
|
|
|
}
|
|
|
|
|
void apply_settings(TestSessionSettings settings) {
|
|
|
|
|
settings_ = settings;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const std::string bad_request_json = "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: 35\r\n\r\n{\"code\":400,\"status\":\"Bad Request\"}";
|
|
|
|
|
const std::string ok_data = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 85\r\n\r\n{\"dht_enabled\":true,\"down_limit\":0,\"paused\":false,\"port\":0,\"torrents\":0,\"up_limit\":0}";
|
|
|
|
|
const std::string ok_data_paused = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 84\r\n\r\n{\"dht_enabled\":true,\"down_limit\":0,\"paused\":true,\"port\":0,\"torrents\":0,\"up_limit\":0}";
|
|
|
|
|
const std::string accepted = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 32\r\n\r\n{\"code\":202,\"status\":\"Accepted\"}";
|
|
|
|
|
|
|
|
|
|
const std::string service_unavailable_json = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: application/json\r\nContent-Length: 43\r\n\r\n{\"code\":503,\"status\":\"Service Unavailable\"}";
|
|
|
|
|
const auto reply_is_service_unavailable = [](std::shared_ptr<TestResponse> response) {
|
|
|
|
|
REQUIRE(response->string() == service_unavailable_json);
|
|
|
|
|
REQUIRE(response->code() == "503");
|
|
|
|
|
REQUIRE(response->message() == "Service Unavailable");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto reply_is_200_ok = [](std::shared_ptr<TestResponse> response) {
|
|
|
|
|
REQUIRE(response->code() == "200");
|
|
|
|
|
REQUIRE(response->message() == "OK");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SCENARIO("We are running a GET /session resource") {
|
|
|
|
|
auto torrent_session = TestTorrentSession();
|
|
|
|
|
auto response = std::make_shared<TestResponse>();
|
|
|
|
|
auto request = std::make_shared<TestRequest>();
|
|
|
|
|
GIVEN("the server is not working properly") {
|
|
|
|
|
AND_WHEN("we recive a request") {
|
|
|
|
|
tr::session::get(torrent_session, response, request);
|
|
|
|
|
THEN("the server should reply with service unavailable") {
|
|
|
|
|
reply_is_service_unavailable(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
GIVEN("the server is working properly") {
|
|
|
|
|
torrent_session.valid = true;
|
|
|
|
|
AND_WHEN("the session is paused the paused field is set to true") {
|
|
|
|
|
torrent_session.paused = true;
|
|
|
|
|
tr::session::get(torrent_session, response, request);
|
|
|
|
|
THEN("the server should reply with resource data") {
|
|
|
|
|
REQUIRE(response->string() == ok_data_paused);
|
|
|
|
|
reply_is_200_ok(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SCENARIO("We recive a PATCH request on the /session resource") {
|
|
|
|
|
auto torrent_session = TestTorrentSession();
|
|
|
|
|
auto response = std::make_shared<TestResponse>();
|
|
|
|
|
auto request = std::make_shared<TestRequest>();
|
|
|
|
|
GIVEN("the server is not working properly") {
|
|
|
|
|
tr::session::patch(torrent_session, response, request);
|
|
|
|
|
THEN("the server should reply with service unavailable") {
|
|
|
|
|
reply_is_service_unavailable(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
GIVEN("the server is working properly") {
|
|
|
|
|
torrent_session.valid = true;
|
|
|
|
|
GIVEN("the server recives invalid request") {
|
|
|
|
|
request->content << "Not valid json";
|
|
|
|
|
tr::session::patch(torrent_session, response, request);
|
|
|
|
|
THEN("the server should respond with bad request") {
|
|
|
|
|
REQUIRE(response->string() == bad_request_json);
|
|
|
|
|
REQUIRE(response->code() == "400");
|
|
|
|
|
REQUIRE(response->message() == "Bad Request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
GIVEN("the server recives a valid request") {
|
|
|
|
|
const nlohmann::json obj({
|
|
|
|
|
{"dht_enabled", false},
|
|
|
|
|
{"paused", true},
|
|
|
|
|
{"port", 1},
|
|
|
|
|
{"down_limit", 100},
|
|
|
|
|
{"up_limit", 100},
|
|
|
|
|
});
|
|
|
|
|
request->content << obj;
|
|
|
|
|
REQUIRE_FALSE(torrent_session.paused);
|
|
|
|
|
REQUIRE(torrent_session.settings_.enable_dht_);
|
|
|
|
|
REQUIRE(torrent_session.listen_port() == 0);
|
|
|
|
|
REQUIRE(torrent_session.settings_.download_rate_limit_ == 0);
|
|
|
|
|
REQUIRE(torrent_session.settings_.upload_rate_limit_ == 0);
|
|
|
|
|
tr::session::patch(torrent_session, response, request);
|
|
|
|
|
REQUIRE(response->string() == accepted);
|
|
|
|
|
REQUIRE(torrent_session.paused);
|
|
|
|
|
REQUIRE_FALSE(torrent_session.settings_.enable_dht_);
|
|
|
|
|
REQUIRE(torrent_session.listen_port() == 1);
|
|
|
|
|
REQUIRE(torrent_session.settings_.download_rate_limit_ == 100);
|
|
|
|
|
REQUIRE(torrent_session.settings_.upload_rate_limit_ == 100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// GIVEN("the session is paused") {
|
|
|
|
|
// auto request = std::make_shared<TestRequest>();
|
|
|
|
|
// torrent_session.valid = true;
|
|
|
|
|
// torrent_session.paused = true;
|
|
|
|
|
// tr::session::patch(torrent_session, response, request);
|
|
|
|
|
// THEN("the server should reply with service unavailable") {
|
|
|
|
|
// REQUIRE(response->string() == ok_data);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|