Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

134 lines · 4.9 KiB · Python

Last changed by

Switch hosts on HSTS preload to HTTPS (#151)Seth Michael Larsonbefore adoption3ba2e8c

History of this file

Take it

1import pytest23from httpx import URL4from httpx.exceptions import InvalidURL567def test_idna_url():8    url = URL("http://中国.icom.museum:80/")9    assert url == URL("http://xn--fiqs8s.icom.museum:80/")10    assert url.host == "xn--fiqs8s.icom.museum"111213def test_url():14    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")15    assert url.scheme == "https"16    assert url.host == "example.org"17    assert url.port == 12318    assert url.authority == "example.org:123"19    assert url.path == "/path/to/somewhere"20    assert url.query == "abc=123"21    assert url.fragment == "anchor"22    assert (23        repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"24    )2526    new = url.copy_with(scheme="http")27    assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")28    assert new.scheme == "http"293031def test_url_eq_str():32    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")33    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"34    assert str(url) == url353637def test_url_params():38    url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})39    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"4041    url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})42    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"434445def test_url_join():46    """47    Some basic URL joining tests.48    """49    url = URL("https://example.org:123/path/to/somewhere")50    assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"51    assert (52        url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"53    )54    assert (55        url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"56    )57    assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"585960def test_url_join_rfc3986():61    """62    URL joining tests, as-per reference examples in RFC 3986.6364    https://tools.ietf.org/html/rfc3986#section-5.465    """6667    url = URL("http://example.com/b/c/d;p?q")6869    with pytest.raises(InvalidURL):70        assert url.join("g:h") == "g:h"7172    assert url.join("g") == "http://example.com/b/c/g"73    assert url.join("./g") == "http://example.com/b/c/g"74    assert url.join("g/") == "http://example.com/b/c/g/"75    assert url.join("/g") == "http://example.com/g"76    assert url.join("//g") == "http://g"77    assert url.join("?y") == "http://example.com/b/c/d;p?y"78    assert url.join("g?y") == "http://example.com/b/c/g?y"79    assert url.join("#s") == "http://example.com/b/c/d;p?q#s"80    assert url.join("g#s") == "http://example.com/b/c/g#s"81    assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"82    assert url.join(";x") == "http://example.com/b/c/;x"83    assert url.join("g;x") == "http://example.com/b/c/g;x"84    assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"85    assert url.join("") == "http://example.com/b/c/d;p?q"86    assert url.join(".") == "http://example.com/b/c/"87    assert url.join("./") == "http://example.com/b/c/"88    assert url.join("..") == "http://example.com/b/"89    assert url.join("../") == "http://example.com/b/"90    assert url.join("../g") == "http://example.com/b/g"91    assert url.join("../..") == "http://example.com/"92    assert url.join("../../") == "http://example.com/"93    assert url.join("../../g") == "http://example.com/g"9495    assert url.join("../../../g") == "http://example.com/g"96    assert url.join("../../../../g") == "http://example.com/g"9798    assert url.join("/./g") == "http://example.com/g"99    assert url.join("/../g") == "http://example.com/g"100    assert url.join("g.") == "http://example.com/b/c/g."101    assert url.join(".g") == "http://example.com/b/c/.g"102    assert url.join("g..") == "http://example.com/b/c/g.."103    assert url.join("..g") == "http://example.com/b/c/..g"104105    assert url.join("./../g") == "http://example.com/b/g"106    assert url.join("./g/.") == "http://example.com/b/c/g/"107    assert url.join("g/./h") == "http://example.com/b/c/g/h"108    assert url.join("g/../h") == "http://example.com/b/c/h"109    assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"110    assert url.join("g;x=1/../y") == "http://example.com/b/c/y"111112    assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"113    assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"114    assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"115    assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"116117118def test_url_set():119    urls = (120        URL("http://example.org:123/path/to/somewhere"),121        URL("http://example.org:123/path/to/somewhere/else"),122    )123124    url_set = set(urls)125126    assert all(url in urls for url in url_set)127128129def test_hsts_preload_converted_to_https():130    url = URL("http://www.paypal.com")131132    assert url.is_ssl133    assert url.scheme == "https"134    assert url == "https://www.paypal.com"