/** IXWebSocketServerTest.cpp* Author: Benjamin Sergeant* Copyright (c) 2019 Machine Zone. All rights reserved.*/#include "IXTest.h"#include "catch.hpp"#include <iostream>#include <ixwebsocket/IXSocket.h>#include <ixwebsocket/IXSocketFactory.h>#include <ixwebsocket/IXWebSocket.h>#include <ixwebsocket/IXWebSocketServer.h>using namespace ix;namespace ix{// Test that we can override the connectionState impl to provide our ownclass 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.setOnClientMessageCallback([&server, &connectionId](std::shared_ptr<ConnectionState> connectionState,WebSocket& webSocket,const ix::WebSocketMessagePtr& msg) {auto remoteIp = connectionState->getRemoteIp();if (msg->type == ix::WebSocketMessageType::Open){TLogger() << "New connection";connectionState->computeId();TLogger() << "remote ip: " << remoteIp;TLogger() << "id: " << connectionState->getId();TLogger() << "Uri: " << msg->openInfo.uri;TLogger() << "Headers:";for (auto it : msg->openInfo.headers){TLogger() << it.first << ": " << it.second;}connectionId = connectionState->getId();}else if (msg->type == ix::WebSocketMessageType::Close){TLogger() << "Closed connection";}else if (msg->type == ix::WebSocketMessageType::Message){for (auto&& client : server.getClients()){if (client.get() != &webSocket){client->send(msg->str, msg->binary);}}}});auto res = server.listen();if (!res.first){TLogger() << res.second;return false;}server.start();return true;}} // namespace ixTEST_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;bool tls = false;SocketTLSOptions tlsOptions;std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);std::string host("127.0.0.1");auto isCancellationRequested = []() -> bool { return false; };bool success = socket->connect(host, port, errMsg, isCancellationRequested);REQUIRE(success);auto lineResult = socket->readLine(isCancellationRequested);auto lineValid = lineResult.first;REQUIRE(lineValid);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 awayix::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;bool tls = false;SocketTLSOptions tlsOptions;std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);std::string host("127.0.0.1");auto isCancellationRequested = []() -> bool { return false; };bool success = socket->connect(host, port, errMsg, isCancellationRequested);REQUIRE(success);TLogger() << "writeBytes";socket->writeBytes("GET /\r\n", isCancellationRequested);auto lineResult = socket->readLine(isCancellationRequested);auto lineValid = lineResult.first;REQUIRE(lineValid);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 awayix::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;bool tls = false;SocketTLSOptions tlsOptions;std::shared_ptr<Socket> socket = createSocket(tls, -1, errMsg, tlsOptions);std::string host("127.0.0.1");auto isCancellationRequested = []() -> bool { return false; };bool success = socket->connect(host, port, errMsg, isCancellationRequested);REQUIRE(success);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);auto lineResult = socket->readLine(isCancellationRequested);auto lineValid = lineResult.first;REQUIRE(lineValid);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 awayix::msleep(500);server.stop();REQUIRE(connectionId == "foobarConnectionId");REQUIRE(server.getClients().size() == 0);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。