Skip to content

Commit 20e2fa6

Browse files
committed
test(client): cover _to_ws_url scheme/port/path-prefix handling
Helper had no coverage. Pins scheme mapping (http/https/ws/wss/unknown), port preservation, the path-prefix regression with and without a scheme, bare-host parsing, trailing-slash dedup, and the empty-url ValueError.
1 parent 20bc40a commit 20e2fa6

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

tests/test_client_url.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Unit tests for zhub.client._to_ws_url — the http/ws → wss URL builder.
2+
3+
The helper documents that it preserves both the port and any path prefix
4+
(a hub may be reverse-proxied under e.g. example.com/zhub/). Port was always
5+
kept; the path prefix used to be dropped whenever a scheme was present, which
6+
silently routed publishers/clients to the wrong WS endpoint behind a proxy.
7+
"""
8+
9+
from zhub.client import _to_ws_url
10+
11+
import pytest
12+
13+
14+
def test_scheme_to_secure_ws():
15+
assert _to_ws_url("https://hub.example.com", "/ws/publish") == "wss://hub.example.com/ws/publish"
16+
assert _to_ws_url("http://hub.example.com", "/ws/publish") == "ws://hub.example.com/ws/publish"
17+
18+
19+
def test_already_ws_scheme_preserved():
20+
assert _to_ws_url("ws://localhost:8080", "/ws/connect") == "ws://localhost:8080/ws/connect"
21+
assert _to_ws_url("wss://hub.example.com", "/ws/connect") == "wss://hub.example.com/ws/connect"
22+
23+
24+
def test_unknown_scheme_defaults_to_secure():
25+
assert _to_ws_url("ftp://hub.example.com", "/ws/publish") == "wss://hub.example.com/ws/publish"
26+
27+
28+
def test_port_is_preserved():
29+
assert _to_ws_url("https://hub.example.com:9000", "/ws/publish") == "wss://hub.example.com:9000/ws/publish"
30+
31+
32+
def test_path_prefix_is_preserved_with_scheme():
33+
# regression: the prefix used to be dropped when a scheme was present
34+
assert _to_ws_url("https://hub.example.com/zhub", "/ws/publish") == "wss://hub.example.com/zhub/ws/publish"
35+
36+
37+
def test_port_and_path_prefix_both_preserved():
38+
assert (
39+
_to_ws_url("https://hub.example.com:9000/api/zhub", "/ws/connect")
40+
== "wss://hub.example.com:9000/api/zhub/ws/connect"
41+
)
42+
43+
44+
def test_bare_host_accepted():
45+
assert _to_ws_url("hub.example.com", "/ws/expose") == "wss://hub.example.com/ws/expose"
46+
47+
48+
def test_bare_host_with_prefix_preserved():
49+
assert _to_ws_url("hub.example.com/zhub", "/ws/publish") == "wss://hub.example.com/zhub/ws/publish"
50+
51+
52+
def test_trailing_slash_does_not_double():
53+
assert _to_ws_url("https://hub.example.com/", "/ws/publish") == "wss://hub.example.com/ws/publish"
54+
55+
56+
def test_empty_url_raises():
57+
with pytest.raises(ValueError):
58+
_to_ws_url("", "/ws/publish")

0 commit comments

Comments
 (0)