Browse Source

test on open

master
Jørgen Lien Sellæg 9 years ago
parent
commit
59fb90f8d5
  1. 5
      toREST/include/config.hpp
  2. 17
      toREST/include/ws.hpp
  3. 22
      toREST/src/main.cxx
  4. 2
      toREST/tests/config_test.cpp
  5. 2
      toREST/tests/torrents_test.cpp
  6. 11
      toREST/tests/ws_test.cpp

5
toREST/include/config.hpp

@ -2,13 +2,14 @@
#define _TR_CONFIG_HPP_
#include <boost/filesystem.hpp>
struct Config {
namespace tr {
struct config {
const boost::filesystem::path root_dir = "/";
const boost::filesystem::path default_download_dir = "toREST";
const int http_port = 8080;
const int ws_port = 3000;
const int http_threads = 1;
};
}
#endif

17
toREST/include/ws.hpp

@ -1,31 +1,32 @@
#ifndef _TR_TORRENT_HPP_
#define _TR_TORRENT_HPP_
#ifndef _TR_WS_HPP_
#define _TR_WS_HPP_
#include <boost/system/error_code.hpp>
#include <iostream>
namespace tr {
namespace ws {
enum status {
normal = 1000,
namespace status {
const int normal = 1000;
const int unexpected = 1011;
};
template <class Session, class Connection>
void on_error(Session session, Connection con, const boost::system::error_code &ec) {
void on_error(Session &session, Connection con, const boost::system::error_code &ec) {
std::cerr << ec.message() << std::endl;
}
template <class Session, class Connection>
void on_close(Session session, Connection con, status sta, const std::string &reason) {
void on_close(Session &session, Connection con, int status, const std::string &reason) {
}
template <class Server, class Session, class Connection>
void on_open(Server &server, Session &session, Connection con) {
if (session.is_valid()) {
session.post_torrent_updates();
} else {
server.send_close(con, status::normal, "Session not valid");
server.send_close(con, status::unexpected, "Session not valid");
}
}
template <class Session, class Connection, class Message>
void on_message(Session session, Connection con, Message message) {
void on_message(Session &session, Connection con, Message message) {
}
}
}

22
toREST/src/main.cxx

@ -10,12 +10,13 @@
#include <session.hpp>
#include <torrent.hpp>
#include <torrents.hpp>
#include <ws.hpp>
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer;
int main(int argc, char *argv[]) {
Config config;
tr::config config;
if (!boost::filesystem::exists(config.root_dir)) {
boost::system::error_code ec;
@ -68,6 +69,25 @@ int main(int argc, char *argv[]) {
tr::session::torrents::id::patch(session, resp, req);
};
auto &end_point = ws_server.endpoint["^/session"];
end_point.on_open = [&](std::shared_ptr<WsServer::Connection> connection) {
tr::ws::on_open(ws_server, session, connection);
};
end_point.on_message = [&](std::shared_ptr<WsServer::Connection> connection, std::shared_ptr<WsServer::Message> message) {
tr::ws::on_message(session, connection, message);
};
end_point.on_error = [&](std::shared_ptr<WsServer::Connection> connection, const boost::system::error_code &code) {
tr::ws::on_error(session, connection, code);
};
end_point.on_close = [&](std::shared_ptr<WsServer::Connection> connection, int status, const std::string &reason) {
tr::ws::on_close(session, connection, status, reason);
};
std::thread http_thread([&http_server]() {
http_server.start();
});

2
toREST/tests/config_test.cpp

@ -2,7 +2,7 @@
#include <config.hpp>
SCENARIO("The configuration have default properties") {
Config config;
tr::config config;
REQUIRE_FALSE(config.default_download_dir.empty());
REQUIRE_FALSE(config.root_dir.empty());
REQUIRE(config.default_download_dir == "toREST");

2
toREST/tests/torrents_test.cpp

@ -38,7 +38,7 @@ SCENARIO("We are running a GET /session/torrents resource") {
SCENARIO("We are running a POST /session/torrents resource") {
auto torrent_session = TestSession();
auto settings = Config();
auto settings = tr::config();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {

11
toREST/tests/ws_test.cpp

@ -8,20 +8,23 @@ SCENARIO("the test suite is working") {
}
struct TestConnection {
std::string reason_ = "";
int status_ = 0;
};
struct TestWsServer {
void send_close(TestConnection &con,tr::ws::status status, std::string reason) {
void send_close(std::shared_ptr<TestConnection> con, int status, const std::string &reason) {
send_close_calls_++;
con->reason_ = reason;
con->status_ = status;
}
int send_close_calls_ = 0;
};
SCENARIO("we have a websocket resource") {
TestConnection connection;
TestSession session;
TestWsServer server;
auto connection = std::make_shared<TestConnection>();
GIVEN("the session is valid") {
session.valid = true;
WHEN("we open the connection") {
@ -36,6 +39,8 @@ SCENARIO("we have a websocket resource") {
tr::ws::on_open(server, session, connection);
THEN("the server should close the connection") {
REQUIRE(server.send_close_calls_ == 1);
REQUIRE(connection->status_ == 1011);
REQUIRE(connection->reason_ == "Session not valid");
}
}
}

Loading…
Cancel
Save