Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions priompt_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import threading
import time
from urllib.parse import urlparse
from urllib.parse import unquote, urlparse

import grpc

Expand Down Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading