From 78bda9e3e9d2d4f3b2bc999047817495d6810e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 1 May 2017 19:04:14 +0200 Subject: [PATCH] More clean up of old imp --- toREST/include/util.hpp | 10 ---- toREST/tests/http_test.cpp | 53 ------------------- toREST/tests/include/SessionContext.hpp | 10 ++-- toREST/tests/include/TorrentContext.hpp | 6 +++ toREST/tests/session_test.cpp | 4 +- toREST/tests/stubs/SessionContext.cpp | 18 +++---- .../stubs/TorrentContext.cpp} | 0 7 files changed, 24 insertions(+), 77 deletions(-) create mode 100644 toREST/tests/include/TorrentContext.hpp rename toREST/{src/session.cpp => tests/stubs/TorrentContext.cpp} (100%) diff --git a/toREST/include/util.hpp b/toREST/include/util.hpp index e5eab73..fb24b8e 100644 --- a/toREST/include/util.hpp +++ b/toREST/include/util.hpp @@ -5,16 +5,6 @@ #include #include -namespace i18N { - using namespace std; - static const auto content_type_not_set= "A Content-Type header set to application/json is required."s; - static const auto unable_to_parse_json= "Unable to parse JSON in body into a JSON-object."s; - static const auto session_unavailable= "A server service is down, please try again later."s; - static const auto wrong_format= "A valid JSON request was posted, but the JSON format was wrong."s; - static const auto unable_to_parse_torrent_uri= "The torrent location was not accepted, the format was wrong."s; - static const auto write_error= "Unable to write to "; -}; - namespace util { class uri { public: diff --git a/toREST/tests/http_test.cpp b/toREST/tests/http_test.cpp index 08f1b6c..f8b1bdc 100644 --- a/toREST/tests/http_test.cpp +++ b/toREST/tests/http_test.cpp @@ -7,7 +7,6 @@ TEST_CASE("Check http status"){ auto out=http::code(http::ok); REQUIRE(out.first == 200); REQUIRE(out.second == "OK"); - // TODO test all } SCENARIO("http response"){ @@ -63,55 +62,3 @@ SCENARIO("Stream http responses with a json response"){ REQUIRE(ss.str()=="HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n{}"s); } } - -/* -TEST(http_response_handler, header){ - ServerTest s; - auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nFoo: bar\r\nFoo: bars\r\nContent-Type: application/json\r\n\r\n"); - std::shared_ptr resp(new ServerTest::Response(nullptr)); - http::request_handler out(req,resp); - ASSERT_TRUE(out.header_set("Content-Type")); - ASSERT_EQ(out.find_last_header_value("Content-Type"),"application/json"); - ASSERT_EQ(out.find_last_header_value("Host"),"localhost:8080"); - ASSERT_EQ(out.find_last_header_value("Foo"),"bars"); - ASSERT_NE(out.find_last_header_value("Foo"), "bar"); - ASSERT_FALSE(out.header_set("Content-Length")); - ASSERT_TRUE(out.header_equals("Content-Type","application/json")); -} - -TEST(http_response_handler, json_response){ - ServerTest s; - auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nContent-Type: application/json\r\n\r\n"); - std::shared_ptr resp(new ServerTest::Response(nullptr)); - http::request_handler out(req,resp); - out.respond(nlohmann::json("{}")); - std::stringstream ss; - ss << resp->rdbuf(); - std::string string_response(ss.str()); - ASSERT_EQ(string_response,"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nContent-Type: application/json\r\n\r\n\"{}\""); -} - -TEST(http_response_handler, string_response){ - ServerTest s; - auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nContent-Type: application/json\r\n\r\n"); - std::shared_ptr resp(new ServerTest::Response(nullptr)); - http::request_handler out(req,resp); - out.respond(std::string("{}")); - std::stringstream ss; - ss << resp->rdbuf(); - std::string string_response(ss.str()); - ASSERT_EQ(string_response,"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n{}"); -} - -TEST(http_response_handler, empty_string){ - ServerTest s; - auto req=s.create_request("GET /v1/session/torrents HTTP/1.1\r\nHost: localhost:8080\r\nContent-Type: application/json\r\n\r\n"); - std::shared_ptr resp(new ServerTest::Response(nullptr)); - http::request_handler out(req,resp); - out.respond(std::string("")); - std::stringstream ss; - ss << resp->rdbuf(); - std::string string_response(ss.str()); - ASSERT_EQ(string_response,"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); -} -*/ \ No newline at end of file diff --git a/toREST/tests/include/SessionContext.hpp b/toREST/tests/include/SessionContext.hpp index c0cc06d..923e83e 100644 --- a/toREST/tests/include/SessionContext.hpp +++ b/toREST/tests/include/SessionContext.hpp @@ -1,7 +1,9 @@ +#ifndef _TR_TEST_SESSION_CONTEXT_HPP_ +#define _TR_TEST_SESSION_CONTEXT_HPP_ + #include #include - -class TestTorrent {}; +#include class TestRequest { public: @@ -35,7 +37,7 @@ public: }; -class TestTorrentSession { +class TestSession { public: bool valid = false; bool paused = false; @@ -50,3 +52,5 @@ public: std::vector get_torrents() const; void apply_settings(TestSessionSettings settings); }; + +#endif diff --git a/toREST/tests/include/TorrentContext.hpp b/toREST/tests/include/TorrentContext.hpp new file mode 100644 index 0000000..b8b3a7f --- /dev/null +++ b/toREST/tests/include/TorrentContext.hpp @@ -0,0 +1,6 @@ +#ifndef _TR_TEST_TORRENT_CONTEXT_HPP_ +#define _TR_TEST_TORRENT_CONTEXT_HPP_ + +class TestTorrent {}; + +#endif \ No newline at end of file diff --git a/toREST/tests/session_test.cpp b/toREST/tests/session_test.cpp index 44a3209..36b4390 100644 --- a/toREST/tests/session_test.cpp +++ b/toREST/tests/session_test.cpp @@ -20,7 +20,7 @@ const auto reply_is_200_ok = [](std::shared_ptr response) { }; SCENARIO("We are running a GET /session resource") { - auto torrent_session = TestTorrentSession(); + auto torrent_session = TestSession(); auto response = std::make_shared(); auto request = std::make_shared(); GIVEN("the server is not working properly") { @@ -45,7 +45,7 @@ SCENARIO("We are running a GET /session resource") { } SCENARIO("We recive a PATCH request on the /session resource") { - auto torrent_session = TestTorrentSession(); + auto torrent_session = TestSession(); auto response = std::make_shared(); auto request = std::make_shared(); GIVEN("the server is not working properly") { diff --git a/toREST/tests/stubs/SessionContext.cpp b/toREST/tests/stubs/SessionContext.cpp index 4d84824..421e2c1 100644 --- a/toREST/tests/stubs/SessionContext.cpp +++ b/toREST/tests/stubs/SessionContext.cpp @@ -54,30 +54,30 @@ void TestSessionSettings::set_str(int type, std::string value) { listen_interfaces_ = value; } } -bool TestTorrentSession::is_valid() { +bool TestSession::is_valid() { return valid; } -TestSessionSettings TestTorrentSession::get_settings() { +TestSessionSettings TestSession::get_settings() { return settings_; } -bool TestTorrentSession::is_paused() const { +bool TestSession::is_paused() const { return paused; } -void TestTorrentSession::pause() { +void TestSession::pause() { paused = true; } -void TestTorrentSession::resume() { +void TestSession::resume() { paused = false; } -int TestTorrentSession::listen_port() { +int TestSession::listen_port() { return std::stoi(settings_.listen_interfaces_.substr(8, settings_.listen_interfaces_.size() - 8)); } -bool TestTorrentSession::is_dht_running() const { +bool TestSession::is_dht_running() const { return settings_.enable_dht_; } -std::vector TestTorrentSession::get_torrents() const { +std::vector TestSession::get_torrents() const { return std::vector(); } -void TestTorrentSession::apply_settings(TestSessionSettings settings) { +void TestSession::apply_settings(TestSessionSettings settings) { settings_ = settings; }; diff --git a/toREST/src/session.cpp b/toREST/tests/stubs/TorrentContext.cpp similarity index 100% rename from toREST/src/session.cpp rename to toREST/tests/stubs/TorrentContext.cpp