[230] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 | import pytest
|
---|
| 3 |
|
---|
| 4 | from rfc3986 import exceptions
|
---|
| 5 | from rfc3986 import parseresult
|
---|
| 6 | from rfc3986 import uri_reference
|
---|
| 7 | from rfc3986 import urlparse
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | SNOWMAN = b"\xe2\x98\x83"
|
---|
| 11 | SNOWMAN_PARAMS = b"http://example.com?utf8=" + SNOWMAN
|
---|
| 12 | SNOWMAN_HOST = b"http://" + SNOWMAN + b".com"
|
---|
| 13 | SNOWMAN_IDNA_HOST = "http://xn--n3h.com"
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | def 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 |
|
---|
| 24 | def 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 |
|
---|
| 31 | def 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 |
|
---|
| 39 | def 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 |
|
---|
| 46 | def 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 |
|
---|
| 52 | def 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 |
|
---|
| 60 | def 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 |
|
---|
| 68 | def test_unsplit_idna_a_unicode_hostname():
|
---|
| 69 | parsed = urlparse(SNOWMAN_HOST)
|
---|
| 70 | assert parsed.unsplit(use_idna=True) == SNOWMAN_IDNA_HOST
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | def test_strict_urlparsing():
|
---|
| 74 | with pytest.raises(exceptions.InvalidAuthority):
|
---|
| 75 | parseresult.ParseResult.from_string(SNOWMAN_HOST)
|
---|