diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp new file mode 100644 index 0000000..c0cc06d --- /dev/null +++ b/toREST/tests/include/SessionContext.hpp @@ -0,0 +1,52 @@ +#include +#include + +class TestTorrent {}; + +class TestRequest { +public: + std::stringstream content; +}; + +class TestResponse : public std::stringstream { +public: + std::string buffer; + std::string string(); + std::string message(); + std::string code(); +}; + +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_bool(int type, int value); + void set_int(int type, int value); + void set_str(int type, std::string value); + int get_int(int type) const; + bool get_bool(int type); + std::string get_str(int type); + +}; + +class TestTorrentSession { +public: + bool valid = false; + bool paused = false; + TestSessionSettings settings_; + bool is_valid(); + TestSessionSettings get_settings(); + bool is_paused() const; + void pause(); + void resume(); + int listen_port(); + bool is_dht_running() const; + std::vector get_torrents() const; + void apply_settings(TestSessionSettings settings); +}; diff --git a/toREST/tests/session_test.cpp b/toREST/tests/session_test.cpp index 625960e..44a3209 100644 --- a/toREST/tests/session_test.cpp +++ b/toREST/tests/session_test.cpp @@ -1,119 +1,13 @@ #include -#include #include - -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 get_torrents() const { - return std::vector(); - } - void apply_settings(TestSessionSettings settings) { - settings_ = settings; - }; -}; +#include 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 response) { REQUIRE(response->string() == service_unavailable_json); REQUIRE(response->code() == "503"); @@ -194,13 +88,4 @@ SCENARIO("We recive a PATCH request on the /session resource") { REQUIRE(torrent_session.settings_.upload_rate_limit_ == 100); } } - // GIVEN("the session is paused") { - // auto request = std::make_shared(); - // 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); - // } - // } } diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp new file mode 100644 index 0000000..4d84824 --- /dev/null +++ b/toREST/tests/stubs/SessionContext.cpp @@ -0,0 +1,83 @@ +#include +#include +#include + +std::string TestResponse::string() { + buffer = str(); + return buffer; +} +std::string TestResponse::message() { + if (buffer.empty()) + string(); + auto msg = buffer.substr(13, buffer.find('\r') - 13); + return msg; +} +std::string TestResponse::code() { + if (buffer.empty()) + string(); + return buffer.substr(9, 3); +} +void TestSessionSettings::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 TestSessionSettings::set_bool(int type, int value) { + set_int(type, value); +} +int TestSessionSettings::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 TestSessionSettings::get_bool(int type) { return get_int(type); } +std::string TestSessionSettings::get_str(int type) { + if (type == listen_interfaces) { + return listen_interfaces_; + } +} +void TestSessionSettings::set_str(int type, std::string value) { + if (type == listen_interfaces) { + listen_interfaces_ = value; + } +} +bool TestTorrentSession::is_valid() { + return valid; +} +TestSessionSettings TestTorrentSession::get_settings() { + return settings_; +} +bool TestTorrentSession::is_paused() const { + return paused; +} +void TestTorrentSession::pause() { + paused = true; +} +void TestTorrentSession::resume() { + paused = false; +} +int TestTorrentSession::listen_port() { + return std::stoi(settings_.listen_interfaces_.substr(8, settings_.listen_interfaces_.size() - 8)); +} +bool TestTorrentSession::is_dht_running() const { + return settings_.enable_dht_; +} +std::vector TestTorrentSession::get_torrents() const { + return std::vector(); +} +void TestTorrentSession::apply_settings(TestSessionSettings settings) { + settings_ = settings; +};