You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.7 KiB
88 lines
2.7 KiB
#include <Catch/catch.hpp> |
|
#include <SessionContext.hpp> |
|
#include <ws.hpp> |
|
|
|
SCENARIO("the test suite is working") { |
|
REQUIRE(true); |
|
REQUIRE_FALSE(false); |
|
} |
|
|
|
struct TestConnection { |
|
std::string reason_ = ""; |
|
int status_ = 0; |
|
}; |
|
|
|
struct TestMessage : public std::stringstream { |
|
std::string string() { |
|
return str(); |
|
} |
|
}; |
|
|
|
struct TestWsServer { |
|
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") { |
|
TestSession session; |
|
TestWsServer server; |
|
auto connection = std::make_shared<TestConnection>(); |
|
GIVEN("the session is valid") { |
|
session.valid = true; |
|
WHEN("we open the connection") { |
|
tr::ws::on_open(server, session, connection); |
|
THEN("the session should not post torrent torrent updates") { |
|
REQUIRE(server.send_close_calls_ == 0); |
|
} |
|
} |
|
} |
|
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); |
|
REQUIRE(connection->status_ == 1011); |
|
REQUIRE(connection->reason_ == "Session not valid"); |
|
} |
|
} |
|
} |
|
auto message = std::make_shared<TestMessage>(); |
|
GIVEN("we recive an json request") { |
|
WHEN("the json isn't valid") { |
|
*message << "Not valid"; |
|
THEN("the server should reject the action") { |
|
tr::ws::on_message(session, connection, message); |
|
REQUIRE(server.send_close_calls_ == 0); |
|
REQUIRE(session.post_torrent_updates_calls_ == 0); |
|
} |
|
} |
|
WHEN("the json is valid but doesn't have any of the required fields") { |
|
*message << "{}"; |
|
THEN("the server should reject the action") { |
|
tr::ws::on_message(session, connection, message); |
|
REQUIRE(server.send_close_calls_ == 0); |
|
REQUIRE(session.post_torrent_updates_calls_ == 0); |
|
} |
|
} |
|
WHEN("the json is valid and have the required fields but not of valid type") { |
|
*message << nlohmann::json({{"type", "N/A"}}); |
|
THEN("the server should reject the action") { |
|
tr::ws::on_message(session, connection, message); |
|
REQUIRE(server.send_close_calls_ == 0); |
|
REQUIRE(session.post_torrent_updates_calls_ == 0); |
|
} |
|
} |
|
WHEN("the json is valid and have the required fields") { |
|
*message << nlohmann::json({{"type", "POST_TORRENT_UPDATES"}}); |
|
THEN("the server should post torrent updates") { |
|
tr::ws::on_message(session, connection, message); |
|
REQUIRE(server.send_close_calls_ == 0); |
|
REQUIRE(session.post_torrent_updates_calls_ == 1); |
|
} |
|
} |
|
} |
|
} |