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. 4
      toREST/tests/session_test.cpp
  5. 2
      toREST/tests/stubs/SessionContext.cpp
  6. 28
      toREST/tests/torrents_test.cpp

46
toREST/tests/http_test.cpp

@ -3,62 +3,62 @@
using namespace std;
TEST_CASE("Check http status"){
auto out=http::code(http::ok);
TEST_CASE("Check http status") {
auto out = http::code(http::ok);
REQUIRE(out.first == 200);
REQUIRE(out.second == "OK");
}
SCENARIO("http response"){
auto response=http::response();
SCENARIO("http response") {
auto response = http::response();
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;
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);
ss << response;
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);
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);
}
}
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);
ss << response;
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);
ss << response;
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.add_header(http::header("Strict-Error"s,"on"s));
response.add_header(http::header("Strict-Error"s, "on"s));
ss << response;
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"){
auto response=http::response();
SCENARIO("Stream http responses with a json response") {
auto response = http::response();
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_body({{"code",404},{"status","Not Found"}});
response.set_body({{"code", 404}, {"status", "Not Found"}});
ss << response;
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());
ss << response;
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_
#define _TR_TEST_SERVER_CONTEXT_HPP_
#include <sstream>
#include <memory>
#include <Catch/fakeit.hpp>
#include <json.hpp>
#include <memory>
#include <sstream>
class TestRequest {
public:

5
toREST/tests/include/SessionContext.hpp

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

4
toREST/tests/session_test.cpp

@ -3,8 +3,8 @@
#include <SessionContext.hpp>
#include <session.hpp>
const nlohmann::json ok_data = {{"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 nlohmann::json ok_data = {{"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}};
SCENARIO("We are running a GET /session resource") {
auto torrent_session = TestSession();

2
toREST/tests/stubs/SessionContext.cpp

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

28
toREST/tests/torrents_test.cpp

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