Skip to content

Commit 41cfd05

Browse files
committed
Fixed ipv6 url replace error and tests type error
1 parent 71508d3 commit 41cfd05

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

baize/datastructures.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,18 @@ def replace(self, **kwargs: typing.Any) -> "URL":
300300
or "hostname" in kwargs
301301
or "port" in kwargs
302302
):
303-
hostname = kwargs.pop("hostname", self.hostname)
303+
hostname = kwargs.pop("hostname", None)
304304
port = kwargs.pop("port", self.port)
305305
username = kwargs.pop("username", self.username)
306306
password = kwargs.pop("password", self.password)
307307

308+
if hostname is None:
309+
netloc = self.netloc
310+
_, _, hostname = netloc.rpartition("@")
311+
312+
if hostname[-1] != "]":
313+
hostname = hostname.rsplit(":", 1)[0]
314+
308315
netloc = hostname
309316
if port is not None:
310317
netloc += f":{port}"

tests/test_datastructures.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import os
32
import tempfile
43

@@ -45,6 +44,24 @@ def test_url():
4544
new = URL(**u.components._asdict())
4645
assert new == u
4746

47+
ipv6_url = URL("https://[fe::2]:12345")
48+
new = ipv6_url.replace(port=8080)
49+
assert new == "https://[fe::2]:8080"
50+
51+
new = ipv6_url.replace(username="username", password="password")
52+
assert new == "https://username:password@[fe::2]:12345"
53+
assert new.netloc == "username:password@[fe::2]:12345"
54+
55+
ipv6_url = URL("https://[fe::2]")
56+
new = ipv6_url.replace(port=123)
57+
assert new == "https://[fe::2]:123"
58+
59+
url = URL("http://u:p@host/")
60+
assert url.replace(hostname="bar") == URL("http://u:p@bar/")
61+
62+
url = URL("http://u:p@host:80")
63+
assert url.replace(port=88) == URL("http://u:p@host:88")
64+
4865

4966
def test_url_query_params():
5067
u = URL("https://example.org/path/?page=3")
@@ -198,7 +215,7 @@ def test_url_blank_params():
198215
assert "abc" in q
199216
assert "def" in q
200217
assert "b" in q
201-
assert len(q.get("abc")) == 0
218+
assert len(q["abc"]) == 0
202219
assert len(q["a"]) == 3
203220
assert list(q.keys()) == ["a", "abc", "def", "b"]
204221

@@ -284,7 +301,7 @@ async def test_async_big_upload_file():
284301

285302

286303
def test_formdata():
287-
upload = io.BytesIO(b"test")
304+
upload = UploadFile("filename", Headers())
288305
form = FormData([("a", "123"), ("a", "456"), ("b", upload)])
289306
assert "a" in form
290307
assert "A" not in form
@@ -309,7 +326,9 @@ def test_formdata():
309326

310327

311328
def test_mutable_multi_mapping():
312-
q = MutableMultiMapping([("a", "123"), ("a", "456"), ("b", "789")])
329+
q: MutableMultiMapping[str, str] = MutableMultiMapping(
330+
[("a", "123"), ("a", "456"), ("b", "789")]
331+
)
313332
assert "a" in q
314333
assert "A" not in q
315334
assert "c" not in q
@@ -393,7 +412,7 @@ def test_mutable_multi_mapping():
393412
q = MutableMultiMapping([("a", "123"), ("b", "456")])
394413
q.update({"a": "789"})
395414
assert q.getlist("a") == ["789"]
396-
q == MutableMultiMapping([("a", "789"), ("b", "456")])
415+
assert q == MutableMultiMapping([("a", "789"), ("b", "456")])
397416

398417
q = MutableMultiMapping([("a", "123"), ("b", "456")])
399418
q.update(q)

0 commit comments

Comments
 (0)