refactor connect unittest so that it hits a local server instead of a remote server
This commit is contained in:
parent
e5edbeacb4
commit
47c9786bab
@ -7,14 +7,14 @@ matrix:
|
|||||||
compiler: clang
|
compiler: clang
|
||||||
script:
|
script:
|
||||||
- python test/run.py
|
- python test/run.py
|
||||||
- make ws_test
|
- make ws
|
||||||
|
|
||||||
# Linux
|
# Linux
|
||||||
- os: linux
|
- os: linux
|
||||||
dist: xenial
|
dist: xenial
|
||||||
script:
|
script:
|
||||||
- python test/run.py
|
- python test/run.py
|
||||||
- make ws_test
|
- make ws
|
||||||
env:
|
env:
|
||||||
- CC=gcc
|
- CC=gcc
|
||||||
- CXX=g++
|
- CXX=g++
|
||||||
|
@ -17,8 +17,12 @@ TEST_CASE("socket_connect", "[net]")
|
|||||||
{
|
{
|
||||||
SECTION("Test connecting to a known hostname")
|
SECTION("Test connecting to a known hostname")
|
||||||
{
|
{
|
||||||
|
int port = getFreePort();
|
||||||
|
ix::WebSocketServer server(port);
|
||||||
|
REQUIRE(startWebSocketEchoServer(server));
|
||||||
|
|
||||||
std::string errMsg;
|
std::string errMsg;
|
||||||
int fd = SocketConnect::connect("www.google.com", 80, errMsg, [] { return false; });
|
int fd = SocketConnect::connect("127.0.0.1", port, errMsg, [] { return false; });
|
||||||
std::cerr << "Error message: " << errMsg << std::endl;
|
std::cerr << "Error message: " << errMsg << std::endl;
|
||||||
REQUIRE(fd != -1);
|
REQUIRE(fd != -1);
|
||||||
}
|
}
|
||||||
@ -34,9 +38,13 @@ TEST_CASE("socket_connect", "[net]")
|
|||||||
|
|
||||||
SECTION("Test connecting to a good hostname, with cancellation")
|
SECTION("Test connecting to a good hostname, with cancellation")
|
||||||
{
|
{
|
||||||
|
int port = getFreePort();
|
||||||
|
ix::WebSocketServer server(port);
|
||||||
|
REQUIRE(startWebSocketEchoServer(server));
|
||||||
|
|
||||||
std::string errMsg;
|
std::string errMsg;
|
||||||
// The callback returning true means we are requesting cancellation
|
// The callback returning true means we are requesting cancellation
|
||||||
int fd = SocketConnect::connect("www.google.com", 80, errMsg, [] { return true; });
|
int fd = SocketConnect::connect("127.0.0.1", port, errMsg, [] { return true; });
|
||||||
std::cerr << "Error message: " << errMsg << std::endl;
|
std::cerr << "Error message: " << errMsg << std::endl;
|
||||||
REQUIRE(fd == -1);
|
REQUIRE(fd == -1);
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
#include <ixwebsocket/IXSocketFactory.h>
|
#include <ixwebsocket/IXSocketFactory.h>
|
||||||
#include <ixwebsocket/IXSocket.h>
|
#include <ixwebsocket/IXSocket.h>
|
||||||
#include <ixwebsocket/IXCancellationRequest.h>
|
#include <ixwebsocket/IXCancellationRequest.h>
|
||||||
#include <ixwebsocket/IXWebSocketServer.h>
|
|
||||||
|
|
||||||
#include "IXTest.h"
|
#include "IXTest.h"
|
||||||
#include "catch.hpp"
|
#include "catch.hpp"
|
||||||
@ -50,60 +49,6 @@ namespace ix
|
|||||||
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
|
REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1);
|
||||||
REQUIRE(status == expectedStatus);
|
REQUIRE(status == expectedStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool startServer(ix::WebSocketServer& server)
|
|
||||||
{
|
|
||||||
server.setOnConnectionCallback(
|
|
||||||
[&server](std::shared_ptr<ix::WebSocket> webSocket,
|
|
||||||
std::shared_ptr<ConnectionState> connectionState)
|
|
||||||
{
|
|
||||||
webSocket->setOnMessageCallback(
|
|
||||||
[webSocket, connectionState, &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)
|
|
||||||
{
|
|
||||||
Logger() << "New connection";
|
|
||||||
Logger() << "Uri: " << openInfo.uri;
|
|
||||||
Logger() << "Headers:";
|
|
||||||
for (auto it : openInfo.headers)
|
|
||||||
{
|
|
||||||
Logger() << it.first << ": " << it.second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (messageType == ix::WebSocketMessageType::Close)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
Logger() << res.second;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.start();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("socket", "[socket]")
|
TEST_CASE("socket", "[socket]")
|
||||||
@ -113,7 +58,7 @@ TEST_CASE("socket", "[socket]")
|
|||||||
// Start a server first which we'll hit with our socket code
|
// Start a server first which we'll hit with our socket code
|
||||||
int port = getFreePort();
|
int port = getFreePort();
|
||||||
ix::WebSocketServer server(port);
|
ix::WebSocketServer server(port);
|
||||||
REQUIRE(startServer(server));
|
REQUIRE(startWebSocketEchoServer(server));
|
||||||
|
|
||||||
std::string errMsg;
|
std::string errMsg;
|
||||||
bool tls = false;
|
bool tls = false;
|
||||||
|
@ -170,4 +170,58 @@ namespace ix
|
|||||||
|
|
||||||
std::cout << prefix << ": " << s << " => " << ss.str() << std::endl;
|
std::cout << prefix << ": " << s << " => " << ss.str() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool startWebSocketEchoServer(ix::WebSocketServer& server)
|
||||||
|
{
|
||||||
|
server.setOnConnectionCallback(
|
||||||
|
[&server](std::shared_ptr<ix::WebSocket> webSocket,
|
||||||
|
std::shared_ptr<ConnectionState> connectionState)
|
||||||
|
{
|
||||||
|
webSocket->setOnMessageCallback(
|
||||||
|
[webSocket, connectionState, &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)
|
||||||
|
{
|
||||||
|
Logger() << "New connection";
|
||||||
|
Logger() << "Uri: " << openInfo.uri;
|
||||||
|
Logger() << "Headers:";
|
||||||
|
for (auto it : openInfo.headers)
|
||||||
|
{
|
||||||
|
Logger() << it.first << ": " << it.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (messageType == ix::WebSocketMessageType::Close)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
Logger() << res.second;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <ixwebsocket/IXWebSocketServer.h>
|
||||||
|
|
||||||
namespace ix
|
namespace ix
|
||||||
{
|
{
|
||||||
@ -46,4 +47,6 @@ namespace ix
|
|||||||
void log(const std::string& msg);
|
void log(const std::string& msg);
|
||||||
|
|
||||||
int getFreePort();
|
int getFreePort();
|
||||||
|
|
||||||
|
bool startWebSocketEchoServer(ix::WebSocketServer& server);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user