Browse Source

cleanup of style

master
Jørgen Lien Sellæg 9 years ago
parent
commit
5f75dd251d
  1. 46
      toREST/tests/http_test.cpp
  2. 4
      toREST/tests/include/ServerContext.hpp
  3. 5
      toREST/tests/include/SessionContext.hpp
  4. 2
      toREST/tests/stubs/SessionContext.cpp
  5. 16
      toREST/tests/torrents_test.cpp

46
toREST/tests/http_test.cpp

@ -3,62 +3,62 @@
using namespace std; using namespace std;
TEST_CASE("Check http status"){ 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");
} }
SCENARIO("http response"){ SCENARIO("http response") {
auto response=http::response(); auto response = http::response();
std::stringstream ss; std::stringstream ss;
GIVEN("we stream our response without setting given any data"){ GIVEN("we stream our response without setting given any data") {
ss << response; ss << response;
THEN("the response will default to 200") THEN("the response will default to 200")
REQUIRE(ss.str()=="HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\n200 OK"s); REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\n200 OK"s);
} }
GIVEN("we stream our data with the response status set to 404"){ GIVEN("we stream our data with the response status set to 404") {
response.set_status(http::not_found); response.set_status(http::not_found);
ss << response; ss << response;
THEN("the default 404 response will be sent instead"){ THEN("the default 404 response will be sent instead") {
REQUIRE(ss.str()=="HTTP/1.1 404 Not Found\r\nContent-Length: 13\r\n\r\n404 Not Found"s); REQUIRE(ss.str() == "HTTP/1.1 404 Not Found\r\nContent-Length: 13\r\n\r\n404 Not Found"s);
} }
} }
GIVEN("we stream our response with the status set to 204 No Content"){ GIVEN("we stream our response with the status set to 204 No Content") {
response.set_status(http::no_content); response.set_status(http::no_content);
ss << response; ss << response;
THEN("the response has no content header set") THEN("the response has no content header set")
REQUIRE(ss.str()=="HTTP/1.1 204 No Content\r\n\r\n"s); REQUIRE(ss.str() == "HTTP/1.1 204 No Content\r\n\r\n"s);
} }
GIVEN("we stream our response with the body set"){ GIVEN("we stream our response with the body set") {
response.set_body("Easter eggs, everywhere! <a href=\"http://stream1.gifsoup.com/view4/4086928/x-x-everywhere-o.gif\">ye</a>"s); response.set_body("Easter eggs, everywhere! <a href=\"http://stream1.gifsoup.com/view4/4086928/x-x-everywhere-o.gif\">ye</a>"s);
ss << response; ss << response;
THEN("our response contains the appropriate headers") THEN("our response contains the appropriate headers")
REQUIRE(ss.str()=="HTTP/1.1 200 OK\r\nContent-Length: 103\r\n\r\nEaster eggs, everywhere! <a href=\"http://stream1.gifsoup.com/view4/4086928/x-x-everywhere-o.gif\">ye</a>"s); REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Length: 103\r\n\r\nEaster eggs, everywhere! <a href=\"http://stream1.gifsoup.com/view4/4086928/x-x-everywhere-o.gif\">ye</a>"s);
} }
GIVEN("we add a header to the resource"){ GIVEN("we add a header to the resource") {
response.set_body("Nothing"s); response.set_body("Nothing"s);
response.add_header(http::header("Strict-Error"s,"on"s)); response.add_header(http::header("Strict-Error"s, "on"s));
ss << response; ss << response;
THEN("our response contains the appropriate headers") THEN("our response contains the appropriate headers")
REQUIRE(ss.str()=="HTTP/1.1 200 OK\r\nStrict-Error: on\r\nContent-Length: 7\r\n\r\nNothing"s); REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nStrict-Error: on\r\nContent-Length: 7\r\n\r\nNothing"s);
} }
} }
SCENARIO("Stream http responses with a json response"){ SCENARIO("Stream http responses with a json response") {
auto response=http::response(); auto response = http::response();
std::stringstream ss; std::stringstream ss;
GIVEN("we set the response code to 404 and stream our response"){ GIVEN("we set the response code to 404 and stream our response") {
response.set_status(http::not_found); response.set_status(http::not_found);
response.set_body({{"code",404},{"status","Not Found"}}); response.set_body({{"code", 404}, {"status", "Not Found"}});
ss << response; ss << response;
THEN("our response is valid json and the resoinse code is 404") THEN("our response is valid json and the resoinse code is 404")
REQUIRE(ss.str()=="HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\nContent-Length: 33\r\n\r\n{\"code\":404,\"status\":\"Not Found\"}"s); REQUIRE(ss.str() == "HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\nContent-Length: 33\r\n\r\n{\"code\":404,\"status\":\"Not Found\"}"s);
} }
GIVEN("we stream our response with the body set"){ GIVEN("we stream our response with the body set") {
response.set_body(nlohmann::json::object()); response.set_body(nlohmann::json::object());
ss << response; ss << response;
THEN("our response contains the appropriate headers") THEN("our response contains the appropriate headers")
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);
} }
} }

4
toREST/tests/include/ServerContext.hpp

@ -1,9 +1,9 @@
#ifndef _TR_TEST_SERVER_CONTEXT_HPP_ #ifndef _TR_TEST_SERVER_CONTEXT_HPP_
#define _TR_TEST_SERVER_CONTEXT_HPP_ #define _TR_TEST_SERVER_CONTEXT_HPP_
#include <sstream>
#include <memory>
#include <Catch/fakeit.hpp> #include <Catch/fakeit.hpp>
#include <json.hpp> #include <json.hpp>
#include <memory>
#include <sstream>
class TestRequest { class TestRequest {
public: public:

5
toREST/tests/include/SessionContext.hpp

@ -1,9 +1,9 @@
#ifndef _TR_TEST_SESSION_CONTEXT_HPP_ #ifndef _TR_TEST_SESSION_CONTEXT_HPP_
#define _TR_TEST_SESSION_CONTEXT_HPP_ #define _TR_TEST_SESSION_CONTEXT_HPP_
#include <TorrentContext.hpp>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <TorrentContext.hpp>
class TestSessionSettings { class TestSessionSettings {
public: public:
@ -21,7 +21,6 @@ public:
int get_int(int type) const; int get_int(int type) const;
bool get_bool(int type); bool get_bool(int type);
std::string get_str(int type); std::string get_str(int type);
}; };
class TestSession { class TestSession {
@ -37,7 +36,7 @@ public:
void resume(); void resume();
int listen_port(); int listen_port();
bool is_dht_running() const; bool is_dht_running() const;
std::vector<TestTorrent>& get_torrents(); std::vector<TestTorrent> &get_torrents();
void apply_settings(TestSessionSettings settings); void apply_settings(TestSessionSettings settings);
}; };

2
toREST/tests/stubs/SessionContext.cpp

@ -60,7 +60,7 @@ int TestSession::listen_port() {
bool TestSession::is_dht_running() const { bool TestSession::is_dht_running() const {
return settings_.enable_dht_; return settings_.enable_dht_;
} }
std::vector<TestTorrent>& TestSession::get_torrents() { std::vector<TestTorrent> &TestSession::get_torrents() {
return torrents; return torrents;
} }
void TestSession::apply_settings(TestSessionSettings settings) { void TestSession::apply_settings(TestSessionSettings settings) {

16
toREST/tests/torrents_test.cpp

@ -11,8 +11,7 @@ const nlohmann::json torrent = {
{"seeding", false}, {"seeding", false},
{"state", 0}, {"state", 0},
{"priority", 0}, {"priority", 0},
{"name", "Arch"} {"name", "Arch"}};
};
SCENARIO("We are running a GET /session/torrents resource") { SCENARIO("We are running a GET /session/torrents resource") {
auto torrent_session = TestSession(); auto torrent_session = TestSession();
@ -31,19 +30,18 @@ SCENARIO("We are running a GET /session/torrents resource") {
WHEN("we recive a valid request") { WHEN("we recive a valid request") {
GIVEN("we have no torrents") { GIVEN("we have no torrents") {
THEN("the server should reply with an empty array") { THEN("the server should reply with an empty array") {
tr::session::torrents::get(torrent_session,response,request); tr::session::torrents::get(torrent_session, response, request);
CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}}); CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}});
} }
} }
GIVEN("we have at least one torrent"){ GIVEN("we have at least one torrent") {
auto t_torrent=TestTorrent(); auto t_torrent = TestTorrent();
torrent_session.get_torrents().emplace_back(t_torrent); torrent_session.get_torrents().emplace_back(t_torrent);
THEN("the server should reply with an array of torrents"){ THEN("the server should reply with an array of torrents") {
tr::session::torrents::get(torrent_session,response,request); tr::session::torrents::get(torrent_session, response, request);
CommonResponse::ok(response, {{"torrents", {torrent}}}); CommonResponse::ok(response, {{"torrents", {torrent}}});
};
} }
} }
} }
}
} }
Loading…
Cancel
Save