Browse Source

More clean up of old imp

master
Jørgen Lien Sellæg 9 years ago
parent
commit
78bda9e3e9
  1. 10
      toREST/include/util.hpp
  2. 53
      toREST/tests/http_test.cpp
  3. 10
      toREST/tests/include/SessionContext.hpp
  4. 6
      toREST/tests/include/TorrentContext.hpp
  5. 4
      toREST/tests/session_test.cpp
  6. 18
      toREST/tests/stubs/SessionContext.cpp
  7. 0
      toREST/tests/stubs/TorrentContext.cpp

10
toREST/include/util.hpp

@ -5,16 +5,6 @@
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <json.hpp> #include <json.hpp>
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 { namespace util {
class uri { class uri {
public: public:

53
toREST/tests/http_test.cpp

@ -7,7 +7,6 @@ TEST_CASE("Check http status"){
auto out=http::code(http::ok); auto out=http::code(http::ok);
REQUIRE(out.first == 200); REQUIRE(out.first == 200);
REQUIRE(out.second == "OK"); REQUIRE(out.second == "OK");
// TODO test all
} }
SCENARIO("http response"){ 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); 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<ServerTest::Response> 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<ServerTest::Response> 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<ServerTest::Response> 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<ServerTest::Response> 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");
}
*/

10
toREST/tests/include/SessionContext.hpp

@ -1,7 +1,9 @@
#ifndef _TR_TEST_SESSION_CONTEXT_HPP_
#define _TR_TEST_SESSION_CONTEXT_HPP_
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <TorrentContext.hpp>
class TestTorrent {};
class TestRequest { class TestRequest {
public: public:
@ -35,7 +37,7 @@ public:
}; };
class TestTorrentSession { class TestSession {
public: public:
bool valid = false; bool valid = false;
bool paused = false; bool paused = false;
@ -50,3 +52,5 @@ public:
std::vector<TestTorrent> get_torrents() const; std::vector<TestTorrent> get_torrents() const;
void apply_settings(TestSessionSettings settings); void apply_settings(TestSessionSettings settings);
}; };
#endif

6
toREST/tests/include/TorrentContext.hpp

@ -0,0 +1,6 @@
#ifndef _TR_TEST_TORRENT_CONTEXT_HPP_
#define _TR_TEST_TORRENT_CONTEXT_HPP_
class TestTorrent {};
#endif

4
toREST/tests/session_test.cpp

@ -20,7 +20,7 @@ const auto reply_is_200_ok = [](std::shared_ptr<TestResponse> response) {
}; };
SCENARIO("We are running a GET /session resource") { SCENARIO("We are running a GET /session resource") {
auto torrent_session = TestTorrentSession(); auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>(); auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>(); auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") { 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") { SCENARIO("We recive a PATCH request on the /session resource") {
auto torrent_session = TestTorrentSession(); auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>(); auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>(); auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") { GIVEN("the server is not working properly") {

18
toREST/tests/stubs/SessionContext.cpp

@ -54,30 +54,30 @@ void TestSessionSettings::set_str(int type, std::string value) {
listen_interfaces_ = value; listen_interfaces_ = value;
} }
} }
bool TestTorrentSession::is_valid() { bool TestSession::is_valid() {
return valid; return valid;
} }
TestSessionSettings TestTorrentSession::get_settings() { TestSessionSettings TestSession::get_settings() {
return settings_; return settings_;
} }
bool TestTorrentSession::is_paused() const { bool TestSession::is_paused() const {
return paused; return paused;
} }
void TestTorrentSession::pause() { void TestSession::pause() {
paused = true; paused = true;
} }
void TestTorrentSession::resume() { void TestSession::resume() {
paused = false; paused = false;
} }
int TestTorrentSession::listen_port() { int TestSession::listen_port() {
return std::stoi(settings_.listen_interfaces_.substr(8, settings_.listen_interfaces_.size() - 8)); 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_; return settings_.enable_dht_;
} }
std::vector<TestTorrent> TestTorrentSession::get_torrents() const { std::vector<TestTorrent> TestSession::get_torrents() const {
return std::vector<TestTorrent>(); return std::vector<TestTorrent>();
} }
void TestTorrentSession::apply_settings(TestSessionSettings settings) { void TestSession::apply_settings(TestSessionSettings settings) {
settings_ = settings; settings_ = settings;
}; };

0
toREST/src/session.cpp → toREST/tests/stubs/TorrentContext.cpp

Loading…
Cancel
Save