From d0d6880a1bbcc0e2f310c94d3240a517275798b2 Mon Sep 17 00:00:00 2001 From: Brunno Vanelli Date: Sat, 8 Oct 2022 12:38:57 +0200 Subject: [PATCH] Fix issue where protocol was changed for ws and wss if no port was provided. --- nats/aio/client.py | 3 ++- tests/test_client.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nats/aio/client.py b/nats/aio/client.py index 194f867a..13b25a36 100644 --- a/nats/aio/client.py +++ b/nats/aio/client.py @@ -1230,7 +1230,8 @@ def _setup_server_pool(self, connect_url: Union[str, List[str]]) -> None: # In case only endpoint with scheme was set. # e.g. nats://demo.nats.io or localhost: - if uri.port is None: + # the ws and wss do not need a default port as the transport will assume 80 and 443, respectively + if uri.port is None and uri.scheme not in ("ws", "wss"): uri = urlparse(f"nats://{uri.hostname}:4222") except ValueError: raise errors.Error("nats: invalid connect url option") diff --git a/tests/test_client.py b/tests/test_client.py index cda60605..b2b2468d 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -187,6 +187,15 @@ def test_connect_syntax_sugar(self): self.assertEqual("", uri.username) self.assertEqual(None, uri.password) + nc = NATS() + nc._setup_server_pool("ws://localhost") + nc._setup_server_pool("wss://localhost") + self.assertEqual(2, len(nc._server_pool)) + self.assertEqual("ws", nc._server_pool[0].uri.scheme) + self.assertEqual(None, nc._server_pool[0].uri.port) + self.assertEqual("wss", nc._server_pool[1].uri.scheme) + self.assertEqual(None, nc._server_pool[1].uri.port) + @async_test async def test_connect_no_servers_on_connect_init(self): nc = NATS()