Browse Source

Test open connection

master
Jørgen Lien Sellæg 9 years ago
parent
commit
534facda79
  1. 3
      toREST/include/config.hpp
  2. 26
      toREST/include/ws.hpp
  3. 31
      toREST/src/main.cxx
  4. 3
      toREST/tests/config_test.cpp
  5. 2
      toREST/tests/include/SessionContext.hpp
  6. 11
      toREST/tests/stubs/SessionContext.cpp
  7. 35
      toREST/tests/ws_test.cpp

3
toREST/include/config.hpp

@ -6,6 +6,9 @@
struct Config { struct Config {
const boost::filesystem::path root_dir = "/"; const boost::filesystem::path root_dir = "/";
const boost::filesystem::path default_download_dir = "toREST"; const boost::filesystem::path default_download_dir = "toREST";
const int http_port = 8080;
const int ws_port = 3000;
const int http_threads = 1;
}; };
#endif #endif

26
toREST/include/ws.hpp

@ -2,20 +2,30 @@
#define _TR_TORRENT_HPP_ #define _TR_TORRENT_HPP_
#include <boost/system/error_code.hpp> #include <boost/system/error_code.hpp>
#include <iostream>
namespace tr { namespace tr {
namespace ws { namespace ws {
template <class Connection, class Server> enum status {
void on_error(Server server, Connection con, const boost::system::error_code) { normal = 1000,
};
template <class Session, class Connection>
void on_error(Session session, Connection con, const boost::system::error_code &ec) {
std::cerr << ec.message() << std::endl;
} }
template <class Connection, class Server> template <class Session, class Connection>
void on_close(Server server, Connection con, int status, const std::string &reason) { void on_close(Session session, Connection con, status sta, const std::string &reason) {
} }
template <class Connection, class Server> template <class Server, class Session, class Connection>
void on_open(Server server, Connection con) { 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");
}
} }
template <class Connection, class Server, class Message> template <class Session, class Connection, class Message>
void on_message(Server server, Connection con, Message message) { void on_message(Session session, Connection con, Message message) {
} }
} }
} }

31
toREST/src/main.cxx

@ -15,20 +15,23 @@ typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer; typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int http_port = 8080, http_threads = 4; Config config;
Config options; if (!boost::filesystem::exists(config.root_dir)) {
if (!boost::filesystem::exists(options.root_dir)) {
boost::system::error_code ec; boost::system::error_code ec;
boost::filesystem::create_directories(options.default_download_dir, ec); boost::filesystem::create_directories(config.default_download_dir, ec);
if (ec) { if (ec) {
std::cout << ec.message() << "\n"; std::cout << ec.message() << "\n";
return 1; return 1;
} }
} }
HttpServer http_server(http_port, http_threads); HttpServer http_server(config.http_port, config.http_threads);
WsServer ws_server;
ws_server.config.port = config.ws_port;
ws_server.config.thread_pool_size = config.http_threads;
libtorrent::session session; libtorrent::session session;
http_server.default_resource["GET"] = [](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { http_server.default_resource["GET"] = [](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) {
@ -50,7 +53,7 @@ int main(int argc, char *argv[]) {
}; };
http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["POST"] = [&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["POST"] = [&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) {
tr::session::torrents::post(options, session, resp, req); tr::session::torrents::post(config, session, resp, req);
}; };
http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["DELETE"] = [&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) { http_server.resource["^/session/torrents(\\?.*)?\\/?$"]["DELETE"] = [&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) {
@ -65,11 +68,17 @@ int main(int argc, char *argv[]) {
tr::session::torrents::id::patch(session, resp, req); tr::session::torrents::id::patch(session, resp, req);
}; };
std::thread server_thread([&http_server]() { std::thread http_thread([&http_server]() {
http_server.start(); http_server.start();
}); });
std::cout << "HttpServer listening on port " << 8080 << std::endl; std::cout << "HTTP/1.1 server listening on port " << config.http_port << std::endl;
std::thread ws_thread([&ws_server]() {
ws_server.start();
});
std::cout << "HTTP/1.1 websocket server listening on port " << config.ws_port << std::endl;
std::string input; std::string input;
while (input != "q") { while (input != "q") {
@ -77,12 +86,14 @@ int main(int argc, char *argv[]) {
if (input == "q") { if (input == "q") {
std::cout << "Stopping server..." << std::endl; std::cout << "Stopping server..." << std::endl;
http_server.stop(); // TODO check if throws http_server.stop(); // TODO check if throws
ws_server.stop();
std::cout << "Server stopped" << std::endl; std::cout << "Server stopped" << std::endl;
break; break;
} }
} }
server_thread.join(); http_thread.join();
ws_thread.join();
return 0; return 0;
}; };

3
toREST/tests/config_test.cpp

@ -6,4 +6,7 @@ SCENARIO("The configuration have default properties") {
REQUIRE_FALSE(config.default_download_dir.empty()); REQUIRE_FALSE(config.default_download_dir.empty());
REQUIRE_FALSE(config.root_dir.empty()); REQUIRE_FALSE(config.root_dir.empty());
REQUIRE(config.default_download_dir == "toREST"); REQUIRE(config.default_download_dir == "toREST");
REQUIRE(config.http_port == 8080);
REQUIRE(config.http_threads == 1);
REQUIRE(config.ws_port == 3000);
} }

2
toREST/tests/include/SessionContext.hpp

@ -45,6 +45,8 @@ public:
void remove_torrent(const TestTorrent &torrent, int options); void remove_torrent(const TestTorrent &torrent, int options);
void apply_settings(TestSessionSettings settings); void apply_settings(TestSessionSettings settings);
void async_add_torrent(const libtorrent::add_torrent_params &params); void async_add_torrent(const libtorrent::add_torrent_params &params);
void post_torrent_updates(int flags = 0);
int post_torrent_updates_calls_ = 0;
void theTorrentExists(); void theTorrentExists();
TestTorrent invalid_; TestTorrent invalid_;
}; };

11
toREST/tests/stubs/SessionContext.cpp

@ -90,13 +90,6 @@ TestTorrent &TestSession::find_torrent(const libtorrent::sha1_hash &hash) {
void TestSession::remove_torrent(const TestTorrent &torrent, int options = -1) { void TestSession::remove_torrent(const TestTorrent &torrent, int options = -1) {
if (options > -1) { if (options > -1) {
// auto itr = torrents_.end();
// for (itr = torrents_.begin(); itr != torrents_.end(); itr++) {
// if (itr->info_hash() == torrent.info_hash()) {
// break;
// }
// }
// if (itr != torrents_.end())
torrents_.erase(std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent a) { torrents_.erase(std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent a) {
return a.info_hash() == torrent.info_hash(); return a.info_hash() == torrent.info_hash();
})); }));
@ -110,3 +103,7 @@ void TestSession::apply_settings(TestSessionSettings settings) {
void TestSession::async_add_torrent(const libtorrent::add_torrent_params &settings) { void TestSession::async_add_torrent(const libtorrent::add_torrent_params &settings) {
torrents_.emplace_back(settings); torrents_.emplace_back(settings);
}; };
void TestSession::post_torrent_updates(int flags) {
post_torrent_updates_calls_++;
}

35
toREST/tests/ws_test.cpp

@ -1,7 +1,42 @@
#include <Catch/catch.hpp> #include <Catch/catch.hpp>
#include <SessionContext.hpp>
#include <ws.hpp> #include <ws.hpp>
SCENARIO("the test suite is working") { SCENARIO("the test suite is working") {
REQUIRE(true); REQUIRE(true);
REQUIRE_FALSE(false); REQUIRE_FALSE(false);
} }
struct TestConnection {
};
struct TestWsServer {
void send_close(TestConnection &con,tr::ws::status status, std::string reason) {
send_close_calls_++;
}
int send_close_calls_ = 0;
};
SCENARIO("we have a websocket resource") {
TestConnection connection;
TestSession session;
TestWsServer server;
GIVEN("the session is valid") {
session.valid = true;
WHEN("we open the connection") {
tr::ws::on_open(server, session, connection);
THEN("the session should post torrent torrent updates") {
REQUIRE(session.post_torrent_updates_calls_ == 1);
}
}
}
GIVEN("the session is invalid") {
WHEN("we open the connection") {
tr::ws::on_open(server, session, connection);
THEN("the server should close the connection") {
REQUIRE(server.send_close_calls_ == 1);
}
}
}
}
Loading…
Cancel
Save