diff --git a/priompt_sdk/client.py b/priompt_sdk/client.py index 086c58f..d7d91ed 100644 --- a/priompt_sdk/client.py +++ b/priompt_sdk/client.py @@ -4,7 +4,7 @@ import os import threading import time -from urllib.parse import urlparse +from urllib.parse import unquote, urlparse import grpc @@ -35,10 +35,10 @@ def _parse_url(raw): if "://" not in raw: return raw, None u = urlparse(raw) - host = u.hostname or "" - if u.port: - host = f"{host}:{u.port}" - return host, u.username + _ = u.port # Preserve urlparse validation without normalizing the authority. + host = u.netloc.rpartition("@")[2] + token = unquote(u.username) if u.username else None + return host, token class PromptClient: diff --git a/tests/test_client.py b/tests/test_client.py index 4d0c799..606faa0 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -337,3 +337,23 @@ def test_token_precedence(monkeypatch): monkeypatch.delenv("PRIOMPT_TOKEN") assert _bearer(PromptClient(url="priompt://urltok@localhost:1")) == "Bearer urltok" assert _bearer(PromptClient("localhost:1")) is None + +@pytest.mark.parametrize("url,expected", [ + ("priompt://sample@[::1]:8443", ("[::1]:8443", "sample")), + ("priompt://[::1]", ("[::1]", None)), + ("priompt://ab%2Bc%3D@Prompts.Example.com:8443", ("Prompts.Example.com:8443", "ab+c=")), + ("priompt://a%40b%2Fc@host", ("host", "a@b/c")), + ("priompt://abc+def@host", ("host", "abc+def")), + ("host:8443", ("host:8443", None)), + ("host", ("host", None)), + ("priompt://host:0", ("host:0", None)), +]) +def test_parse_connection_url_preserves_authority_and_decodes_token(url, expected): + from priompt_sdk.client import _parse_url + assert _parse_url(url) == expected + +@pytest.mark.parametrize("url", ["priompt://host:bad", "priompt://host:-1", "priompt://host:65536"]) +def test_parse_connection_url_still_rejects_invalid_ports(url): + from priompt_sdk.client import _parse_url + with pytest.raises(ValueError): + _parse_url(url)