6 changed files with 148 additions and 317 deletions
@ -1,68 +0,0 @@ |
|||||||
#include <session.hpp> |
|
||||||
|
|
||||||
namespace session { |
|
||||||
nlohmann::json manager::get_json() const { |
|
||||||
nlohmann::json session_json; |
|
||||||
session_json["id"] = handle->id().to_string(); |
|
||||||
session_json["paused"] = handle->is_paused(); |
|
||||||
session_json["listening"] = handle->is_listening(); |
|
||||||
session_json["port"] = handle->listen_port(); |
|
||||||
session_json["ssl_port"] = handle->ssl_listen_port(); |
|
||||||
return session_json; |
|
||||||
} |
|
||||||
|
|
||||||
bool manager::patch(const nlohmann::json &data) { |
|
||||||
int paused=data["paused"]; |
|
||||||
int port=data["port"]; |
|
||||||
|
|
||||||
const auto pause = [this](int is_paused){ |
|
||||||
if(is_paused!=-1){ |
|
||||||
if(is_paused==handle->is_paused()){ |
|
||||||
return true; |
|
||||||
}else if(is_paused==true){ |
|
||||||
handle->resume(); |
|
||||||
return true; |
|
||||||
}else if(is_paused==false){ |
|
||||||
handle->pause(); |
|
||||||
return true; |
|
||||||
} |
|
||||||
} |
|
||||||
return false; |
|
||||||
}; |
|
||||||
|
|
||||||
const auto listen = [this](int listen_port){ |
|
||||||
if(listen_port!=-1&&listen_port>6880){ |
|
||||||
libtorrent::settings_pack sp; |
|
||||||
sp.set_str(libtorrent::settings_pack::listen_interfaces,"0.0.0.0:"+std::to_string(listen_port)); |
|
||||||
handle->apply_settings(sp); |
|
||||||
return true; |
|
||||||
} |
|
||||||
return false; |
|
||||||
}; |
|
||||||
|
|
||||||
return pause(paused) && listen(port); |
|
||||||
}; |
|
||||||
void resource::patch(const nlohmann::json &data){ |
|
||||||
if(mgr && mgr->is_valid()){ |
|
||||||
if(data.is_object() && !data.is_null()){ |
|
||||||
int is_paused=util::json::get<int>("is_paused",data,-1), |
|
||||||
listen_port=util::json::get<int>("listen_port",data,-1); |
|
||||||
bool at_at_least_one_option_is_set=is_paused!=-1||listen_port!=-1; |
|
||||||
if(at_at_least_one_option_is_set){ |
|
||||||
nlohmann::json patch={{"is_paused",is_paused},{"listen_port",listen_port}}; |
|
||||||
if(mgr->patch(patch)){ |
|
||||||
return response.set_status(http::ok); |
|
||||||
}else{ |
|
||||||
return response.set_status(http::internal_server_error); |
|
||||||
} |
|
||||||
}else{ |
|
||||||
return response.set_status(http::bad_request); |
|
||||||
} |
|
||||||
} |
|
||||||
return response.set_status(http::bad_request); |
|
||||||
// auto data=data.array();
|
|
||||||
}else{ |
|
||||||
return response.set_status(http::service_unavailable); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,76 @@ |
|||||||
|
#include <Catch/fakeit.hpp> |
||||||
|
#include <fakeserver.hpp> |
||||||
|
#include <session.hpp> |
||||||
|
|
||||||
|
using namespace std; |
||||||
|
|
||||||
|
class TestTorrent {}; |
||||||
|
class TestRequest : public std::stringstream {}; |
||||||
|
class TestResponse : public std::stringstream {}; |
||||||
|
|
||||||
|
class TestSessionSettings { |
||||||
|
public: |
||||||
|
const int download_rate_limit = 1; |
||||||
|
const int upload_rate_limit = 2; |
||||||
|
int get_int(int type) const { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
class TestTorrentSession { |
||||||
|
public: |
||||||
|
bool valid = false; |
||||||
|
bool paused = false; |
||||||
|
bool dht_running = true; |
||||||
|
bool is_valid() { |
||||||
|
return valid; |
||||||
|
} |
||||||
|
TestSessionSettings get_settings() { |
||||||
|
return TestSessionSettings(); |
||||||
|
} |
||||||
|
bool is_paused() { |
||||||
|
return paused; |
||||||
|
} |
||||||
|
int listen_port() { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
int is_dht_running() { |
||||||
|
return dht_running; |
||||||
|
} |
||||||
|
std::vector<TestTorrent> get_torrents() const { |
||||||
|
return std::vector<TestTorrent>(); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
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 std::string ok_json = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 82\r\n\r\n{\"dht_enabled\":1,\"down_limit\":0,\"paused\":false,\"port\":0,\"torrents\":0,\"up_limit\":0}"; |
||||||
|
const std::string ok_json_paused = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 81\r\n\r\n{\"dht_enabled\":1,\"down_limit\":0,\"paused\":true,\"port\":0,\"torrents\":0,\"up_limit\":0}"; |
||||||
|
|
||||||
|
SCENARIO("We recive a GET request to the /session resource") { |
||||||
|
auto torrent_session = TestTorrentSession(); |
||||||
|
auto response = std::make_shared<TestResponse>(); |
||||||
|
GIVEN("the session is invalid") { |
||||||
|
auto request = std::make_shared<TestResponse>(); |
||||||
|
tr::session::get(torrent_session, response, request); |
||||||
|
THEN("the server should reply with service unavailable") { |
||||||
|
REQUIRE(response->str() == service_unavailable_json); |
||||||
|
} |
||||||
|
} |
||||||
|
GIVEN("the session is valid") { |
||||||
|
auto request = std::make_shared<TestResponse>(); |
||||||
|
torrent_session.valid = true; |
||||||
|
tr::session::get(torrent_session, response, request); |
||||||
|
THEN("the server should reply with service unavailable") { |
||||||
|
REQUIRE(response->str() == ok_json); |
||||||
|
} |
||||||
|
} |
||||||
|
GIVEN("the session is valid") { |
||||||
|
auto request = std::make_shared<TestResponse>(); |
||||||
|
torrent_session.valid = true; |
||||||
|
torrent_session.paused = true; |
||||||
|
tr::session::get(torrent_session, response, request); |
||||||
|
THEN("the server should reply with service unavailable") { |
||||||
|
REQUIRE(response->str() == ok_json_paused); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue