Browse Source

Session responses should be valid json

master
Jørgen Lien Sellæg 9 years ago
parent
commit
26a77989cf
  1. 4
      toREST/include/session.hpp
  2. 98
      toREST/tests/session_test.cpp

4
toREST/include/session.hpp

@ -30,7 +30,7 @@ namespace tr{
auto http_response = http::response(); auto http_response = http::response();
if (!session.is_valid()) { if (!session.is_valid()) {
auto response_code = http::code(http::service_unavailable); auto response_code = http::code(http::service_unavailable);
http_response.set_body({{response_code.first, response_code.second}}); http_response.set_body({{"code",response_code.first},{"status", response_code.second}});
http_response.set_status(response_code.first); http_response.set_status(response_code.first);
} else { } else {
const auto json=util::json::parse(req->content); const auto json=util::json::parse(req->content);
@ -58,7 +58,7 @@ namespace tr{
http_response.set_body({{200,"OK"}}); http_response.set_body({{200,"OK"}});
} else { } else {
auto response_code = http::code(http::bad_request); auto response_code = http::code(http::bad_request);
http_response.set_body({{response_code.first,response_code.second}}); http_response.set_body({{"code",response_code.first},{"status", response_code.second}});
http_response.set_status(response_code.first); http_response.set_status(response_code.first);
} }
} }

98
toREST/tests/session_test.cpp

@ -6,6 +6,7 @@ using namespace std;
class TestTorrent {}; class TestTorrent {};
class TestRequest { class TestRequest {
public:
std::stringstream content; std::stringstream content;
}; };
class TestResponse : public std::stringstream { class TestResponse : public std::stringstream {
@ -30,10 +31,49 @@ public:
class TestSessionSettings { class TestSessionSettings {
public: public:
const int download_rate_limit = 1; int download_rate_limit = 1;
const int upload_rate_limit = 2; int upload_rate_limit = 2;
int listen_interfaces = 3;
int enable_dht = 4;
bool enable_dht_ = false;
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 { int get_int(int type) const {
return 0; 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_;
}
};
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;
}
} }
}; };
@ -51,26 +91,29 @@ public:
bool is_paused() { bool is_paused() {
return paused; return paused;
} }
void pause() { paused = true; }
void resume() { paused = false; }
int listen_port() { int listen_port() {
return 0; return 0;
} }
int is_dht_running() { bool is_dht_running() {
return dht_running; return dht_running;
} }
std::vector<TestTorrent> get_torrents() const { std::vector<TestTorrent> get_torrents() const {
return std::vector<TestTorrent>(); return std::vector<TestTorrent>();
} }
void apply_settings(TestSessionSettings){};
}; };
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 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_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_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}"; const std::string ok_json_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}";
SCENARIO("We recive a GET request to the /session resource") { SCENARIO("We recive a GET request to the /session resource") {
auto torrent_session = TestTorrentSession(); auto torrent_session = TestTorrentSession();
auto response = std::make_shared<TestResponse>(); auto response = std::make_shared<TestResponse>();
GIVEN("the session is invalid") { GIVEN("the session is invalid") {
auto request = std::make_shared<TestResponse>(); auto request = std::make_shared<TestRequest>();
tr::session::get(torrent_session, response, request); tr::session::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") { THEN("the server should reply with service unavailable") {
REQUIRE(response->string() == service_unavailable_json); REQUIRE(response->string() == service_unavailable_json);
@ -79,31 +122,52 @@ SCENARIO("We recive a GET request to the /session resource") {
} }
} }
GIVEN("the session is valid") { GIVEN("the session is valid") {
auto request = std::make_shared<TestResponse>(); auto request = std::make_shared<TestRequest>();
torrent_session.valid = true; torrent_session.valid = true;
tr::session::get(torrent_session, response, request); tr::session::get(torrent_session, response, request);
THEN("the server should reply with status 200 OK") { THEN("the server should reply with status 200 OK") {
REQUIRE(response->string() == ok_json); REQUIRE(response->string() == ok_data);
REQUIRE(response->code() == "200"); REQUIRE(response->code() == "200");
REQUIRE(response->message() == "OK"); REQUIRE(response->message() == "OK");
} }
} }
GIVEN("the session is paused") { GIVEN("the session is paused") {
auto request = std::make_shared<TestResponse>(); auto request = std::make_shared<TestRequest>();
torrent_session.valid = true; torrent_session.valid = true;
torrent_session.paused = true; torrent_session.paused = true;
torrent_session.dht_running = true;
tr::session::get(torrent_session, response, request); tr::session::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") { THEN("the server should reply with service unavailable") {
REQUIRE(response->str() == ok_json); REQUIRE(response->string() == ok_json_paused);
} }
} }
GIVEN("the session is valid") { }
auto request = std::make_shared<TestResponse>();
torrent_session.valid = true; SCENARIO("We recive a PATCH request to the /session resource") {
torrent_session.paused = true; auto torrent_session = TestTorrentSession();
tr::session::get(torrent_session, response, request); auto response = std::make_shared<TestResponse>();
GIVEN("the session is invalid") {
auto request = std::make_shared<TestRequest>();
tr::session::patch(torrent_session, response, request);
THEN("the server should reply with service unavailable") { THEN("the server should reply with service unavailable") {
REQUIRE(response->str() == ok_json_paused); REQUIRE(response->string() == service_unavailable_json);
} }
} }
// GIVEN("the session is valid") {
// auto request = std::make_shared<TestRequest>();
// torrent_session.valid = true;
// tr::session::patch(torrent_session, response, request);
// THEN("the server should reply with session data") {
// REQUIRE(response->string() == ok_data);
// }
// }
// GIVEN("the session is paused") {
// auto request = std::make_shared<TestRequest>();
// 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);
// }
// }
} }

Loading…
Cancel
Save