Skip to content

Commit

Permalink
Added unittest for Host header
Browse files Browse the repository at this point in the history
(cherry picked from commit 1e0efb0)
  • Loading branch information
The-EDev committed Aug 24, 2022
1 parent 3423662 commit edf12f6
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down

0 comments on commit edf12f6

Please sign in to comment.