mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-14 10:29:35 +00:00
add set_hostname_addr_map to WebSocketClient (#2463)
* add set_hostname_addr_map to WebSocketClient * add WebSocketTest unit test cases * SpecifyServerIPAddress_AnotherHostname * SpecifyServerIPAddress_RealHostname * Change wrong_ip from 0.0.0.0 to 192.0.2.1 Use 192.0.2.1 (RFC 5737 documentation address) to ensure it acts as a non-routable address and does not alias to loopback. * Fix style check * set short timeout in WebSocketTest.SpecifyServerIPAddress_RealHostname cannot reach wrong_ip
This commit is contained in:
1 parent
0c2f535b74
commit
907257f51d
2 files changed
+66
-1
No files matched your search
@@ -17706,6 +17706,57 @@ TEST(WebSocketTest, ComplexPath) {
|
||||
EXPECT_TRUE(ws2.is_valid());
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, SpecifyServerIPAddress_AnotherHostname) {
|
||||
Server svr;
|
||||
svr.WebSocket("/ws", [](const Request &, ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {}
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port(HOST);
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
svr.wait_until_ready();
|
||||
|
||||
auto another_host = "example.com";
|
||||
auto wrong_ip = "192.0.2.1";
|
||||
|
||||
ws::WebSocketClient client("ws://localhost:" + std::to_string(port) + "/ws");
|
||||
client.set_hostname_addr_map({{another_host, wrong_ip}});
|
||||
|
||||
ASSERT_TRUE(client.connect());
|
||||
EXPECT_TRUE(client.is_open());
|
||||
client.close();
|
||||
|
||||
svr.stop();
|
||||
t.join();
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, SpecifyServerIPAddress_RealHostname) {
|
||||
Server svr;
|
||||
svr.WebSocket("/ws", [](const Request &, ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {}
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port(HOST);
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
svr.wait_until_ready();
|
||||
|
||||
auto wrong_ip = "192.0.2.1";
|
||||
|
||||
ws::WebSocketClient client("ws://localhost:" + std::to_string(port) + "/ws");
|
||||
client.set_hostname_addr_map({{"localhost", wrong_ip}});
|
||||
client.set_connection_timeout(1);
|
||||
client.set_read_timeout(1);
|
||||
client.set_write_timeout(1);
|
||||
|
||||
EXPECT_FALSE(client.connect());
|
||||
EXPECT_FALSE(client.is_open());
|
||||
|
||||
svr.stop();
|
||||
t.join();
|
||||
}
|
||||
|
||||
class WebSocketIntegrationTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
|
||||
Reference in new issue
Block a user