IXWebSocket/test/IXWebSocketServerTest.cpp

212 lines
7.0 KiB
C++
Raw Normal View History

/*
2019-01-03 06:59:06 +01:00
* IXWebSocketServerTest.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone. All rights reserved.
*/
#include <iostream>
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXWebSocketServer.h>
2019-03-15 02:37:38 +01:00
#include <ixwebsocket/IXSocketFactory.h>
#include "IXTest.h"
#include "catch.hpp"
using namespace ix;
namespace ix
{
// Test that we can override the connectionState impl to provide our own
class ConnectionStateCustom : public ConnectionState
{
void computeId()
{
// a very boring invariant id that we can test against in the unittest
_id = "foobarConnectionId";
}
};
bool startServer(ix::WebSocketServer& server,
std::string& connectionId)
{
auto factory = []() -> std::shared_ptr<ConnectionState>
{
return std::make_shared<ConnectionStateCustom>();
};
server.setConnectionStateFactory(factory);
server.setOnConnectionCallback(
[&server, &connectionId](std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState)
{
webSocket->setOnMessageCallback(
[webSocket, connectionState,
&connectionId, &server](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketOpenInfo& openInfo,
const ix::WebSocketCloseInfo& closeInfo)
{
if (messageType == ix::WebSocketMessageType::Open)
{
2019-01-08 03:04:28 +01:00
Logger() << "New connection";
2019-03-22 17:56:28 +01:00
connectionState->computeId();
Logger() << "id: " << connectionState->getId();
2019-01-08 03:04:28 +01:00
Logger() << "Uri: " << openInfo.uri;
Logger() << "Headers:";
for (auto it : openInfo.headers)
{
2019-01-08 03:04:28 +01:00
Logger() << it.first << ": " << it.second;
}
connectionId = connectionState->getId();
}
else if (messageType == ix::WebSocketMessageType::Close)
{
2019-01-08 03:04:28 +01:00
Logger() << "Closed connection";
}
else if (messageType == ix::WebSocketMessageType::Message)
{
for (auto&& client : server.getClients())
{
if (client != webSocket)
{
client->send(str);
}
}
}
}
);
}
);
auto res = server.listen();
if (!res.first)
{
2019-01-08 03:04:28 +01:00
Logger() << res.second;
return false;
}
server.start();
return true;
}
}
TEST_CASE("Websocket_server", "[websocket_server]")
{
SECTION("Connect to the server, do not send anything. Should timeout and return 400")
{
int port = getFreePort();
ix::WebSocketServer server(port);
std::string connectionId;
REQUIRE(startServer(server, connectionId));
std::string errMsg;
2019-03-15 02:37:38 +01:00
bool tls = false;
std::shared_ptr<Socket> socket = createSocket(tls, errMsg);
std::string host("127.0.0.1");
auto isCancellationRequested = []() -> bool
{
return false;
};
2019-03-15 02:37:38 +01:00
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
REQUIRE(success);
2019-03-15 02:37:38 +01:00
auto lineResult = socket->readLine(isCancellationRequested);
auto lineValid = lineResult.first;
auto line = lineResult.second;
int status = -1;
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
REQUIRE(status == 400);
// FIXME: explicitely set a client timeout larger than the server one (3)
// Give us 500ms for the server to notice that clients went away
ix::msleep(500);
server.stop();
REQUIRE(server.getClients().size() == 0);
}
SECTION("Connect to the server. Send GET request without header. Should return 400")
{
int port = getFreePort();
ix::WebSocketServer server(port);
std::string connectionId;
REQUIRE(startServer(server, connectionId));
std::string errMsg;
2019-03-15 02:37:38 +01:00
bool tls = false;
std::shared_ptr<Socket> socket = createSocket(tls, errMsg);
std::string host("127.0.0.1");
auto isCancellationRequested = []() -> bool
{
return false;
};
2019-03-15 02:37:38 +01:00
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
REQUIRE(success);
2019-01-08 03:04:28 +01:00
Logger() << "writeBytes";
2019-03-15 02:37:38 +01:00
socket->writeBytes("GET /\r\n", isCancellationRequested);
2019-03-15 02:37:38 +01:00
auto lineResult = socket->readLine(isCancellationRequested);
auto lineValid = lineResult.first;
auto line = lineResult.second;
int status = -1;
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
REQUIRE(status == 400);
// FIXME: explicitely set a client timeout larger than the server one (3)
// Give us 500ms for the server to notice that clients went away
ix::msleep(500);
server.stop();
REQUIRE(server.getClients().size() == 0);
}
SECTION("Connect to the server. Send GET request with correct header")
{
int port = getFreePort();
ix::WebSocketServer server(port);
std::string connectionId;
REQUIRE(startServer(server, connectionId));
std::string errMsg;
2019-03-15 02:37:38 +01:00
bool tls = false;
std::shared_ptr<Socket> socket = createSocket(tls, errMsg);
std::string host("127.0.0.1");
auto isCancellationRequested = []() -> bool
{
return false;
};
2019-03-15 02:37:38 +01:00
bool success = socket->connect(host, port, errMsg, isCancellationRequested);
REQUIRE(success);
2019-03-15 02:37:38 +01:00
socket->writeBytes("GET / HTTP/1.1\r\n"
"Upgrade: websocket\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: foobar\r\n"
"\r\n",
isCancellationRequested);
2019-03-15 02:37:38 +01:00
auto lineResult = socket->readLine(isCancellationRequested);
auto lineValid = lineResult.first;
auto line = lineResult.second;
int status = -1;
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
REQUIRE(status == 101);
// Give us 500ms for the server to notice that clients went away
ix::msleep(500);
server.stop();
REQUIRE(connectionId == "foobarConnectionId");
REQUIRE(server.getClients().size() == 0);
}
}