From fc0776303afdc72d2e7f968039236a229be832b2 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Thu, 16 May 2019 12:24:58 -0700 Subject: [PATCH] first socket test hit a local server instead of a remote server / this can help with a windows intermittent failure --- test/IXSocketTest.cpp | 69 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/test/IXSocketTest.cpp b/test/IXSocketTest.cpp index 6306c7ed..279ad1ad 100644 --- a/test/IXSocketTest.cpp +++ b/test/IXSocketTest.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "IXTest.h" #include "catch.hpp" @@ -49,17 +50,75 @@ 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]") { - SECTION("Connect to google HTTP server. Send GET request without header. Should return 200") + SECTION("Connect to a local websocket server over a free port. Send GET request without header. Should return 400") { + // Start a server first which we'll hit with our socket code + int port = getFreePort(); + ix::WebSocketServer server(port); + REQUIRE(startServer(server)); + std::string errMsg; bool tls = false; std::shared_ptr socket = createSocket(tls, errMsg); - std::string host("www.google.com"); - int port = 80; + std::string host("127.0.0.1"); std::stringstream ss; ss << "GET / HTTP/1.1\r\n"; @@ -67,14 +126,14 @@ TEST_CASE("socket", "[socket]") ss << "\r\n"; std::string request(ss.str()); - int expectedStatus = 200; + int expectedStatus = 400; int timeoutSecs = 3; testSocket(host, port, request, socket, expectedStatus, timeoutSecs); } #if defined(__APPLE__) || defined(__linux__) - SECTION("Connect to google HTTPS server. Send GET request without header. Should return 200") + SECTION("Connect to google HTTPS server over port 443. Send GET request without header. Should return 200") { std::string errMsg; bool tls = true;