| 17 | |
| 18 | |
| 19 | def test_url() -> None: |
| 20 | u = URL("https://example.org:123/path/to/somewhere?abc=123#anchor") |
| 21 | assert u.scheme == "https" |
| 22 | assert u.hostname == "example.org" |
| 23 | assert u.port == 123 |
| 24 | assert u.netloc == "example.org:123" |
| 25 | assert u.username is None |
| 26 | assert u.password is None |
| 27 | assert u.path == "/path/to/somewhere" |
| 28 | assert u.query == "abc=123" |
| 29 | assert u.fragment == "anchor" |
| 30 | |
| 31 | new = u.replace(scheme="http") |
| 32 | assert new == "http://example.org:123/path/to/somewhere?abc=123#anchor" |
| 33 | assert new.scheme == "http" |
| 34 | |
| 35 | new = u.replace(port=None) |
| 36 | assert new == "https://example.org/path/to/somewhere?abc=123#anchor" |
| 37 | assert new.port is None |
| 38 | |
| 39 | new = u.replace(hostname="example.com") |
| 40 | assert new == "https://example.com:123/path/to/somewhere?abc=123#anchor" |
| 41 | assert new.hostname == "example.com" |
| 42 | |
| 43 | ipv6_url = URL("https://[fe::2]:12345") |
| 44 | new = ipv6_url.replace(port=8080) |
| 45 | assert new == "https://[fe::2]:8080" |
| 46 | |
| 47 | new = ipv6_url.replace(username="username", password="password") |
| 48 | assert new == "https://username:password@[fe::2]:12345" |
| 49 | assert new.netloc == "username:password@[fe::2]:12345" |
| 50 | |
| 51 | ipv6_url = URL("https://[fe::2]") |
| 52 | new = ipv6_url.replace(port=123) |
| 53 | assert new == "https://[fe::2]:123" |
| 54 | |
| 55 | url = URL("http://u:p@host/") |
| 56 | assert url.replace(hostname="bar") == URL("http://u:p@bar/") |
| 57 | |
| 58 | url = URL("http://u:p@host:80") |
| 59 | assert url.replace(port=88) == URL("http://u:p@host:88") |
| 60 | |
| 61 | url = URL("http://host:80") |
| 62 | assert url.replace(username="u") == URL("http://u@host:80") |
| 63 | |
| 64 | # Replacing authority components on a URL that has no authority should not |
| 65 | # raise, and should add the requested component to the netloc. |
| 66 | url = URL("/path?a=1") |
| 67 | assert url.replace(port=8080) == URL("//:8080/path?a=1") |
| 68 | assert url.replace(port=8080).port == 8080 |
| 69 | assert url.replace(username="u") == URL("//u@/path?a=1") |
| 70 | |
| 71 | |
| 72 | def test_url_query_params() -> None: |