Skip to content

Commit edf12f6

Browse files
committed
Added unittest for Host header
(cherry picked from commit 1e0efb0)
1 parent 3423662 commit edf12f6

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/unittest.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,62 @@ TEST_CASE("websocket")
22282228
app.stop();
22292229
} // websocket
22302230

2231+
TEST_CASE("websocket_missing_host")
2232+
{
2233+
static std::string http_message = "GET /ws HTTP/1.1\r\nConnection: keep-alive, Upgrade\r\nupgrade: websocket\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n";
2234+
2235+
static bool connected{false};
2236+
2237+
SimpleApp app;
2238+
2239+
CROW_ROUTE(app, "/ws").websocket()
2240+
.onaccept([&](const crow::request& req, void**) {
2241+
CROW_LOG_INFO << "Accepted websocket with URL " << req.url;
2242+
return true;
2243+
})
2244+
.onopen([&](websocket::connection&) {
2245+
connected = true;
2246+
CROW_LOG_INFO << "Connected websocket and value is " << connected;
2247+
})
2248+
.onmessage([&](websocket::connection& conn, const std::string& message, bool isbin) {
2249+
CROW_LOG_INFO << "Message is \"" << message << '\"';
2250+
if (!isbin && message == "PINGME")
2251+
conn.send_ping("");
2252+
else if (!isbin && message == "Hello")
2253+
conn.send_text("Hello back");
2254+
else if (isbin && message == "Hello bin")
2255+
conn.send_binary("Hello back bin");
2256+
})
2257+
.onclose([&](websocket::connection&, const std::string&) {
2258+
CROW_LOG_INFO << "Closing websocket";
2259+
});
2260+
2261+
app.validate();
2262+
2263+
auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45471).run_async();
2264+
app.wait_for_server_start();
2265+
asio::io_service is;
2266+
2267+
asio::ip::tcp::socket c(is);
2268+
c.connect(asio::ip::tcp::endpoint(
2269+
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45471));
2270+
2271+
2272+
char buf[2048];
2273+
2274+
// Handshake should fail
2275+
{
2276+
std::fill_n(buf, 2048, 0);
2277+
c.send(asio::buffer(http_message));
2278+
2279+
c.receive(asio::buffer(buf, 2048));
2280+
std::this_thread::sleep_for(std::chrono::milliseconds(5));
2281+
CHECK(!connected);
2282+
}
2283+
2284+
app.stop();
2285+
} // websocket
2286+
22312287
#ifdef CROW_ENABLE_COMPRESSION
22322288
TEST_CASE("zlib_compression")
22332289
{

0 commit comments

Comments
 (0)