From 6f2fff57595b3ccab2b6f59d6e66109f19216bf2 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Wed, 17 Jan 2024 13:27:14 +0100 Subject: [PATCH] Added test for WebSocket max message size --- test/websocketserver.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/websocketserver.cpp b/test/websocketserver.cpp index c146f91b1..08d4c35e1 100644 --- a/test/websocketserver.cpp +++ b/test/websocketserver.cpp @@ -24,14 +24,13 @@ template weak_ptr make_weak_ptr(shared_ptr ptr) { return ptr; } void test_websocketserver() { InitLogger(LogLevel::Debug); - const string myMessage = "Hello world from client"; - WebSocketServer::Configuration serverConfig; serverConfig.port = 48080; serverConfig.enableTls = true; // serverConfig.certificatePemFile = ... // serverConfig.keyPemFile = ... serverConfig.bindAddress = "127.0.0.1"; // to test IPv4 fallback + serverConfig.maxMessageSize = 1000; // to test max message size WebSocketServer server(std::move(serverConfig)); shared_ptr client; @@ -63,15 +62,19 @@ void test_websocketserver() { config.disableTlsVerification = true; WebSocket ws(std::move(config)); + const string myMessage = "Hello world from client"; + ws.onOpen([&ws, &myMessage]() { cout << "WebSocket: Open" << endl; + ws.send(binary(1001, byte(0))); // test max message size ws.send(myMessage); }); ws.onClosed([]() { cout << "WebSocket: Closed" << endl; }); std::atomic received = false; - ws.onMessage([&received, &myMessage](variant message) { + std::atomic maxSizeReceived = false; + ws.onMessage([&received, &maxSizeReceived, &myMessage](variant message) { if (holds_alternative(message)) { string str = std::move(get(message)); if ((received = (str == myMessage))) @@ -79,6 +82,13 @@ void test_websocketserver() { else cout << "WebSocket: Received UNEXPECTED message" << endl; } + else { + binary bin = std::move(get(message)); + if ((maxSizeReceived = (bin.size() == 1000))) + cout << "WebSocket: Received large message truncated at max size" << endl; + else + cout << "WebSocket: Received large message NOT TRUNCATED" << endl; + } }); ws.open("wss://localhost:48080/"); @@ -90,8 +100,8 @@ void test_websocketserver() { if (!ws.isOpen()) throw runtime_error("WebSocket is not open"); - if (!received) - throw runtime_error("Expected message not received"); + if (!received || !maxSizeReceived) + throw runtime_error("Expected messages not received"); ws.close(); this_thread::sleep_for(1s);