Browse Source

Add more tests and fix accepted bug

master
Jørgen Lien Sellæg 9 years ago
parent
commit
304fe0f672
  1. 1
      toREST/include/session.hpp
  2. 29
      toREST/tests/include/ServerContext.hpp
  3. 13
      toREST/tests/include/SessionContext.hpp
  4. 4
      toREST/tests/include/TorrentContext.hpp
  5. 44
      toREST/tests/session_test.cpp
  6. 43
      toREST/tests/stubs/ServerContext.cpp
  7. 17
      toREST/tests/stubs/SessionContext.cpp
  8. 30
      toREST/tests/torrents_test.hpp

1
toREST/include/session.hpp

@ -57,6 +57,7 @@ namespace tr{
session.apply_settings(settings); session.apply_settings(settings);
auto response_code = http::code(http::accepted); auto response_code = http::code(http::accepted);
http_response.set_body({{"code",response_code.first},{"status", response_code.second}}); http_response.set_body({{"code",response_code.first},{"status", response_code.second}});
http_response.set_status(response_code.first);
} else { } else {
auto response_code = http::code(http::bad_request); auto response_code = http::code(http::bad_request);
http_response.set_body({{"code",response_code.first},{"status", response_code.second}}); http_response.set_body({{"code",response_code.first},{"status", response_code.second}});

29
toREST/tests/include/ServerContext.hpp

@ -0,0 +1,29 @@
#ifndef _TR_TEST_SERVER_CONTEXT_HPP_
#define _TR_TEST_SERVER_CONTEXT_HPP_
#include <sstream>
#include <memory>
#include <Catch/fakeit.hpp>
#include <json.hpp>
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<TestResponse> response);
static void ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data);
static void bad_request(std::shared_ptr<TestResponse> response);
static void accepted(std::shared_ptr<TestResponse> response);
};
#endif

13
toREST/tests/include/SessionContext.hpp

@ -5,19 +5,6 @@
#include <vector> #include <vector>
#include <TorrentContext.hpp> #include <TorrentContext.hpp>
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 { class TestSessionSettings {
public: public:
int download_rate_limit = 1; int download_rate_limit = 1;

4
toREST/tests/include/TorrentContext.hpp

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

44
toREST/tests/session_test.cpp

@ -1,23 +1,10 @@
#include <Catch/fakeit.hpp> #include <Catch/fakeit.hpp>
#include <session.hpp> #include <ServerContext.hpp>
#include <SessionContext.hpp> #include <SessionContext.hpp>
#include <session.hpp>
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 nlohmann::json ok_data = {{"dht_enabled", true}, {"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 nlohmann::json ok_data_paused = {{"dht_enabled", true}, {"down_limit", 0}, {"paused", true}, {"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<TestResponse> 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<TestResponse> response) {
REQUIRE(response->code() == "200");
REQUIRE(response->message() == "OK");
};
SCENARIO("We are running a GET /session resource") { SCENARIO("We are running a GET /session resource") {
auto torrent_session = TestSession(); auto torrent_session = TestSession();
@ -27,18 +14,23 @@ SCENARIO("We are running a GET /session resource") {
AND_WHEN("we recive a request") { AND_WHEN("we recive a request") {
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") {
reply_is_service_unavailable(response); CommonResponse::service_unavailable(response);
} }
} }
} }
GIVEN("the server is working properly") { GIVEN("the server is working properly") {
torrent_session.valid = true; 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; torrent_session.paused = true;
tr::session::get(torrent_session, response, request); tr::session::get(torrent_session, response, request);
THEN("the server should reply with resource data") { THEN("the server should reply with the paused field set to true") {
REQUIRE(response->string() == ok_data_paused); CommonResponse::ok(response, ok_data_paused);
reply_is_200_ok(response); }
}
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") { GIVEN("the server is not working properly") {
tr::session::patch(torrent_session, response, request); tr::session::patch(torrent_session, response, request);
THEN("the server should reply with service unavailable") { THEN("the server should reply with service unavailable") {
reply_is_service_unavailable(response); CommonResponse::service_unavailable(response);
} }
} }
GIVEN("the server is working properly") { 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"; request->content << "Not valid json";
tr::session::patch(torrent_session, response, request); tr::session::patch(torrent_session, response, request);
THEN("the server should respond with bad request") { THEN("the server should respond with bad request") {
REQUIRE(response->string() == bad_request_json); CommonResponse::bad_request(response);
REQUIRE(response->code() == "400");
REQUIRE(response->message() == "Bad Request");
} }
} }
GIVEN("the server recives a valid request") { 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_.download_rate_limit_ == 0);
REQUIRE(torrent_session.settings_.upload_rate_limit_ == 0); REQUIRE(torrent_session.settings_.upload_rate_limit_ == 0);
tr::session::patch(torrent_session, response, request); tr::session::patch(torrent_session, response, request);
REQUIRE(response->string() == accepted); CommonResponse::accepted(response);
REQUIRE(torrent_session.paused); REQUIRE(torrent_session.paused);
REQUIRE_FALSE(torrent_session.settings_.enable_dht_); REQUIRE_FALSE(torrent_session.settings_.enable_dht_);
REQUIRE(torrent_session.listen_port() == 1); REQUIRE(torrent_session.listen_port() == 1);

43
toREST/tests/stubs/ServerContext.cpp

@ -0,0 +1,43 @@
#include <ServerContext.hpp>
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<TestResponse> 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<TestResponse> 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<TestResponse> 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<TestResponse> 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");
};

17
toREST/tests/stubs/SessionContext.cpp

@ -1,22 +1,7 @@
#include <ServerContext.hpp>
#include <SessionContext.hpp> #include <SessionContext.hpp>
#include <sstream>
#include <vector> #include <vector>
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) { void TestSessionSettings::set_int(int type, int value) {
if (type == download_rate_limit) { if (type == download_rate_limit) {
download_rate_limit_ = value; download_rate_limit_ = value;

30
toREST/tests/torrents_test.hpp

@ -0,0 +1,30 @@
#include <Catch/fakeit.hpp>
#include <SessionContext.hpp>
#include <ServerContext.hpp>
using namespace std;
SCENARIO("We are running a GET /session resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
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);
}
}
}
}
Loading…
Cancel
Save