Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

121 lines · 4.6 KiB · Python

Last changed by

Fix `URL.join(...)` (#144)Tom Christiebefore adoption00e4fdb

History of this file

Take it

1from httpx import URL2from httpx.exceptions import InvalidURL3import pytest456def test_idna_url():7    url = URL("http://中国.icom.museum:80/")8    assert url == URL("http://xn--fiqs8s.icom.museum:80/")9    assert url.host == "xn--fiqs8s.icom.museum"101112def test_url():13    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")14    assert url.scheme == "https"15    assert url.host == "example.org"16    assert url.port == 12317    assert url.authority == "example.org:123"18    assert url.path == "/path/to/somewhere"19    assert url.query == "abc=123"20    assert url.fragment == "anchor"21    assert (22        repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"23    )2425    new = url.copy_with(scheme="http")26    assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")27    assert new.scheme == "http"282930def test_url_eq_str():31    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")32    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"33    assert str(url) == url343536def test_url_params():37    url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})38    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"3940    url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})41    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"424344def test_url_join():45    """46    Some basic URL joining tests.47    """48    url = URL("https://example.org:123/path/to/somewhere")49    assert url.join('/somewhere-else') == "https://example.org:123/somewhere-else"50    assert url.join('somewhere-else') == "https://example.org:123/path/to/somewhere-else"51    assert url.join('../somewhere-else') == "https://example.org:123/path/somewhere-else"52    assert url.join('../../somewhere-else') == "https://example.org:123/somewhere-else"535455def test_url_join_rfc3986():56    """57    URL joining tests, as-per reference examples in RFC 3986.5859    https://tools.ietf.org/html/rfc3986#section-5.460    """6162    url = URL("http://example.com/b/c/d;p?q")6364    with pytest.raises(InvalidURL):65        assert url.join("g:h") == "g:h"6667    assert url.join("g") == "http://example.com/b/c/g"68    assert url.join("./g") == "http://example.com/b/c/g"69    assert url.join("g/") == "http://example.com/b/c/g/"70    assert url.join("/g") == "http://example.com/g"71    assert url.join("//g") == "http://g"72    assert url.join("?y") == "http://example.com/b/c/d;p?y"73    assert url.join("g?y") == "http://example.com/b/c/g?y"74    assert url.join("#s") == "http://example.com/b/c/d;p?q#s"75    assert url.join("g#s") == "http://example.com/b/c/g#s"76    assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"77    assert url.join(";x") == "http://example.com/b/c/;x"78    assert url.join("g;x") == "http://example.com/b/c/g;x"79    assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"80    assert url.join("") == "http://example.com/b/c/d;p?q"81    assert url.join(".") == "http://example.com/b/c/"82    assert url.join("./") == "http://example.com/b/c/"83    assert url.join("..") == "http://example.com/b/"84    assert url.join("../") == "http://example.com/b/"85    assert url.join("../g") == "http://example.com/b/g"86    assert url.join("../..") == "http://example.com/"87    assert url.join("../../") == "http://example.com/"88    assert url.join("../../g") == "http://example.com/g"8990    assert url.join("../../../g") == "http://example.com/g"91    assert url.join("../../../../g") == "http://example.com/g"9293    assert url.join("/./g") == "http://example.com/g"94    assert url.join("/../g") == "http://example.com/g"95    assert url.join("g.") == "http://example.com/b/c/g."96    assert url.join(".g") == "http://example.com/b/c/.g"97    assert url.join("g..") == "http://example.com/b/c/g.."98    assert url.join("..g") == "http://example.com/b/c/..g"99100    assert url.join("./../g") == "http://example.com/b/g"101    assert url.join("./g/.") == "http://example.com/b/c/g/"102    assert url.join("g/./h") == "http://example.com/b/c/g/h"103    assert url.join("g/../h") == "http://example.com/b/c/h"104    assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"105    assert url.join("g;x=1/../y") == "http://example.com/b/c/y"106107    assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"108    assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"109    assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"110    assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"111112113def test_url_set():114    urls = (115        URL("http://example.org:123/path/to/somewhere"),116        URL("http://example.org:123/path/to/somewhere/else"),117    )118119    url_set = set(urls)120121    assert all(url in urls for url in url_set)