source: uri/tests/test_builder.py@ 587

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

#94 fix URI test code

File size: 8.9 KB
Line 
1# -*- coding: utf-8 -*-
2# Copyright (c) 2017 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.
15"""Module containing the tests for the URIBuilder object."""
16try:
17 from urllib.parse import parse_qs
18except ImportError:
19 from urlparse import parse_qs
20
21import pytest
22
23import rfc3986.builder as builder
24from rfc3986.api import uri_reference
25
26
27def test_builder_default():
28 """Verify the default values."""
29 uribuilder = builder.URIBuilder()
30 assert uribuilder.scheme is None
31 assert uribuilder.userinfo is None
32 assert uribuilder.host is None
33 assert uribuilder.port is None
34 assert uribuilder.path is None
35 assert uribuilder.query is None
36 assert uribuilder.fragment is None
37
38
39def test_from_uri_reference():
40 uri = uri_reference("http://foo.bar:1234/baz")
41 uribuilder = builder.URIBuilder().from_uri(uri)
42 assert uribuilder.scheme == "http"
43 assert uribuilder.userinfo is None
44 assert uribuilder.host == "foo.bar"
45 assert uribuilder.port == "1234"
46 assert uribuilder.path == "/baz"
47 assert uribuilder.query is None
48 assert uribuilder.fragment is None
49
50
51def test_from_uri_string():
52 uribuilder = builder.URIBuilder().from_uri("https://bar.foo:4321/boom")
53 assert uribuilder.scheme == "https"
54 assert uribuilder.userinfo is None
55 assert uribuilder.host == "bar.foo"
56 assert uribuilder.port == "4321"
57 assert uribuilder.path == "/boom"
58 assert uribuilder.query is None
59 assert uribuilder.fragment is None
60
61
62def test_repr():
63 """Verify our repr looks like our class."""
64 uribuilder = builder.URIBuilder()
65 assert repr(uribuilder).startswith("URIBuilder(scheme=None")
66
67
68@pytest.mark.parametrize(
69 "scheme",
70 [
71 "https",
72 "hTTps",
73 "Https",
74 "HtTpS",
75 "HTTPS",
76 ],
77)
78def test_add_scheme(scheme):
79 """Verify schemes are normalized when added."""
80 uribuilder = builder.URIBuilder().add_scheme(scheme)
81 assert uribuilder.scheme == "https"
82
83
84@pytest.mark.parametrize(
85 "username, password, userinfo",
86 [
87 ("user", "pass", "user:pass"),
88 ("user", None, "user"),
89 ("user@domain.com", "password", "user%40domain.com:password"),
90 ("user", "pass:word", "user:pass%3Aword"),
91 ],
92)
93def test_add_credentials(username, password, userinfo):
94 """Verify we normalize usernames and passwords."""
95 uribuilder = builder.URIBuilder().add_credentials(username, password)
96 assert uribuilder.userinfo == userinfo
97
98
99def test_add_credentials_requires_username():
100 """Verify one needs a username to add credentials."""
101 with pytest.raises(ValueError):
102 builder.URIBuilder().add_credentials(None, None)
103
104
105@pytest.mark.parametrize(
106 ["hostname", "expected_hostname"],
107 [
108 ("google.com", "google.com"),
109 ("GOOGLE.COM", "google.com"),
110 ("gOOgLe.COM", "google.com"),
111 ("goOgLE.com", "google.com"),
112 ("[::ff%etH0]", "[::ff%25etH0]"),
113 ("[::ff%25etH0]", "[::ff%25etH0]"),
114 ("[::FF%etH0]", "[::ff%25etH0]"),
115 ],
116)
117def test_add_host(hostname, expected_hostname):
118 """Verify we normalize hostnames in add_host."""
119 uribuilder = builder.URIBuilder().add_host(hostname)
120 assert uribuilder.host == expected_hostname
121
122
123@pytest.mark.parametrize(
124 "port",
125 [
126 -100,
127 "-100",
128 -1,
129 "-1",
130 65536,
131 "65536",
132 1000000,
133 "1000000",
134 "",
135 "abc",
136 "0b10",
137 ],
138)
139def test_add_invalid_port(port):
140 """Verify we raise a ValueError for invalid ports."""
141 with pytest.raises(ValueError):
142 builder.URIBuilder().add_port(port)
143
144
145@pytest.mark.parametrize(
146 "port, expected",
147 [
148 (0, "0"),
149 ("0", "0"),
150 (1, "1"),
151 ("1", "1"),
152 (22, "22"),
153 ("22", "22"),
154 (80, "80"),
155 ("80", "80"),
156 (443, "443"),
157 ("443", "443"),
158 (65535, "65535"),
159 ("65535", "65535"),
160 ],
161)
162def test_add_port(port, expected):
163 """Verify we normalize our port."""
164 uribuilder = builder.URIBuilder().add_port(port)
165 assert uribuilder.port == expected
166
167
168@pytest.mark.parametrize(
169 "path",
170 [
171 "sigmavirus24/rfc3986",
172 "/sigmavirus24/rfc3986",
173 ],
174)
175def test_add_path(path):
176 """Verify we normalize our path value."""
177 uribuilder = builder.URIBuilder().add_path(path)
178 assert uribuilder.path == "/sigmavirus24/rfc3986"
179
180
181@pytest.mark.parametrize(
182 "query_items, expected",
183 [
184 ({"a": "b c"}, "a=b+c"),
185 ({"a": "b+c"}, "a=b%2Bc"),
186 ([("a", "b c")], "a=b+c"),
187 ([("a", "b+c")], "a=b%2Bc"),
188 ([("a", "b"), ("c", "d")], "a=b&c=d"),
189 ([("a", "b"), ("username", "@d")], "a=b&username=%40d"),
190 ([("percent", "%")], "percent=%25"),
191 ],
192)
193def test_add_query_from(query_items, expected):
194 """Verify the behaviour of add_query_from."""
195 uribuilder = builder.URIBuilder().add_query_from(query_items)
196 assert uribuilder.query == expected
197
198
199def test_add_query():
200 """Verify we do not modify the provided query string."""
201 uribuilder = builder.URIBuilder().add_query("username=@foo")
202 assert uribuilder.query == "username=@foo"
203
204
205def test_add_fragment():
206 """Verify our handling of fragments."""
207 uribuilder = builder.URIBuilder().add_fragment("section-2.5.1")
208 assert uribuilder.fragment == "section-2.5.1"
209
210
211@pytest.mark.parametrize(
212 "uri, extend_with, expected_path",
213 [
214 ("https://api.github.com", "/users", "/users"),
215 ("https://api.github.com", "/users/", "/users/"),
216 ("https://api.github.com", "users", "/users"),
217 ("https://api.github.com", "users/", "/users/"),
218 ("", "users/", "/users/"),
219 ("", "users", "/users"),
220 ("?foo=bar", "users", "/users"),
221 (
222 "https://api.github.com/users/",
223 "sigmavirus24",
224 "/users/sigmavirus24",
225 ),
226 (
227 "https://api.github.com/users",
228 "sigmavirus24",
229 "/users/sigmavirus24",
230 ),
231 (
232 "https://api.github.com/users",
233 "/sigmavirus24",
234 "/users/sigmavirus24",
235 ),
236 ],
237)
238def test_extend_path(uri, extend_with, expected_path):
239 """Verify the behaviour of extend_path."""
240 uribuilder = (
241 builder.URIBuilder()
242 .from_uri(uri_reference(uri))
243 .extend_path(extend_with)
244 )
245 assert uribuilder.path == expected_path
246
247
248@pytest.mark.parametrize(
249 "uri, extend_with, expected_query",
250 [
251 (
252 "https://github.com",
253 [("a", "b c"), ("d", "e&f")],
254 {"a": ["b c"], "d": ["e&f"]},
255 ),
256 (
257 "https://github.com?a=0",
258 [("a", "b c"), ("d", "e&f")],
259 {"a": ["0", "b c"], "d": ["e&f"]},
260 ),
261 (
262 "https://github.com?a=0&e=f",
263 [("a", "b c"), ("d", "e&f")],
264 {"a": ["0", "b c"], "e": ["f"], "d": ["e&f"]},
265 ),
266 (
267 "https://github.com",
268 {"a": "b c", "d": "e&f"},
269 {"a": ["b c"], "d": ["e&f"]},
270 ),
271 (
272 "https://github.com?a=0",
273 {"a": "b c", "d": "e&f"},
274 {"a": ["0", "b c"], "d": ["e&f"]},
275 ),
276 (
277 "https://github.com?a=0&e=f",
278 {"a": "b c", "d": "e&f"},
279 {"a": ["0", "b c"], "e": ["f"], "d": ["e&f"]},
280 ),
281 ],
282)
283def test_extend_query_with(uri, extend_with, expected_query):
284 """Verify the behaviour of extend_query_with."""
285 uribuilder = (
286 builder.URIBuilder()
287 .from_uri(uri_reference(uri))
288 .extend_query_with(extend_with)
289 )
290 assert parse_qs(uribuilder.query) == expected_query
291
292
293def test_finalize():
294 """Verify the whole thing."""
295 uri = (
296 builder.URIBuilder()
297 .add_scheme("https")
298 .add_credentials("sigmavirus24", "not-my-re@l-password")
299 .add_host("github.com")
300 .add_path("sigmavirus24/rfc3986")
301 .finalize()
302 .unsplit()
303 )
304 expected = (
305 "https://sigmavirus24:not-my-re%40l-password@github.com/"
306 "sigmavirus24/rfc3986"
307 )
308 assert expected == uri
309
310
311def test_geturl():
312 """Verify the short-cut to the URL."""
313 uri = (
314 builder.URIBuilder()
315 .add_scheme("https")
316 .add_credentials("sigmavirus24", "not-my-re@l-password")
317 .add_host("github.com")
318 .add_path("sigmavirus24/rfc3986")
319 .geturl()
320 )
321 expected = (
322 "https://sigmavirus24:not-my-re%40l-password@github.com/"
323 "sigmavirus24/rfc3986"
324 )
325 assert expected == uri
Note: See TracBrowser for help on using the repository browser.