WebSocket handshake response is deterministic on failure:

Add test for websocket handshake failure HTTP response

fix #2364
close #2365
This commit is contained in:
Richard Hodges committed 2021-12-22 12:00:17 +01:00
1 parent 55d2ceb627
commit b5a94db2a5
4 files changed
+116 -6

No files matched your search

+92
View File
@@ -724,6 +724,97 @@ public:
}
#endif
void
testIssue2364()
{
{ // sync unauthorized
net::io_context ioc;
stream<test::stream> ws{ioc};
auto tr = connect(ws.next_layer());
std::string const reply =
"HTTP/1.1 401 Unauthorized\r\n"
"\r\n";
ws.next_layer().append(reply);
tr.close();
try
{
websocket::response_type response;
error_code ec;
ws.handshake(response, "localhost:80", "/", ec);
BEAST_EXPECT(ec);
BEAST_EXPECTS(response.result() == http::status::unauthorized,
std::to_string(response.result_int()));
}
catch(system_error const&)
{
fail();
}
}
{ // sync invalid response
net::io_context ioc;
stream<test::stream> ws{ioc};
auto tr = connect(ws.next_layer());
std::string const reply =
"zzzzzzzzzzzzzzzzzzzzz";
ws.next_layer().append(reply);
tr.close();
try
{
websocket::response_type response;
error_code ec;
ws.handshake(response, "localhost:80", "/", ec);
BEAST_EXPECT(ec);
BEAST_EXPECTS(response.result() == http::status::internal_server_error,
std::to_string(response.result_int()));
}
catch(system_error const&)
{
fail();
}
}
{ // async unauthorized
net::io_context ioc;
stream<test::stream> ws{ioc};
auto tr = connect(ws.next_layer());
std::string const reply =
"HTTP/1.1 401 Unauthorized\r\n"
"\r\n";
ws.next_layer().append(reply);
tr.close();
websocket::response_type response;
auto handler = [&](error_code ec)
{
BEAST_EXPECT(ec);
BEAST_EXPECTS(response.result() == http::status::unauthorized,
std::to_string(response.result_int()));
};
ws.async_handshake(response, "localhost:80", "/", handler);
ioc.run();
}
{ // async invalid response
net::io_context ioc;
stream<test::stream> ws{ioc};
auto tr = connect(ws.next_layer());
std::string const reply =
"zzzzzzzzzzzzzzzz";
ws.next_layer().append(reply);
tr.close();
websocket::response_type response;
auto handler = [&](error_code ec)
{
BEAST_EXPECT(ec);
BEAST_EXPECTS(response.result() == http::status::internal_server_error,
std::to_string(response.result_int()));
};
ws.async_handshake(response, "localhost:80", "/", handler);
ioc.run();
}
}
void
run() override
{
@@ -737,6 +828,7 @@ public:
#if BOOST_ASIO_HAS_CO_AWAIT
boost::ignore_unused(&handshake_test::testAwaitableCompiles);
#endif
testIssue2364();
}
};