source: uri/tests/test_unicode_support.py@ 830

Last change on this file since 830 was 264, checked in by wouter, 3 years ago

#94 fix URI test code

File size: 2.1 KB
Line 
1# -*- coding: utf-8 -*-
2import pytest
3
4from rfc3986 import exceptions
5from rfc3986 import parseresult
6from rfc3986.api import uri_reference
7from rfc3986.api import urlparse
8
9
10SNOWMAN = b"\xe2\x98\x83"
11SNOWMAN_PARAMS = b"http://example.com?utf8=" + SNOWMAN
12SNOWMAN_HOST = b"http://" + SNOWMAN + b".com"
13SNOWMAN_IDNA_HOST = "http://xn--n3h.com"
14
15
16def test_unicode_uri():
17 url_bytestring = SNOWMAN_PARAMS
18 unicode_url = url_bytestring.decode("utf-8")
19 uri = uri_reference(unicode_url)
20 assert uri.is_valid() is True
21 assert uri == "http://example.com?utf8=%E2%98%83"
22
23
24def test_unicode_uri_passed_as_bytes():
25 url_bytestring = SNOWMAN_PARAMS
26 uri = uri_reference(url_bytestring)
27 assert uri.is_valid() is True
28 assert uri == "http://example.com?utf8=%E2%98%83"
29
30
31def test_unicode_authority():
32 url_bytestring = SNOWMAN_HOST
33 unicode_url = url_bytestring.decode("utf-8")
34 uri = uri_reference(unicode_url)
35 assert uri.is_valid() is False
36 assert uri == unicode_url
37
38
39def test_urlparse_a_unicode_hostname():
40 url_bytestring = SNOWMAN_HOST
41 unicode_url = url_bytestring.decode("utf-8")
42 parsed = urlparse(url_bytestring)
43 assert parsed.host == unicode_url[7:]
44
45
46def test_urlparse_a_unicode_hostname_with_auth():
47 url = b"http://userinfo@" + SNOWMAN + b".com"
48 parsed = urlparse(url)
49 assert parsed.userinfo == "userinfo"
50
51
52def test_urlparse_idna_encoding_with_geturl():
53 """https://github.com/python-hyper/rfc3986/issues/57"""
54 parsed = urlparse("https://i❤.ws")
55 encoded = parsed.encode("idna")
56 assert encoded.encoding == "idna"
57 assert encoded.geturl() == b"https://xn--i-7iq.ws"
58
59
60def test_urlparse_an_invalid_authority_parses_port():
61 url = "http://foo:b@r@[::1]:80/get"
62 parsed = urlparse(url)
63 assert parsed.port == 80
64 assert parsed.userinfo == "foo:b@r"
65 assert parsed.hostname == "[::1]"
66
67
68def test_unsplit_idna_a_unicode_hostname():
69 parsed = urlparse(SNOWMAN_HOST)
70 assert parsed.unsplit(use_idna=True) == SNOWMAN_IDNA_HOST
71
72
73def test_strict_urlparsing():
74 with pytest.raises(exceptions.InvalidAuthority):
75 parseresult.ParseResult.from_string(SNOWMAN_HOST)
Note: See TracBrowser for help on using the repository browser.