From 47c9786bab2fa8e4f91bd50d29863335eae75ac6 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Thu, 16 May 2019 14:25:31 -0700 Subject: [PATCH] refactor connect unittest so that it hits a local server instead of a remote server --- .travis.yml | 4 +-- test/IXSocketConnectTest.cpp | 12 ++++++-- test/IXSocketTest.cpp | 57 +----------------------------------- test/IXTest.cpp | 54 ++++++++++++++++++++++++++++++++++ test/IXTest.h | 3 ++ 5 files changed, 70 insertions(+), 60 deletions(-) diff --git a/.travis.yml b/.travis.yml index 335a350d..5fadfeec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,14 +7,14 @@ matrix: compiler: clang script: - python test/run.py - - make ws_test + - make ws # Linux - os: linux dist: xenial script: - python test/run.py - - make ws_test + - make ws env: - CC=gcc - CXX=g++ diff --git a/test/IXSocketConnectTest.cpp b/test/IXSocketConnectTest.cpp index 1f98173d..aeeb63bd 100644 --- a/test/IXSocketConnectTest.cpp +++ b/test/IXSocketConnectTest.cpp @@ -17,8 +17,12 @@ TEST_CASE("socket_connect", "[net]") { SECTION("Test connecting to a known hostname") { + int port = getFreePort(); + ix::WebSocketServer server(port); + REQUIRE(startWebSocketEchoServer(server)); + 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; REQUIRE(fd != -1); } @@ -34,9 +38,13 @@ TEST_CASE("socket_connect", "[net]") SECTION("Test connecting to a good hostname, with cancellation") { + int port = getFreePort(); + ix::WebSocketServer server(port); + REQUIRE(startWebSocketEchoServer(server)); + std::string errMsg; // 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; REQUIRE(fd == -1); } diff --git a/test/IXSocketTest.cpp b/test/IXSocketTest.cpp index 279ad1ad..7c689c09 100644 --- a/test/IXSocketTest.cpp +++ b/test/IXSocketTest.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include "IXTest.h" #include "catch.hpp" @@ -50,60 +49,6 @@ namespace ix REQUIRE(sscanf(line.c_str(), "HTTP/1.1 %d", &status) == 1); REQUIRE(status == expectedStatus); } - - bool startServer(ix::WebSocketServer& server) - { - server.setOnConnectionCallback( - [&server](std::shared_ptr webSocket, - std::shared_ptr 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]") @@ -113,7 +58,7 @@ TEST_CASE("socket", "[socket]") // Start a server first which we'll hit with our socket code int port = getFreePort(); ix::WebSocketServer server(port); - REQUIRE(startServer(server)); + REQUIRE(startWebSocketEchoServer(server)); std::string errMsg; bool tls = false; diff --git a/test/IXTest.cpp b/test/IXTest.cpp index 886df20f..d9e2affb 100644 --- a/test/IXTest.cpp +++ b/test/IXTest.cpp @@ -170,4 +170,58 @@ namespace ix std::cout << prefix << ": " << s << " => " << ss.str() << std::endl; } + + bool startWebSocketEchoServer(ix::WebSocketServer& server) + { + server.setOnConnectionCallback( + [&server](std::shared_ptr webSocket, + std::shared_ptr 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; + } } diff --git a/test/IXTest.h b/test/IXTest.h index adf263af..c65b265a 100644 --- a/test/IXTest.h +++ b/test/IXTest.h @@ -12,6 +12,7 @@ #include #include #include +#include namespace ix { @@ -46,4 +47,6 @@ namespace ix void log(const std::string& msg); int getFreePort(); + + bool startWebSocketEchoServer(ix::WebSocketServer& server); }