source: uri/tests/test_parseresult.py@ 711

Last change on this file since 711 was 230, checked in by wouter, 4 years ago

#91 clone https://pypi.org/project/rfc3986/

File size: 6.1 KB
RevLine 
[230]1# -*- coding: utf-8 -*-
2# Copyright (c) 2015 Ian Stapleton Cordasco
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15import rfc3986
16from rfc3986 import exceptions
17from rfc3986 import parseresult as pr
18
19import pytest
20
21from . import base
22
23INVALID_PORTS = ["443:80", "443:80:443", "abcdef", "port", "43port"]
24
25SNOWMAN = b"\xe2\x98\x83"
26SNOWMAN_IDNA_HOST = "http://xn--n3h.com"
27
28
29@pytest.mark.parametrize("port", INVALID_PORTS)
30def test_port_parsing(port):
31 with pytest.raises(exceptions.InvalidPort):
32 rfc3986.urlparse("https://httpbin.org:{0}/get".format(port))
33
34
35@pytest.mark.parametrize(
36 "parts, unsplit",
37 [
38 (("https", None, "httpbin.org"), u"https://httpbin.org"),
39 (("https", "user", "httpbin.org"), u"https://user@httpbin.org"),
40 (
41 ("https", None, "httpbin.org", 443, "/get"),
42 u"https://httpbin.org:443/get",
43 ),
44 (("HTTPS", None, "HTTPBIN.ORG"), u"https://httpbin.org"),
45 ],
46)
47def test_from_parts(parts, unsplit):
48 uri = pr.ParseResult.from_parts(*parts)
49 assert uri.unsplit() == unsplit
50
51
52@pytest.mark.parametrize(
53 "parts, unsplit",
54 [
55 (("https", None, "httpbin.org"), b"https://httpbin.org"),
56 (("https", "user", "httpbin.org"), b"https://user@httpbin.org"),
57 (
58 ("https", None, "httpbin.org", 443, "/get"),
59 b"https://httpbin.org:443/get",
60 ),
61 (("HTTPS", None, "HTTPBIN.ORG"), b"https://httpbin.org"),
62 ],
63)
64def test_bytes_from_parts(parts, unsplit):
65 uri = pr.ParseResultBytes.from_parts(*parts)
66 assert uri.unsplit() == unsplit
67
68
69class TestParseResultParsesURIs(base.BaseTestParsesURIs):
70 test_class = pr.ParseResult
71
72
73class TestParseResultUnsplits(base.BaseTestUnsplits):
74 test_class = pr.ParseResult
75
76
77def test_normalizes_uris_when_using_from_string(uri_to_normalize):
78 """Verify we always get the same thing out as we expect."""
79 result = pr.ParseResult.from_string(
80 uri_to_normalize, lazy_normalize=False
81 )
82 assert result.scheme == "https"
83 assert result.host == "example.com"
84
85
86class TestStdlibShims:
87 def test_uri_with_everything(self, uri_with_everything):
88 uri = pr.ParseResult.from_string(uri_with_everything)
89 assert uri.host == uri.hostname
90 assert uri.netloc == uri.authority
91 assert uri.query == uri.params
92 assert uri.geturl() == uri.unsplit()
93
94
95def test_creates_a_copy_with_a_new_path(uri_with_everything):
96 uri = pr.ParseResult.from_string(uri_with_everything)
97 new_uri = uri.copy_with(path="/parse/result/tests/are/fun")
98 assert new_uri.path == "/parse/result/tests/are/fun"
99
100
101def test_creates_a_copy_with_a_new_port(basic_uri):
102 uri = pr.ParseResult.from_string(basic_uri)
103 new_uri = uri.copy_with(port=443)
104 assert new_uri.port == 443
105
106
107def test_parse_result_encodes_itself(uri_with_everything):
108 uri = pr.ParseResult.from_string(uri_with_everything)
109 uribytes = uri.encode()
110 encoding = uri.encoding
111 assert uri.scheme.encode(encoding) == uribytes.scheme
112 assert uri.userinfo.encode(encoding) == uribytes.userinfo
113 assert uri.host.encode(encoding) == uribytes.host
114 assert uri.port == uribytes.port
115 assert uri.path.encode(encoding) == uribytes.path
116 assert uri.query.encode(encoding) == uribytes.query
117 assert uri.fragment.encode(encoding) == uribytes.fragment
118
119
120class TestParseResultBytes:
121 def test_handles_uri_with_everything(self, uri_with_everything):
122 uri = pr.ParseResultBytes.from_string(uri_with_everything)
123 assert uri.scheme == b"https"
124 assert uri.path == b"/path/to/resource"
125 assert uri.query == b"key=value"
126 assert uri.fragment == b"fragment"
127 assert uri.userinfo == b"user:pass"
128 assert uri.port == 443
129 assert isinstance(uri.authority, bytes) is True
130
131 def test_raises_invalid_authority_for_invalid_uris(self, invalid_uri):
132 with pytest.raises(exceptions.InvalidAuthority):
133 pr.ParseResultBytes.from_string(invalid_uri)
134
135 @pytest.mark.parametrize("port", INVALID_PORTS)
136 def test_raises_invalid_port_non_strict_parse(self, port):
137 with pytest.raises(exceptions.InvalidPort):
138 pr.ParseResultBytes.from_string(
139 "https://httpbin.org:{0}/get".format(port), strict=False
140 )
141
142 def test_copy_with_a_new_path(self, uri_with_everything):
143 uri = pr.ParseResultBytes.from_string(uri_with_everything)
144 new_uri = uri.copy_with(path=b"/parse/result/tests/are/fun")
145 assert new_uri.path == b"/parse/result/tests/are/fun"
146
147 def test_copy_with_a_new_unicode_path(self, uri_with_everything):
148 uri = pr.ParseResultBytes.from_string(uri_with_everything)
149 pathbytes = b"/parse/result/tests/are/fun" + SNOWMAN
150 new_uri = uri.copy_with(path=pathbytes.decode("utf-8"))
151 assert new_uri.path == (b"/parse/result/tests/are/fun" + SNOWMAN)
152
153 def test_unsplit(self):
154 uri = pr.ParseResultBytes.from_string(
155 b"http://" + SNOWMAN + b".com/path", strict=False
156 )
157 idna_encoded = SNOWMAN_IDNA_HOST.encode("utf-8") + b"/path"
158 assert uri.unsplit(use_idna=True) == idna_encoded
159
160 def test_eager_normalization_from_string(self):
161 uri = pr.ParseResultBytes.from_string(
162 b"http://" + SNOWMAN + b".com/path",
163 strict=False,
164 lazy_normalize=False,
165 )
166 assert uri.unsplit() == b"http:/path"
167
168 def test_eager_normalization_from_parts(self):
169 uri = pr.ParseResultBytes.from_parts(
170 scheme="http",
171 host=SNOWMAN.decode("utf-8"),
172 path="/path",
173 lazy_normalize=False,
174 )
175 assert uri.unsplit() == b"http:/path"
Note: See TracBrowser for help on using the repository browser.