From e72e78180e5abd6194f3bdb27f91c644eaeadee2 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 12 Jun 2020 10:17:17 +0200 Subject: [PATCH] let system choose port of test server --- test/webfuse/tests/adapter/test_client.cc | 4 ++-- test/webfuse/utils/threaded_ws_server.cc | 19 +++++++++++++++++++ test/webfuse/utils/threaded_ws_server.h | 4 +++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/test/webfuse/tests/adapter/test_client.cc b/test/webfuse/tests/adapter/test_client.cc index c548b85..11d30e6 100644 --- a/test/webfuse/tests/adapter/test_client.cc +++ b/test/webfuse/tests/adapter/test_client.cc @@ -112,7 +112,7 @@ TEST(client, general_usage) TEST(client, connect) { - ThreadedWsServer server(54321); + ThreadedWsServer server; context ctx; ctx.state = connection_state::connecting; @@ -120,7 +120,7 @@ TEST(client, connect) wf_client * client = wf_client_create( &callback2, reinterpret_cast(&ctx)); - wf_client_connect(client, "ws://localhost:54321/"); + wf_client_connect(client, server.GetUrl().c_str()); while (ctx.state != connection_state::connected) { wf_client_service(client); diff --git a/test/webfuse/utils/threaded_ws_server.cc b/test/webfuse/utils/threaded_ws_server.cc index 40f2e43..6d1df1b 100644 --- a/test/webfuse/utils/threaded_ws_server.cc +++ b/test/webfuse/utils/threaded_ws_server.cc @@ -7,6 +7,7 @@ #include #include #include +#include #define TIMEOUT (std::chrono::milliseconds(10 * 1000)) @@ -35,6 +36,7 @@ public: explicit Private(int port); ~Private(); void WaitForConnection(); + std::string GetUrl() const; void OnConnected(lws * wsi) override; void OnConnectionClosed(lws * wsi) override; @@ -108,6 +110,12 @@ void ThreadedWsServer::WaitForConnection() d->WaitForConnection(); } +std::string ThreadedWsServer::GetUrl() const +{ + return d->GetUrl(); +} + + ThreadedWsServer::Private::Private(int port) : port_(port) , is_connected(false) @@ -130,9 +138,13 @@ ThreadedWsServer::Private::Private(int port) info.vhost_name = "localhost"; info.ws_ping_pong_interval = 10; info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; + info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS; ws_context = lws_create_context(&info); + struct lws_vhost * vhost = lws_create_vhost(ws_context, &info); + port_ = lws_get_vhost_port(vhost); + context = std::thread(&run, this); } @@ -193,5 +205,12 @@ void ThreadedWsServer::Private::OnConnectionClosed(lws * wsi) } } +std::string ThreadedWsServer::Private::GetUrl() const +{ + std::ostringstream stream; + stream << "ws://localhost:" << port_ << "/"; + return stream.str(); +} + } \ No newline at end of file diff --git a/test/webfuse/utils/threaded_ws_server.h b/test/webfuse/utils/threaded_ws_server.h index 4e908d3..127dcf4 100644 --- a/test/webfuse/utils/threaded_ws_server.h +++ b/test/webfuse/utils/threaded_ws_server.h @@ -3,6 +3,7 @@ #include #include +#include namespace webfuse_test { @@ -12,9 +13,10 @@ class ThreadedWsServer ThreadedWsServer(ThreadedWsServer const &) = delete; ThreadedWsServer & operator=(ThreadedWsServer const &) = delete; public: - explicit ThreadedWsServer(int port); + explicit ThreadedWsServer(int port = 0); ~ThreadedWsServer(); void WaitForConnection(); + std::string GetUrl() const; private: class Private; Private * d;