Skip to content

Commit 4560b88

Browse files
lesnik512claude
andcommitted
refactor: validate and normalize ClientConfig.base_url in __post_init__
ClientConfig.__post_init__ now: - rejects empty string / non-str base_url with ValueError - strips trailing slash so the stored value is canonical AsyncClient._resolve_url no longer does its own rstrip("/") on base_url since the stored value is already canonical (DRY: one source of truth for what a stored base_url looks like). Closes deferred-work entry: "URL validation" (base_url normalization part; the Request.url non-empty check shipped in the prior Request __post_init__ commit). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 45ddb25 commit 4560b88

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎src/httpware/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _resolve_url(self, path: str) -> str:
119119
base = self._config.base_url
120120
if base is None:
121121
return path
122-
return f"{base.rstrip('/')}/{path.lstrip('/')}"
122+
return f"{base}/{path.lstrip('/')}"
123123

124124
def _build_request(
125125
self,

‎src/httpware/config.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ class ClientConfig:
5656
limits: Limits = field(default_factory=Limits)
5757
decoder: ResponseDecoder = field(default_factory=PydanticDecoder)
5858
middleware: tuple[Middleware, ...] = ()
59+
60+
def __post_init__(self) -> None:
61+
if self.base_url is not None:
62+
if not isinstance(self.base_url, str):
63+
msg = "base_url must be a non-empty string or None"
64+
raise ValueError(msg)
65+
normalized = self.base_url.rstrip("/")
66+
if not normalized:
67+
msg = "base_url must be a non-empty string or None"
68+
raise ValueError(msg)
69+
object.__setattr__(self, "base_url", normalized)

‎tests/test_config.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,43 @@ def test_limits_rejects_negative_keepalive_expiry() -> None:
7676

7777
def test_limits_accepts_zero() -> None:
7878
Limits(max_connections=0, max_keepalive_connections=0, keepalive_expiry=0.0)
79+
80+
81+
def test_client_config_strips_trailing_slash_from_base_url() -> None:
82+
cfg = ClientConfig(base_url="https://api.example.com/")
83+
assert cfg.base_url == "https://api.example.com"
84+
85+
86+
def test_client_config_leaves_base_url_without_trailing_slash() -> None:
87+
cfg = ClientConfig(base_url="https://api.example.com")
88+
assert cfg.base_url == "https://api.example.com"
89+
90+
91+
def test_client_config_strips_multiple_trailing_slashes() -> None:
92+
cfg = ClientConfig(base_url="https://api.example.com///")
93+
assert cfg.base_url == "https://api.example.com"
94+
95+
96+
def test_client_config_allows_none_base_url() -> None:
97+
cfg = ClientConfig(base_url=None)
98+
assert cfg.base_url is None
99+
100+
101+
def test_client_config_rejects_empty_base_url() -> None:
102+
with pytest.raises(ValueError, match="base_url must be a non-empty string or None"):
103+
ClientConfig(base_url="")
104+
105+
106+
def test_client_config_rejects_slash_only_base_url() -> None:
107+
with pytest.raises(ValueError, match="base_url must be a non-empty string or None"):
108+
ClientConfig(base_url="/")
109+
110+
111+
def test_client_config_rejects_multiple_slashes_only_base_url() -> None:
112+
with pytest.raises(ValueError, match="base_url must be a non-empty string or None"):
113+
ClientConfig(base_url="///")
114+
115+
116+
def test_client_config_rejects_non_str_base_url() -> None:
117+
with pytest.raises(ValueError, match="base_url must be a non-empty string or None"):
118+
ClientConfig(base_url=123) # ty: ignore[invalid-argument-type]

0 commit comments

Comments
 (0)