Added unittest for Host header

(cherry picked from commit 1e0efb0b8b)
This commit is contained in:
The-EDev 2022-08-22 22:32:22 +03:00
parent 3423662496
commit edf12f699e
No known key found for this signature in database
GPG Key ID: 51C45DC0C413DCD9
1 changed files with 56 additions and 0 deletions

View File

@ -2228,6 +2228,62 @@ TEST_CASE("websocket")
app.stop();
} // websocket
TEST_CASE("websocket_missing_host")
{
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";
static bool connected{false};
SimpleApp app;
CROW_ROUTE(app, "/ws").websocket()
.onaccept([&](const crow::request& req, void**) {
CROW_LOG_INFO << "Accepted websocket with URL " << req.url;
return true;
})
.onopen([&](websocket::connection&) {
connected = true;
CROW_LOG_INFO << "Connected websocket and value is " << connected;
})
.onmessage([&](websocket::connection& conn, const std::string& message, bool isbin) {
CROW_LOG_INFO << "Message is \"" << message << '\"';
if (!isbin && message == "PINGME")
conn.send_ping("");
else if (!isbin && message == "Hello")
conn.send_text("Hello back");
else if (isbin && message == "Hello bin")
conn.send_binary("Hello back bin");
})
.onclose([&](websocket::connection&, const std::string&) {
CROW_LOG_INFO << "Closing websocket";
});
app.validate();
auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45471).run_async();
app.wait_for_server_start();
asio::io_service is;
asio::ip::tcp::socket c(is);
c.connect(asio::ip::tcp::endpoint(
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45471));
char buf[2048];
// Handshake should fail
{
std::fill_n(buf, 2048, 0);
c.send(asio::buffer(http_message));
c.receive(asio::buffer(buf, 2048));
std::this_thread::sleep_for(std::chrono::milliseconds(5));
CHECK(!connected);
}
app.stop();
} // websocket
#ifdef CROW_ENABLE_COMPRESSION
TEST_CASE("zlib_compression")
{