From 304fe0f672b57c813ef90481fd62abb22be15189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 1 May 2017 19:57:16 +0200 Subject: [PATCH] Add more tests and fix accepted bug --- toREST/include/session.hpp | 1 + toREST/tests/include/ServerContext.hpp | 29 ++++++++++++++++ toREST/tests/include/SessionContext.hpp | 13 -------- toREST/tests/include/TorrentContext.hpp | 4 ++- toREST/tests/session_test.cpp | 44 ++++++++++--------------- toREST/tests/stubs/ServerContext.cpp | 43 ++++++++++++++++++++++++ toREST/tests/stubs/SessionContext.cpp | 17 +--------- toREST/tests/torrents_test.hpp | 30 +++++++++++++++++ 8 files changed, 124 insertions(+), 57 deletions(-) create mode 100644 toREST/tests/include/ServerContext.hpp create mode 100644 toREST/tests/stubs/ServerContext.cpp create mode 100644 toREST/tests/torrents_test.hpp diff --git a/toREST/include/session.hpp b/toREST/include/session.hpp index 056078b..e60fd6c 100644 --- a/toREST/include/session.hpp +++ b/toREST/include/session.hpp @@ -57,6 +57,7 @@ namespace tr{ session.apply_settings(settings); auto response_code = http::code(http::accepted); http_response.set_body({{"code",response_code.first},{"status", response_code.second}}); + http_response.set_status(response_code.first); } else { auto response_code = http::code(http::bad_request); http_response.set_body({{"code",response_code.first},{"status", response_code.second}}); diff --git a/toREST/tests/include/ServerContext.hpp b/toREST/tests/include/ServerContext.hpp new file mode 100644 index 0000000..3b86862 --- /dev/null +++ b/toREST/tests/include/ServerContext.hpp @@ -0,0 +1,29 @@ +#ifndef _TR_TEST_SERVER_CONTEXT_HPP_ +#define _TR_TEST_SERVER_CONTEXT_HPP_ +#include +#include +#include +#include + +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 CommonResponse { +public: + static void service_unavailable(std::shared_ptr response); + static void ok(std::shared_ptr response, const nlohmann::json &data); + static void bad_request(std::shared_ptr response); + static void accepted(std::shared_ptr response); +}; + +#endif \ No newline at end of file diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index 923e83e..b4fa02e 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -5,19 +5,6 @@ #include #include -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; diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp index b8b3a7f..b1a9cbb 100644 --- a/toREST/tests/include/TorrentContext.hpp +++ b/toREST/tests/include/TorrentContext.hpp @@ -1,6 +1,8 @@ #ifndef _TR_TEST_TORRENT_CONTEXT_HPP_ #define _TR_TEST_TORRENT_CONTEXT_HPP_ -class TestTorrent {}; +class TestTorrent { + +}; #endif \ No newline at end of file diff --git a/toREST/tests/session_test.cpp b/toREST/tests/session_test.cpp index 36b4390..cff486f 100644 --- a/toREST/tests/session_test.cpp +++ b/toREST/tests/session_test.cpp @@ -1,23 +1,10 @@ #include -#include +#include #include +#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"); - REQUIRE(response->message() == "Service Unavailable"); -}; - -const auto reply_is_200_ok = [](std::shared_ptr response) { - REQUIRE(response->code() == "200"); - REQUIRE(response->message() == "OK"); -}; +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(); @@ -27,18 +14,23 @@ SCENARIO("We are running a GET /session resource") { 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); + CommonResponse::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") { + WHEN("the server is paused") { 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); + THEN("the server should reply with the paused field set to true") { + CommonResponse::ok(response, ok_data_paused); + } + } + 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); } } } @@ -51,7 +43,7 @@ SCENARIO("We recive a PATCH request on the /session resource") { 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); + CommonResponse::service_unavailable(response); } } GIVEN("the server is working properly") { @@ -60,9 +52,7 @@ SCENARIO("We recive a PATCH request on the /session resource") { 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"); + CommonResponse::bad_request(response); } } GIVEN("the server recives a valid request") { @@ -80,7 +70,7 @@ SCENARIO("We recive a PATCH request on the /session resource") { 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); + CommonResponse::accepted(response); REQUIRE(torrent_session.paused); REQUIRE_FALSE(torrent_session.settings_.enable_dht_); REQUIRE(torrent_session.listen_port() == 1); diff --git a/toREST/tests/stubs/ServerContext.cpp b/toREST/tests/stubs/ServerContext.cpp new file mode 100644 index 0000000..77e4896 --- /dev/null +++ b/toREST/tests/stubs/ServerContext.cpp @@ -0,0 +1,43 @@ +#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 CommonResponse::service_unavailable(std::shared_ptr response) { + 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\"}"; + REQUIRE(response->string() == service_unavailable_json); + REQUIRE(response->code() == "503"); + REQUIRE(response->message() == "Service Unavailable"); +}; +void CommonResponse::ok(std::shared_ptr response, const nlohmann::json &data) { + std::string str(data.dump()); + const std::string res = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: " + std::to_string(str.size()) + "\r\n\r\n" + str; + REQUIRE(response->code() == "200"); + REQUIRE(response->message() == "OK"); + REQUIRE(response->string() == res); +}; +void CommonResponse::bad_request(std::shared_ptr response) { + 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\"}"; + REQUIRE(response->string() == bad_request_json); + REQUIRE(response->code() == "400"); + REQUIRE(response->message() == "Bad Request"); +}; +void CommonResponse::accepted(std::shared_ptr response) { + const std::string accepted = "HTTP/1.1 202 Accepted\r\nContent-Type: application/json\r\nContent-Length: 32\r\n\r\n{\"code\":202,\"status\":\"Accepted\"}"; + REQUIRE(response->string() == accepted); + REQUIRE(response->code() == "202"); + REQUIRE(response->message() == "Accepted"); +}; diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index 421e2c1..d51d237 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -1,22 +1,7 @@ +#include #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; diff --git a/toREST/tests/torrents_test.hpp b/toREST/tests/torrents_test.hpp new file mode 100644 index 0000000..296b9fd --- /dev/null +++ b/toREST/tests/torrents_test.hpp @@ -0,0 +1,30 @@ +#include +#include +#include + +using namespace std; + +SCENARIO("We are running a GET /session resource") { + auto torrent_session = TestSession(); + auto response = std::make_shared(); + auto request = std::make_shared(); + 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); + } + } + } +} \ No newline at end of file