1 | n/a | import unittest |
---|
2 | n/a | import urllib.parse |
---|
3 | n/a | |
---|
4 | n/a | RFC1808_BASE = "http://a/b/c/d;p?q#f" |
---|
5 | n/a | RFC2396_BASE = "http://a/b/c/d;p?q" |
---|
6 | n/a | RFC3986_BASE = 'http://a/b/c/d;p?q' |
---|
7 | n/a | SIMPLE_BASE = 'http://a/b/c/d' |
---|
8 | n/a | |
---|
9 | n/a | # A list of test cases. Each test case is a two-tuple that contains |
---|
10 | n/a | # a string with the query and a dictionary with the expected result. |
---|
11 | n/a | |
---|
12 | n/a | parse_qsl_test_cases = [ |
---|
13 | n/a | ("", []), |
---|
14 | n/a | ("&", []), |
---|
15 | n/a | ("&&", []), |
---|
16 | n/a | ("=", [('', '')]), |
---|
17 | n/a | ("=a", [('', 'a')]), |
---|
18 | n/a | ("a", [('a', '')]), |
---|
19 | n/a | ("a=", [('a', '')]), |
---|
20 | n/a | ("&a=b", [('a', 'b')]), |
---|
21 | n/a | ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]), |
---|
22 | n/a | ("a=1&a=2", [('a', '1'), ('a', '2')]), |
---|
23 | n/a | (b"", []), |
---|
24 | n/a | (b"&", []), |
---|
25 | n/a | (b"&&", []), |
---|
26 | n/a | (b"=", [(b'', b'')]), |
---|
27 | n/a | (b"=a", [(b'', b'a')]), |
---|
28 | n/a | (b"a", [(b'a', b'')]), |
---|
29 | n/a | (b"a=", [(b'a', b'')]), |
---|
30 | n/a | (b"&a=b", [(b'a', b'b')]), |
---|
31 | n/a | (b"a=a+b&b=b+c", [(b'a', b'a b'), (b'b', b'b c')]), |
---|
32 | n/a | (b"a=1&a=2", [(b'a', b'1'), (b'a', b'2')]), |
---|
33 | n/a | (";", []), |
---|
34 | n/a | (";;", []), |
---|
35 | n/a | (";a=b", [('a', 'b')]), |
---|
36 | n/a | ("a=a+b;b=b+c", [('a', 'a b'), ('b', 'b c')]), |
---|
37 | n/a | ("a=1;a=2", [('a', '1'), ('a', '2')]), |
---|
38 | n/a | (b";", []), |
---|
39 | n/a | (b";;", []), |
---|
40 | n/a | (b";a=b", [(b'a', b'b')]), |
---|
41 | n/a | (b"a=a+b;b=b+c", [(b'a', b'a b'), (b'b', b'b c')]), |
---|
42 | n/a | (b"a=1;a=2", [(b'a', b'1'), (b'a', b'2')]), |
---|
43 | n/a | ] |
---|
44 | n/a | |
---|
45 | n/a | parse_qs_test_cases = [ |
---|
46 | n/a | ("", {}), |
---|
47 | n/a | ("&", {}), |
---|
48 | n/a | ("&&", {}), |
---|
49 | n/a | ("=", {'': ['']}), |
---|
50 | n/a | ("=a", {'': ['a']}), |
---|
51 | n/a | ("a", {'a': ['']}), |
---|
52 | n/a | ("a=", {'a': ['']}), |
---|
53 | n/a | ("&a=b", {'a': ['b']}), |
---|
54 | n/a | ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}), |
---|
55 | n/a | ("a=1&a=2", {'a': ['1', '2']}), |
---|
56 | n/a | (b"", {}), |
---|
57 | n/a | (b"&", {}), |
---|
58 | n/a | (b"&&", {}), |
---|
59 | n/a | (b"=", {b'': [b'']}), |
---|
60 | n/a | (b"=a", {b'': [b'a']}), |
---|
61 | n/a | (b"a", {b'a': [b'']}), |
---|
62 | n/a | (b"a=", {b'a': [b'']}), |
---|
63 | n/a | (b"&a=b", {b'a': [b'b']}), |
---|
64 | n/a | (b"a=a+b&b=b+c", {b'a': [b'a b'], b'b': [b'b c']}), |
---|
65 | n/a | (b"a=1&a=2", {b'a': [b'1', b'2']}), |
---|
66 | n/a | (";", {}), |
---|
67 | n/a | (";;", {}), |
---|
68 | n/a | (";a=b", {'a': ['b']}), |
---|
69 | n/a | ("a=a+b;b=b+c", {'a': ['a b'], 'b': ['b c']}), |
---|
70 | n/a | ("a=1;a=2", {'a': ['1', '2']}), |
---|
71 | n/a | (b";", {}), |
---|
72 | n/a | (b";;", {}), |
---|
73 | n/a | (b";a=b", {b'a': [b'b']}), |
---|
74 | n/a | (b"a=a+b;b=b+c", {b'a': [b'a b'], b'b': [b'b c']}), |
---|
75 | n/a | (b"a=1;a=2", {b'a': [b'1', b'2']}), |
---|
76 | n/a | ] |
---|
77 | n/a | |
---|
78 | n/a | class UrlParseTestCase(unittest.TestCase): |
---|
79 | n/a | |
---|
80 | n/a | def checkRoundtrips(self, url, parsed, split): |
---|
81 | n/a | result = urllib.parse.urlparse(url) |
---|
82 | n/a | self.assertEqual(result, parsed) |
---|
83 | n/a | t = (result.scheme, result.netloc, result.path, |
---|
84 | n/a | result.params, result.query, result.fragment) |
---|
85 | n/a | self.assertEqual(t, parsed) |
---|
86 | n/a | # put it back together and it should be the same |
---|
87 | n/a | result2 = urllib.parse.urlunparse(result) |
---|
88 | n/a | self.assertEqual(result2, url) |
---|
89 | n/a | self.assertEqual(result2, result.geturl()) |
---|
90 | n/a | |
---|
91 | n/a | # the result of geturl() is a fixpoint; we can always parse it |
---|
92 | n/a | # again to get the same result: |
---|
93 | n/a | result3 = urllib.parse.urlparse(result.geturl()) |
---|
94 | n/a | self.assertEqual(result3.geturl(), result.geturl()) |
---|
95 | n/a | self.assertEqual(result3, result) |
---|
96 | n/a | self.assertEqual(result3.scheme, result.scheme) |
---|
97 | n/a | self.assertEqual(result3.netloc, result.netloc) |
---|
98 | n/a | self.assertEqual(result3.path, result.path) |
---|
99 | n/a | self.assertEqual(result3.params, result.params) |
---|
100 | n/a | self.assertEqual(result3.query, result.query) |
---|
101 | n/a | self.assertEqual(result3.fragment, result.fragment) |
---|
102 | n/a | self.assertEqual(result3.username, result.username) |
---|
103 | n/a | self.assertEqual(result3.password, result.password) |
---|
104 | n/a | self.assertEqual(result3.hostname, result.hostname) |
---|
105 | n/a | self.assertEqual(result3.port, result.port) |
---|
106 | n/a | |
---|
107 | n/a | # check the roundtrip using urlsplit() as well |
---|
108 | n/a | result = urllib.parse.urlsplit(url) |
---|
109 | n/a | self.assertEqual(result, split) |
---|
110 | n/a | t = (result.scheme, result.netloc, result.path, |
---|
111 | n/a | result.query, result.fragment) |
---|
112 | n/a | self.assertEqual(t, split) |
---|
113 | n/a | result2 = urllib.parse.urlunsplit(result) |
---|
114 | n/a | self.assertEqual(result2, url) |
---|
115 | n/a | self.assertEqual(result2, result.geturl()) |
---|
116 | n/a | |
---|
117 | n/a | # check the fixpoint property of re-parsing the result of geturl() |
---|
118 | n/a | result3 = urllib.parse.urlsplit(result.geturl()) |
---|
119 | n/a | self.assertEqual(result3.geturl(), result.geturl()) |
---|
120 | n/a | self.assertEqual(result3, result) |
---|
121 | n/a | self.assertEqual(result3.scheme, result.scheme) |
---|
122 | n/a | self.assertEqual(result3.netloc, result.netloc) |
---|
123 | n/a | self.assertEqual(result3.path, result.path) |
---|
124 | n/a | self.assertEqual(result3.query, result.query) |
---|
125 | n/a | self.assertEqual(result3.fragment, result.fragment) |
---|
126 | n/a | self.assertEqual(result3.username, result.username) |
---|
127 | n/a | self.assertEqual(result3.password, result.password) |
---|
128 | n/a | self.assertEqual(result3.hostname, result.hostname) |
---|
129 | n/a | self.assertEqual(result3.port, result.port) |
---|
130 | n/a | |
---|
131 | n/a | def test_qsl(self): |
---|
132 | n/a | for orig, expect in parse_qsl_test_cases: |
---|
133 | n/a | result = urllib.parse.parse_qsl(orig, keep_blank_values=True) |
---|
134 | n/a | self.assertEqual(result, expect, "Error parsing %r" % orig) |
---|
135 | n/a | expect_without_blanks = [v for v in expect if len(v[1])] |
---|
136 | n/a | result = urllib.parse.parse_qsl(orig, keep_blank_values=False) |
---|
137 | n/a | self.assertEqual(result, expect_without_blanks, |
---|
138 | n/a | "Error parsing %r" % orig) |
---|
139 | n/a | |
---|
140 | n/a | def test_qs(self): |
---|
141 | n/a | for orig, expect in parse_qs_test_cases: |
---|
142 | n/a | result = urllib.parse.parse_qs(orig, keep_blank_values=True) |
---|
143 | n/a | self.assertEqual(result, expect, "Error parsing %r" % orig) |
---|
144 | n/a | expect_without_blanks = {v: expect[v] |
---|
145 | n/a | for v in expect if len(expect[v][0])} |
---|
146 | n/a | result = urllib.parse.parse_qs(orig, keep_blank_values=False) |
---|
147 | n/a | self.assertEqual(result, expect_without_blanks, |
---|
148 | n/a | "Error parsing %r" % orig) |
---|
149 | n/a | |
---|
150 | n/a | def test_roundtrips(self): |
---|
151 | n/a | str_cases = [ |
---|
152 | n/a | ('file:///tmp/junk.txt', |
---|
153 | n/a | ('file', '', '/tmp/junk.txt', '', '', ''), |
---|
154 | n/a | ('file', '', '/tmp/junk.txt', '', '')), |
---|
155 | n/a | ('imap://mail.python.org/mbox1', |
---|
156 | n/a | ('imap', 'mail.python.org', '/mbox1', '', '', ''), |
---|
157 | n/a | ('imap', 'mail.python.org', '/mbox1', '', '')), |
---|
158 | n/a | ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf', |
---|
159 | n/a | ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
---|
160 | n/a | '', '', ''), |
---|
161 | n/a | ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
---|
162 | n/a | '', '')), |
---|
163 | n/a | ('nfs://server/path/to/file.txt', |
---|
164 | n/a | ('nfs', 'server', '/path/to/file.txt', '', '', ''), |
---|
165 | n/a | ('nfs', 'server', '/path/to/file.txt', '', '')), |
---|
166 | n/a | ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/', |
---|
167 | n/a | ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/', |
---|
168 | n/a | '', '', ''), |
---|
169 | n/a | ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/', |
---|
170 | n/a | '', '')), |
---|
171 | n/a | ('git+ssh://git@github.com/user/project.git', |
---|
172 | n/a | ('git+ssh', 'git@github.com','/user/project.git', |
---|
173 | n/a | '','',''), |
---|
174 | n/a | ('git+ssh', 'git@github.com','/user/project.git', |
---|
175 | n/a | '', '')), |
---|
176 | n/a | ] |
---|
177 | n/a | def _encode(t): |
---|
178 | n/a | return (t[0].encode('ascii'), |
---|
179 | n/a | tuple(x.encode('ascii') for x in t[1]), |
---|
180 | n/a | tuple(x.encode('ascii') for x in t[2])) |
---|
181 | n/a | bytes_cases = [_encode(x) for x in str_cases] |
---|
182 | n/a | for url, parsed, split in str_cases + bytes_cases: |
---|
183 | n/a | self.checkRoundtrips(url, parsed, split) |
---|
184 | n/a | |
---|
185 | n/a | def test_http_roundtrips(self): |
---|
186 | n/a | # urllib.parse.urlsplit treats 'http:' as an optimized special case, |
---|
187 | n/a | # so we test both 'http:' and 'https:' in all the following. |
---|
188 | n/a | # Three cheers for white box knowledge! |
---|
189 | n/a | str_cases = [ |
---|
190 | n/a | ('://www.python.org', |
---|
191 | n/a | ('www.python.org', '', '', '', ''), |
---|
192 | n/a | ('www.python.org', '', '', '')), |
---|
193 | n/a | ('://www.python.org#abc', |
---|
194 | n/a | ('www.python.org', '', '', '', 'abc'), |
---|
195 | n/a | ('www.python.org', '', '', 'abc')), |
---|
196 | n/a | ('://www.python.org?q=abc', |
---|
197 | n/a | ('www.python.org', '', '', 'q=abc', ''), |
---|
198 | n/a | ('www.python.org', '', 'q=abc', '')), |
---|
199 | n/a | ('://www.python.org/#abc', |
---|
200 | n/a | ('www.python.org', '/', '', '', 'abc'), |
---|
201 | n/a | ('www.python.org', '/', '', 'abc')), |
---|
202 | n/a | ('://a/b/c/d;p?q#f', |
---|
203 | n/a | ('a', '/b/c/d', 'p', 'q', 'f'), |
---|
204 | n/a | ('a', '/b/c/d;p', 'q', 'f')), |
---|
205 | n/a | ] |
---|
206 | n/a | def _encode(t): |
---|
207 | n/a | return (t[0].encode('ascii'), |
---|
208 | n/a | tuple(x.encode('ascii') for x in t[1]), |
---|
209 | n/a | tuple(x.encode('ascii') for x in t[2])) |
---|
210 | n/a | bytes_cases = [_encode(x) for x in str_cases] |
---|
211 | n/a | str_schemes = ('http', 'https') |
---|
212 | n/a | bytes_schemes = (b'http', b'https') |
---|
213 | n/a | str_tests = str_schemes, str_cases |
---|
214 | n/a | bytes_tests = bytes_schemes, bytes_cases |
---|
215 | n/a | for schemes, test_cases in (str_tests, bytes_tests): |
---|
216 | n/a | for scheme in schemes: |
---|
217 | n/a | for url, parsed, split in test_cases: |
---|
218 | n/a | url = scheme + url |
---|
219 | n/a | parsed = (scheme,) + parsed |
---|
220 | n/a | split = (scheme,) + split |
---|
221 | n/a | self.checkRoundtrips(url, parsed, split) |
---|
222 | n/a | |
---|
223 | n/a | def checkJoin(self, base, relurl, expected): |
---|
224 | n/a | str_components = (base, relurl, expected) |
---|
225 | n/a | self.assertEqual(urllib.parse.urljoin(base, relurl), expected) |
---|
226 | n/a | bytes_components = baseb, relurlb, expectedb = [ |
---|
227 | n/a | x.encode('ascii') for x in str_components] |
---|
228 | n/a | self.assertEqual(urllib.parse.urljoin(baseb, relurlb), expectedb) |
---|
229 | n/a | |
---|
230 | n/a | def test_unparse_parse(self): |
---|
231 | n/a | str_cases = ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',] |
---|
232 | n/a | bytes_cases = [x.encode('ascii') for x in str_cases] |
---|
233 | n/a | for u in str_cases + bytes_cases: |
---|
234 | n/a | self.assertEqual(urllib.parse.urlunsplit(urllib.parse.urlsplit(u)), u) |
---|
235 | n/a | self.assertEqual(urllib.parse.urlunparse(urllib.parse.urlparse(u)), u) |
---|
236 | n/a | |
---|
237 | n/a | def test_RFC1808(self): |
---|
238 | n/a | # "normal" cases from RFC 1808: |
---|
239 | n/a | self.checkJoin(RFC1808_BASE, 'g:h', 'g:h') |
---|
240 | n/a | self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g') |
---|
241 | n/a | self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g') |
---|
242 | n/a | self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/') |
---|
243 | n/a | self.checkJoin(RFC1808_BASE, '/g', 'http://a/g') |
---|
244 | n/a | self.checkJoin(RFC1808_BASE, '//g', 'http://g') |
---|
245 | n/a | self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y') |
---|
246 | n/a | self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
---|
247 | n/a | self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s') |
---|
248 | n/a | self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s') |
---|
249 | n/a | self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
---|
250 | n/a | self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
---|
251 | n/a | self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x') |
---|
252 | n/a | self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
---|
253 | n/a | self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/') |
---|
254 | n/a | self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/') |
---|
255 | n/a | self.checkJoin(RFC1808_BASE, '..', 'http://a/b/') |
---|
256 | n/a | self.checkJoin(RFC1808_BASE, '../', 'http://a/b/') |
---|
257 | n/a | self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g') |
---|
258 | n/a | self.checkJoin(RFC1808_BASE, '../..', 'http://a/') |
---|
259 | n/a | self.checkJoin(RFC1808_BASE, '../../', 'http://a/') |
---|
260 | n/a | self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g') |
---|
261 | n/a | |
---|
262 | n/a | # "abnormal" cases from RFC 1808: |
---|
263 | n/a | self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f') |
---|
264 | n/a | self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.') |
---|
265 | n/a | self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g') |
---|
266 | n/a | self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..') |
---|
267 | n/a | self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g') |
---|
268 | n/a | self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g') |
---|
269 | n/a | self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/') |
---|
270 | n/a | self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h') |
---|
271 | n/a | self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h') |
---|
272 | n/a | |
---|
273 | n/a | # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808), |
---|
274 | n/a | # so we'll not actually run these tests (which expect 1808 behavior). |
---|
275 | n/a | #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g') |
---|
276 | n/a | #self.checkJoin(RFC1808_BASE, 'http:', 'http:') |
---|
277 | n/a | |
---|
278 | n/a | # XXX: The following tests are no longer compatible with RFC3986 |
---|
279 | n/a | # self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g') |
---|
280 | n/a | # self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g') |
---|
281 | n/a | # self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g') |
---|
282 | n/a | # self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g') |
---|
283 | n/a | |
---|
284 | n/a | |
---|
285 | n/a | def test_RFC2368(self): |
---|
286 | n/a | # Issue 11467: path that starts with a number is not parsed correctly |
---|
287 | n/a | self.assertEqual(urllib.parse.urlparse('mailto:1337@example.org'), |
---|
288 | n/a | ('mailto', '', '1337@example.org', '', '', '')) |
---|
289 | n/a | |
---|
290 | n/a | def test_RFC2396(self): |
---|
291 | n/a | # cases from RFC 2396 |
---|
292 | n/a | |
---|
293 | n/a | |
---|
294 | n/a | self.checkJoin(RFC2396_BASE, 'g:h', 'g:h') |
---|
295 | n/a | self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g') |
---|
296 | n/a | self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g') |
---|
297 | n/a | self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/') |
---|
298 | n/a | self.checkJoin(RFC2396_BASE, '/g', 'http://a/g') |
---|
299 | n/a | self.checkJoin(RFC2396_BASE, '//g', 'http://g') |
---|
300 | n/a | self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y') |
---|
301 | n/a | self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s') |
---|
302 | n/a | self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s') |
---|
303 | n/a | self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
---|
304 | n/a | self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x') |
---|
305 | n/a | self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
---|
306 | n/a | self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/') |
---|
307 | n/a | self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/') |
---|
308 | n/a | self.checkJoin(RFC2396_BASE, '..', 'http://a/b/') |
---|
309 | n/a | self.checkJoin(RFC2396_BASE, '../', 'http://a/b/') |
---|
310 | n/a | self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g') |
---|
311 | n/a | self.checkJoin(RFC2396_BASE, '../..', 'http://a/') |
---|
312 | n/a | self.checkJoin(RFC2396_BASE, '../../', 'http://a/') |
---|
313 | n/a | self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g') |
---|
314 | n/a | self.checkJoin(RFC2396_BASE, '', RFC2396_BASE) |
---|
315 | n/a | self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.') |
---|
316 | n/a | self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g') |
---|
317 | n/a | self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..') |
---|
318 | n/a | self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g') |
---|
319 | n/a | self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g') |
---|
320 | n/a | self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/') |
---|
321 | n/a | self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h') |
---|
322 | n/a | self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h') |
---|
323 | n/a | self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y') |
---|
324 | n/a | self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y') |
---|
325 | n/a | self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
---|
326 | n/a | self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x') |
---|
327 | n/a | self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
---|
328 | n/a | self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x') |
---|
329 | n/a | |
---|
330 | n/a | # XXX: The following tests are no longer compatible with RFC3986 |
---|
331 | n/a | # self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g') |
---|
332 | n/a | # self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g') |
---|
333 | n/a | # self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g') |
---|
334 | n/a | # self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g') |
---|
335 | n/a | |
---|
336 | n/a | |
---|
337 | n/a | def test_RFC3986(self): |
---|
338 | n/a | # Test cases from RFC3986 |
---|
339 | n/a | self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y') |
---|
340 | n/a | self.checkJoin(RFC3986_BASE, ';x', 'http://a/b/c/;x') |
---|
341 | n/a | self.checkJoin(RFC3986_BASE, 'g:h','g:h') |
---|
342 | n/a | self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g') |
---|
343 | n/a | self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g') |
---|
344 | n/a | self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/') |
---|
345 | n/a | self.checkJoin(RFC3986_BASE, '/g','http://a/g') |
---|
346 | n/a | self.checkJoin(RFC3986_BASE, '//g','http://g') |
---|
347 | n/a | self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y') |
---|
348 | n/a | self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y') |
---|
349 | n/a | self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s') |
---|
350 | n/a | self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s') |
---|
351 | n/a | self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s') |
---|
352 | n/a | self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x') |
---|
353 | n/a | self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x') |
---|
354 | n/a | self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s') |
---|
355 | n/a | self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q') |
---|
356 | n/a | self.checkJoin(RFC3986_BASE, '.','http://a/b/c/') |
---|
357 | n/a | self.checkJoin(RFC3986_BASE, './','http://a/b/c/') |
---|
358 | n/a | self.checkJoin(RFC3986_BASE, '..','http://a/b/') |
---|
359 | n/a | self.checkJoin(RFC3986_BASE, '../','http://a/b/') |
---|
360 | n/a | self.checkJoin(RFC3986_BASE, '../g','http://a/b/g') |
---|
361 | n/a | self.checkJoin(RFC3986_BASE, '../..','http://a/') |
---|
362 | n/a | self.checkJoin(RFC3986_BASE, '../../','http://a/') |
---|
363 | n/a | self.checkJoin(RFC3986_BASE, '../../g','http://a/g') |
---|
364 | n/a | self.checkJoin(RFC3986_BASE, '../../../g', 'http://a/g') |
---|
365 | n/a | |
---|
366 | n/a | #Abnormal Examples |
---|
367 | n/a | |
---|
368 | n/a | # The 'abnormal scenarios' are incompatible with RFC2986 parsing |
---|
369 | n/a | # Tests are here for reference. |
---|
370 | n/a | |
---|
371 | n/a | self.checkJoin(RFC3986_BASE, '../../../g','http://a/g') |
---|
372 | n/a | self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g') |
---|
373 | n/a | self.checkJoin(RFC3986_BASE, '/./g','http://a/g') |
---|
374 | n/a | self.checkJoin(RFC3986_BASE, '/../g','http://a/g') |
---|
375 | n/a | self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.') |
---|
376 | n/a | self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g') |
---|
377 | n/a | self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..') |
---|
378 | n/a | self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g') |
---|
379 | n/a | self.checkJoin(RFC3986_BASE, './../g','http://a/b/g') |
---|
380 | n/a | self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/') |
---|
381 | n/a | self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h') |
---|
382 | n/a | self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h') |
---|
383 | n/a | self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y') |
---|
384 | n/a | self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y') |
---|
385 | n/a | self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x') |
---|
386 | n/a | self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x') |
---|
387 | n/a | self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x') |
---|
388 | n/a | self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x') |
---|
389 | n/a | #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser |
---|
390 | n/a | self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') #relaxed parser |
---|
391 | n/a | |
---|
392 | n/a | # Test for issue9721 |
---|
393 | n/a | self.checkJoin('http://a/b/c/de', ';x','http://a/b/c/;x') |
---|
394 | n/a | |
---|
395 | n/a | def test_urljoins(self): |
---|
396 | n/a | self.checkJoin(SIMPLE_BASE, 'g:h','g:h') |
---|
397 | n/a | self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g') |
---|
398 | n/a | self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d') |
---|
399 | n/a | self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g') |
---|
400 | n/a | self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g') |
---|
401 | n/a | self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/') |
---|
402 | n/a | self.checkJoin(SIMPLE_BASE, '/g','http://a/g') |
---|
403 | n/a | self.checkJoin(SIMPLE_BASE, '//g','http://g') |
---|
404 | n/a | self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y') |
---|
405 | n/a | self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y') |
---|
406 | n/a | self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x') |
---|
407 | n/a | self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/') |
---|
408 | n/a | self.checkJoin(SIMPLE_BASE, './','http://a/b/c/') |
---|
409 | n/a | self.checkJoin(SIMPLE_BASE, '..','http://a/b/') |
---|
410 | n/a | self.checkJoin(SIMPLE_BASE, '../','http://a/b/') |
---|
411 | n/a | self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g') |
---|
412 | n/a | self.checkJoin(SIMPLE_BASE, '../..','http://a/') |
---|
413 | n/a | self.checkJoin(SIMPLE_BASE, '../../g','http://a/g') |
---|
414 | n/a | self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g') |
---|
415 | n/a | self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/') |
---|
416 | n/a | self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h') |
---|
417 | n/a | self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h') |
---|
418 | n/a | self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g') |
---|
419 | n/a | self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d') |
---|
420 | n/a | self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y') |
---|
421 | n/a | self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y') |
---|
422 | n/a | self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x') |
---|
423 | n/a | self.checkJoin('http:///', '..','http:///') |
---|
424 | n/a | self.checkJoin('', 'http://a/b/c/g?y/./x','http://a/b/c/g?y/./x') |
---|
425 | n/a | self.checkJoin('', 'http://a/./g', 'http://a/./g') |
---|
426 | n/a | self.checkJoin('svn://pathtorepo/dir1', 'dir2', 'svn://pathtorepo/dir2') |
---|
427 | n/a | self.checkJoin('svn+ssh://pathtorepo/dir1', 'dir2', 'svn+ssh://pathtorepo/dir2') |
---|
428 | n/a | self.checkJoin('ws://a/b','g','ws://a/g') |
---|
429 | n/a | self.checkJoin('wss://a/b','g','wss://a/g') |
---|
430 | n/a | |
---|
431 | n/a | # XXX: The following tests are no longer compatible with RFC3986 |
---|
432 | n/a | # self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g') |
---|
433 | n/a | # self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g') |
---|
434 | n/a | |
---|
435 | n/a | # test for issue22118 duplicate slashes |
---|
436 | n/a | self.checkJoin(SIMPLE_BASE + '/', 'foo', SIMPLE_BASE + '/foo') |
---|
437 | n/a | |
---|
438 | n/a | # Non-RFC-defined tests, covering variations of base and trailing |
---|
439 | n/a | # slashes |
---|
440 | n/a | self.checkJoin('http://a/b/c/d/e/', '../../f/g/', 'http://a/b/c/f/g/') |
---|
441 | n/a | self.checkJoin('http://a/b/c/d/e', '../../f/g/', 'http://a/b/f/g/') |
---|
442 | n/a | self.checkJoin('http://a/b/c/d/e/', '/../../f/g/', 'http://a/f/g/') |
---|
443 | n/a | self.checkJoin('http://a/b/c/d/e', '/../../f/g/', 'http://a/f/g/') |
---|
444 | n/a | self.checkJoin('http://a/b/c/d/e/', '../../f/g', 'http://a/b/c/f/g') |
---|
445 | n/a | self.checkJoin('http://a/b/', '../../f/g/', 'http://a/f/g/') |
---|
446 | n/a | |
---|
447 | n/a | # issue 23703: don't duplicate filename |
---|
448 | n/a | self.checkJoin('a', 'b', 'b') |
---|
449 | n/a | |
---|
450 | n/a | def test_RFC2732(self): |
---|
451 | n/a | str_cases = [ |
---|
452 | n/a | ('http://Test.python.org:5432/foo/', 'test.python.org', 5432), |
---|
453 | n/a | ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432), |
---|
454 | n/a | ('http://[::1]:5432/foo/', '::1', 5432), |
---|
455 | n/a | ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432), |
---|
456 | n/a | ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432), |
---|
457 | n/a | ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/', |
---|
458 | n/a | 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432), |
---|
459 | n/a | ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432), |
---|
460 | n/a | ('http://[::ffff:12.34.56.78]:5432/foo/', |
---|
461 | n/a | '::ffff:12.34.56.78', 5432), |
---|
462 | n/a | ('http://Test.python.org/foo/', 'test.python.org', None), |
---|
463 | n/a | ('http://12.34.56.78/foo/', '12.34.56.78', None), |
---|
464 | n/a | ('http://[::1]/foo/', '::1', None), |
---|
465 | n/a | ('http://[dead:beef::1]/foo/', 'dead:beef::1', None), |
---|
466 | n/a | ('http://[dead:beef::]/foo/', 'dead:beef::', None), |
---|
467 | n/a | ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/', |
---|
468 | n/a | 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None), |
---|
469 | n/a | ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None), |
---|
470 | n/a | ('http://[::ffff:12.34.56.78]/foo/', |
---|
471 | n/a | '::ffff:12.34.56.78', None), |
---|
472 | n/a | ('http://Test.python.org:/foo/', 'test.python.org', None), |
---|
473 | n/a | ('http://12.34.56.78:/foo/', '12.34.56.78', None), |
---|
474 | n/a | ('http://[::1]:/foo/', '::1', None), |
---|
475 | n/a | ('http://[dead:beef::1]:/foo/', 'dead:beef::1', None), |
---|
476 | n/a | ('http://[dead:beef::]:/foo/', 'dead:beef::', None), |
---|
477 | n/a | ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:/foo/', |
---|
478 | n/a | 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None), |
---|
479 | n/a | ('http://[::12.34.56.78]:/foo/', '::12.34.56.78', None), |
---|
480 | n/a | ('http://[::ffff:12.34.56.78]:/foo/', |
---|
481 | n/a | '::ffff:12.34.56.78', None), |
---|
482 | n/a | ] |
---|
483 | n/a | def _encode(t): |
---|
484 | n/a | return t[0].encode('ascii'), t[1].encode('ascii'), t[2] |
---|
485 | n/a | bytes_cases = [_encode(x) for x in str_cases] |
---|
486 | n/a | for url, hostname, port in str_cases + bytes_cases: |
---|
487 | n/a | urlparsed = urllib.parse.urlparse(url) |
---|
488 | n/a | self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port)) |
---|
489 | n/a | |
---|
490 | n/a | str_cases = [ |
---|
491 | n/a | 'http://::12.34.56.78]/', |
---|
492 | n/a | 'http://[::1/foo/', |
---|
493 | n/a | 'ftp://[::1/foo/bad]/bad', |
---|
494 | n/a | 'http://[::1/foo/bad]/bad', |
---|
495 | n/a | 'http://[::ffff:12.34.56.78'] |
---|
496 | n/a | bytes_cases = [x.encode('ascii') for x in str_cases] |
---|
497 | n/a | for invalid_url in str_cases + bytes_cases: |
---|
498 | n/a | self.assertRaises(ValueError, urllib.parse.urlparse, invalid_url) |
---|
499 | n/a | |
---|
500 | n/a | def test_urldefrag(self): |
---|
501 | n/a | str_cases = [ |
---|
502 | n/a | ('http://python.org#frag', 'http://python.org', 'frag'), |
---|
503 | n/a | ('http://python.org', 'http://python.org', ''), |
---|
504 | n/a | ('http://python.org/#frag', 'http://python.org/', 'frag'), |
---|
505 | n/a | ('http://python.org/', 'http://python.org/', ''), |
---|
506 | n/a | ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'), |
---|
507 | n/a | ('http://python.org/?q', 'http://python.org/?q', ''), |
---|
508 | n/a | ('http://python.org/p#frag', 'http://python.org/p', 'frag'), |
---|
509 | n/a | ('http://python.org/p?q', 'http://python.org/p?q', ''), |
---|
510 | n/a | (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'), |
---|
511 | n/a | (RFC2396_BASE, 'http://a/b/c/d;p?q', ''), |
---|
512 | n/a | ] |
---|
513 | n/a | def _encode(t): |
---|
514 | n/a | return type(t)(x.encode('ascii') for x in t) |
---|
515 | n/a | bytes_cases = [_encode(x) for x in str_cases] |
---|
516 | n/a | for url, defrag, frag in str_cases + bytes_cases: |
---|
517 | n/a | result = urllib.parse.urldefrag(url) |
---|
518 | n/a | self.assertEqual(result.geturl(), url) |
---|
519 | n/a | self.assertEqual(result, (defrag, frag)) |
---|
520 | n/a | self.assertEqual(result.url, defrag) |
---|
521 | n/a | self.assertEqual(result.fragment, frag) |
---|
522 | n/a | |
---|
523 | n/a | def test_urlsplit_attributes(self): |
---|
524 | n/a | url = "HTTP://WWW.PYTHON.ORG/doc/#frag" |
---|
525 | n/a | p = urllib.parse.urlsplit(url) |
---|
526 | n/a | self.assertEqual(p.scheme, "http") |
---|
527 | n/a | self.assertEqual(p.netloc, "WWW.PYTHON.ORG") |
---|
528 | n/a | self.assertEqual(p.path, "/doc/") |
---|
529 | n/a | self.assertEqual(p.query, "") |
---|
530 | n/a | self.assertEqual(p.fragment, "frag") |
---|
531 | n/a | self.assertEqual(p.username, None) |
---|
532 | n/a | self.assertEqual(p.password, None) |
---|
533 | n/a | self.assertEqual(p.hostname, "www.python.org") |
---|
534 | n/a | self.assertEqual(p.port, None) |
---|
535 | n/a | # geturl() won't return exactly the original URL in this case |
---|
536 | n/a | # since the scheme is always case-normalized |
---|
537 | n/a | # We handle this by ignoring the first 4 characters of the URL |
---|
538 | n/a | self.assertEqual(p.geturl()[4:], url[4:]) |
---|
539 | n/a | |
---|
540 | n/a | url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag" |
---|
541 | n/a | p = urllib.parse.urlsplit(url) |
---|
542 | n/a | self.assertEqual(p.scheme, "http") |
---|
543 | n/a | self.assertEqual(p.netloc, "User:Pass@www.python.org:080") |
---|
544 | n/a | self.assertEqual(p.path, "/doc/") |
---|
545 | n/a | self.assertEqual(p.query, "query=yes") |
---|
546 | n/a | self.assertEqual(p.fragment, "frag") |
---|
547 | n/a | self.assertEqual(p.username, "User") |
---|
548 | n/a | self.assertEqual(p.password, "Pass") |
---|
549 | n/a | self.assertEqual(p.hostname, "www.python.org") |
---|
550 | n/a | self.assertEqual(p.port, 80) |
---|
551 | n/a | self.assertEqual(p.geturl(), url) |
---|
552 | n/a | |
---|
553 | n/a | # Addressing issue1698, which suggests Username can contain |
---|
554 | n/a | # "@" characters. Though not RFC compliant, many ftp sites allow |
---|
555 | n/a | # and request email addresses as usernames. |
---|
556 | n/a | |
---|
557 | n/a | url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag" |
---|
558 | n/a | p = urllib.parse.urlsplit(url) |
---|
559 | n/a | self.assertEqual(p.scheme, "http") |
---|
560 | n/a | self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080") |
---|
561 | n/a | self.assertEqual(p.path, "/doc/") |
---|
562 | n/a | self.assertEqual(p.query, "query=yes") |
---|
563 | n/a | self.assertEqual(p.fragment, "frag") |
---|
564 | n/a | self.assertEqual(p.username, "User@example.com") |
---|
565 | n/a | self.assertEqual(p.password, "Pass") |
---|
566 | n/a | self.assertEqual(p.hostname, "www.python.org") |
---|
567 | n/a | self.assertEqual(p.port, 80) |
---|
568 | n/a | self.assertEqual(p.geturl(), url) |
---|
569 | n/a | |
---|
570 | n/a | # And check them all again, only with bytes this time |
---|
571 | n/a | url = b"HTTP://WWW.PYTHON.ORG/doc/#frag" |
---|
572 | n/a | p = urllib.parse.urlsplit(url) |
---|
573 | n/a | self.assertEqual(p.scheme, b"http") |
---|
574 | n/a | self.assertEqual(p.netloc, b"WWW.PYTHON.ORG") |
---|
575 | n/a | self.assertEqual(p.path, b"/doc/") |
---|
576 | n/a | self.assertEqual(p.query, b"") |
---|
577 | n/a | self.assertEqual(p.fragment, b"frag") |
---|
578 | n/a | self.assertEqual(p.username, None) |
---|
579 | n/a | self.assertEqual(p.password, None) |
---|
580 | n/a | self.assertEqual(p.hostname, b"www.python.org") |
---|
581 | n/a | self.assertEqual(p.port, None) |
---|
582 | n/a | self.assertEqual(p.geturl()[4:], url[4:]) |
---|
583 | n/a | |
---|
584 | n/a | url = b"http://User:Pass@www.python.org:080/doc/?query=yes#frag" |
---|
585 | n/a | p = urllib.parse.urlsplit(url) |
---|
586 | n/a | self.assertEqual(p.scheme, b"http") |
---|
587 | n/a | self.assertEqual(p.netloc, b"User:Pass@www.python.org:080") |
---|
588 | n/a | self.assertEqual(p.path, b"/doc/") |
---|
589 | n/a | self.assertEqual(p.query, b"query=yes") |
---|
590 | n/a | self.assertEqual(p.fragment, b"frag") |
---|
591 | n/a | self.assertEqual(p.username, b"User") |
---|
592 | n/a | self.assertEqual(p.password, b"Pass") |
---|
593 | n/a | self.assertEqual(p.hostname, b"www.python.org") |
---|
594 | n/a | self.assertEqual(p.port, 80) |
---|
595 | n/a | self.assertEqual(p.geturl(), url) |
---|
596 | n/a | |
---|
597 | n/a | url = b"http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag" |
---|
598 | n/a | p = urllib.parse.urlsplit(url) |
---|
599 | n/a | self.assertEqual(p.scheme, b"http") |
---|
600 | n/a | self.assertEqual(p.netloc, b"User@example.com:Pass@www.python.org:080") |
---|
601 | n/a | self.assertEqual(p.path, b"/doc/") |
---|
602 | n/a | self.assertEqual(p.query, b"query=yes") |
---|
603 | n/a | self.assertEqual(p.fragment, b"frag") |
---|
604 | n/a | self.assertEqual(p.username, b"User@example.com") |
---|
605 | n/a | self.assertEqual(p.password, b"Pass") |
---|
606 | n/a | self.assertEqual(p.hostname, b"www.python.org") |
---|
607 | n/a | self.assertEqual(p.port, 80) |
---|
608 | n/a | self.assertEqual(p.geturl(), url) |
---|
609 | n/a | |
---|
610 | n/a | # Verify an illegal port raises ValueError |
---|
611 | n/a | url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag" |
---|
612 | n/a | p = urllib.parse.urlsplit(url) |
---|
613 | n/a | with self.assertRaisesRegex(ValueError, "out of range"): |
---|
614 | n/a | p.port |
---|
615 | n/a | |
---|
616 | n/a | def test_attributes_bad_port(self): |
---|
617 | n/a | """Check handling of invalid ports.""" |
---|
618 | n/a | for bytes in (False, True): |
---|
619 | n/a | for parse in (urllib.parse.urlsplit, urllib.parse.urlparse): |
---|
620 | n/a | for port in ("foo", "1.5", "-1", "0x10"): |
---|
621 | n/a | with self.subTest(bytes=bytes, parse=parse, port=port): |
---|
622 | n/a | netloc = "www.example.net:" + port |
---|
623 | n/a | url = "http://" + netloc |
---|
624 | n/a | if bytes: |
---|
625 | n/a | netloc = netloc.encode("ascii") |
---|
626 | n/a | url = url.encode("ascii") |
---|
627 | n/a | p = parse(url) |
---|
628 | n/a | self.assertEqual(p.netloc, netloc) |
---|
629 | n/a | with self.assertRaises(ValueError): |
---|
630 | n/a | p.port |
---|
631 | n/a | |
---|
632 | n/a | def test_attributes_without_netloc(self): |
---|
633 | n/a | # This example is straight from RFC 3261. It looks like it |
---|
634 | n/a | # should allow the username, hostname, and port to be filled |
---|
635 | n/a | # in, but doesn't. Since it's a URI and doesn't use the |
---|
636 | n/a | # scheme://netloc syntax, the netloc and related attributes |
---|
637 | n/a | # should be left empty. |
---|
638 | n/a | uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15" |
---|
639 | n/a | p = urllib.parse.urlsplit(uri) |
---|
640 | n/a | self.assertEqual(p.netloc, "") |
---|
641 | n/a | self.assertEqual(p.username, None) |
---|
642 | n/a | self.assertEqual(p.password, None) |
---|
643 | n/a | self.assertEqual(p.hostname, None) |
---|
644 | n/a | self.assertEqual(p.port, None) |
---|
645 | n/a | self.assertEqual(p.geturl(), uri) |
---|
646 | n/a | |
---|
647 | n/a | p = urllib.parse.urlparse(uri) |
---|
648 | n/a | self.assertEqual(p.netloc, "") |
---|
649 | n/a | self.assertEqual(p.username, None) |
---|
650 | n/a | self.assertEqual(p.password, None) |
---|
651 | n/a | self.assertEqual(p.hostname, None) |
---|
652 | n/a | self.assertEqual(p.port, None) |
---|
653 | n/a | self.assertEqual(p.geturl(), uri) |
---|
654 | n/a | |
---|
655 | n/a | # You guessed it, repeating the test with bytes input |
---|
656 | n/a | uri = b"sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15" |
---|
657 | n/a | p = urllib.parse.urlsplit(uri) |
---|
658 | n/a | self.assertEqual(p.netloc, b"") |
---|
659 | n/a | self.assertEqual(p.username, None) |
---|
660 | n/a | self.assertEqual(p.password, None) |
---|
661 | n/a | self.assertEqual(p.hostname, None) |
---|
662 | n/a | self.assertEqual(p.port, None) |
---|
663 | n/a | self.assertEqual(p.geturl(), uri) |
---|
664 | n/a | |
---|
665 | n/a | p = urllib.parse.urlparse(uri) |
---|
666 | n/a | self.assertEqual(p.netloc, b"") |
---|
667 | n/a | self.assertEqual(p.username, None) |
---|
668 | n/a | self.assertEqual(p.password, None) |
---|
669 | n/a | self.assertEqual(p.hostname, None) |
---|
670 | n/a | self.assertEqual(p.port, None) |
---|
671 | n/a | self.assertEqual(p.geturl(), uri) |
---|
672 | n/a | |
---|
673 | n/a | def test_noslash(self): |
---|
674 | n/a | # Issue 1637: http://foo.com?query is legal |
---|
675 | n/a | self.assertEqual(urllib.parse.urlparse("http://example.com?blahblah=/foo"), |
---|
676 | n/a | ('http', 'example.com', '', '', 'blahblah=/foo', '')) |
---|
677 | n/a | self.assertEqual(urllib.parse.urlparse(b"http://example.com?blahblah=/foo"), |
---|
678 | n/a | (b'http', b'example.com', b'', b'', b'blahblah=/foo', b'')) |
---|
679 | n/a | |
---|
680 | n/a | def test_withoutscheme(self): |
---|
681 | n/a | # Test urlparse without scheme |
---|
682 | n/a | # Issue 754016: urlparse goes wrong with IP:port without scheme |
---|
683 | n/a | # RFC 1808 specifies that netloc should start with //, urlparse expects |
---|
684 | n/a | # the same, otherwise it classifies the portion of url as path. |
---|
685 | n/a | self.assertEqual(urllib.parse.urlparse("path"), |
---|
686 | n/a | ('','','path','','','')) |
---|
687 | n/a | self.assertEqual(urllib.parse.urlparse("//www.python.org:80"), |
---|
688 | n/a | ('','www.python.org:80','','','','')) |
---|
689 | n/a | self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"), |
---|
690 | n/a | ('http','www.python.org:80','','','','')) |
---|
691 | n/a | # Repeat for bytes input |
---|
692 | n/a | self.assertEqual(urllib.parse.urlparse(b"path"), |
---|
693 | n/a | (b'',b'',b'path',b'',b'',b'')) |
---|
694 | n/a | self.assertEqual(urllib.parse.urlparse(b"//www.python.org:80"), |
---|
695 | n/a | (b'',b'www.python.org:80',b'',b'',b'',b'')) |
---|
696 | n/a | self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"), |
---|
697 | n/a | (b'http',b'www.python.org:80',b'',b'',b'',b'')) |
---|
698 | n/a | |
---|
699 | n/a | def test_portseparator(self): |
---|
700 | n/a | # Issue 754016 makes changes for port separator ':' from scheme separator |
---|
701 | n/a | self.assertEqual(urllib.parse.urlparse("path:80"), |
---|
702 | n/a | ('','','path:80','','','')) |
---|
703 | n/a | self.assertEqual(urllib.parse.urlparse("http:"),('http','','','','','')) |
---|
704 | n/a | self.assertEqual(urllib.parse.urlparse("https:"),('https','','','','','')) |
---|
705 | n/a | self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"), |
---|
706 | n/a | ('http','www.python.org:80','','','','')) |
---|
707 | n/a | # As usual, need to check bytes input as well |
---|
708 | n/a | self.assertEqual(urllib.parse.urlparse(b"path:80"), |
---|
709 | n/a | (b'',b'',b'path:80',b'',b'',b'')) |
---|
710 | n/a | self.assertEqual(urllib.parse.urlparse(b"http:"),(b'http',b'',b'',b'',b'',b'')) |
---|
711 | n/a | self.assertEqual(urllib.parse.urlparse(b"https:"),(b'https',b'',b'',b'',b'',b'')) |
---|
712 | n/a | self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"), |
---|
713 | n/a | (b'http',b'www.python.org:80',b'',b'',b'',b'')) |
---|
714 | n/a | |
---|
715 | n/a | def test_usingsys(self): |
---|
716 | n/a | # Issue 3314: sys module is used in the error |
---|
717 | n/a | self.assertRaises(TypeError, urllib.parse.urlencode, "foo") |
---|
718 | n/a | |
---|
719 | n/a | def test_anyscheme(self): |
---|
720 | n/a | # Issue 7904: s3://foo.com/stuff has netloc "foo.com". |
---|
721 | n/a | self.assertEqual(urllib.parse.urlparse("s3://foo.com/stuff"), |
---|
722 | n/a | ('s3', 'foo.com', '/stuff', '', '', '')) |
---|
723 | n/a | self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff"), |
---|
724 | n/a | ('x-newscheme', 'foo.com', '/stuff', '', '', '')) |
---|
725 | n/a | self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query#fragment"), |
---|
726 | n/a | ('x-newscheme', 'foo.com', '/stuff', '', 'query', 'fragment')) |
---|
727 | n/a | self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query"), |
---|
728 | n/a | ('x-newscheme', 'foo.com', '/stuff', '', 'query', '')) |
---|
729 | n/a | |
---|
730 | n/a | # And for bytes... |
---|
731 | n/a | self.assertEqual(urllib.parse.urlparse(b"s3://foo.com/stuff"), |
---|
732 | n/a | (b's3', b'foo.com', b'/stuff', b'', b'', b'')) |
---|
733 | n/a | self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff"), |
---|
734 | n/a | (b'x-newscheme', b'foo.com', b'/stuff', b'', b'', b'')) |
---|
735 | n/a | self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query#fragment"), |
---|
736 | n/a | (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b'fragment')) |
---|
737 | n/a | self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query"), |
---|
738 | n/a | (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b'')) |
---|
739 | n/a | |
---|
740 | n/a | def test_default_scheme(self): |
---|
741 | n/a | # Exercise the scheme parameter of urlparse() and urlsplit() |
---|
742 | n/a | for func in (urllib.parse.urlparse, urllib.parse.urlsplit): |
---|
743 | n/a | with self.subTest(function=func): |
---|
744 | n/a | result = func("http://example.net/", "ftp") |
---|
745 | n/a | self.assertEqual(result.scheme, "http") |
---|
746 | n/a | result = func(b"http://example.net/", b"ftp") |
---|
747 | n/a | self.assertEqual(result.scheme, b"http") |
---|
748 | n/a | self.assertEqual(func("path", "ftp").scheme, "ftp") |
---|
749 | n/a | self.assertEqual(func("path", scheme="ftp").scheme, "ftp") |
---|
750 | n/a | self.assertEqual(func(b"path", scheme=b"ftp").scheme, b"ftp") |
---|
751 | n/a | self.assertEqual(func("path").scheme, "") |
---|
752 | n/a | self.assertEqual(func(b"path").scheme, b"") |
---|
753 | n/a | self.assertEqual(func(b"path", "").scheme, b"") |
---|
754 | n/a | |
---|
755 | n/a | def test_parse_fragments(self): |
---|
756 | n/a | # Exercise the allow_fragments parameter of urlparse() and urlsplit() |
---|
757 | n/a | tests = ( |
---|
758 | n/a | ("http:#frag", "path"), |
---|
759 | n/a | ("//example.net#frag", "path"), |
---|
760 | n/a | ("index.html#frag", "path"), |
---|
761 | n/a | (";a=b#frag", "params"), |
---|
762 | n/a | ("?a=b#frag", "query"), |
---|
763 | n/a | ("#frag", "path"), |
---|
764 | n/a | ) |
---|
765 | n/a | for url, attr in tests: |
---|
766 | n/a | for func in (urllib.parse.urlparse, urllib.parse.urlsplit): |
---|
767 | n/a | if attr == "params" and func is urllib.parse.urlsplit: |
---|
768 | n/a | attr = "path" |
---|
769 | n/a | with self.subTest(url=url, function=func): |
---|
770 | n/a | result = func(url, allow_fragments=False) |
---|
771 | n/a | self.assertEqual(result.fragment, "") |
---|
772 | n/a | self.assertTrue(getattr(result, attr).endswith("#frag")) |
---|
773 | n/a | self.assertEqual(func(url, "", False).fragment, "") |
---|
774 | n/a | |
---|
775 | n/a | result = func(url, allow_fragments=True) |
---|
776 | n/a | self.assertEqual(result.fragment, "frag") |
---|
777 | n/a | self.assertFalse(getattr(result, attr).endswith("frag")) |
---|
778 | n/a | self.assertEqual(func(url, "", True).fragment, "frag") |
---|
779 | n/a | self.assertEqual(func(url).fragment, "frag") |
---|
780 | n/a | |
---|
781 | n/a | def test_mixed_types_rejected(self): |
---|
782 | n/a | # Several functions that process either strings or ASCII encoded bytes |
---|
783 | n/a | # accept multiple arguments. Check they reject mixed type input |
---|
784 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
785 | n/a | urllib.parse.urlparse("www.python.org", b"http") |
---|
786 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
787 | n/a | urllib.parse.urlparse(b"www.python.org", "http") |
---|
788 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
789 | n/a | urllib.parse.urlsplit("www.python.org", b"http") |
---|
790 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
791 | n/a | urllib.parse.urlsplit(b"www.python.org", "http") |
---|
792 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
793 | n/a | urllib.parse.urlunparse(( b"http", "www.python.org","","","","")) |
---|
794 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
795 | n/a | urllib.parse.urlunparse(("http", b"www.python.org","","","","")) |
---|
796 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
797 | n/a | urllib.parse.urlunsplit((b"http", "www.python.org","","","")) |
---|
798 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
799 | n/a | urllib.parse.urlunsplit(("http", b"www.python.org","","","")) |
---|
800 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
801 | n/a | urllib.parse.urljoin("http://python.org", b"http://python.org") |
---|
802 | n/a | with self.assertRaisesRegex(TypeError, "Cannot mix str"): |
---|
803 | n/a | urllib.parse.urljoin(b"http://python.org", "http://python.org") |
---|
804 | n/a | |
---|
805 | n/a | def _check_result_type(self, str_type): |
---|
806 | n/a | num_args = len(str_type._fields) |
---|
807 | n/a | bytes_type = str_type._encoded_counterpart |
---|
808 | n/a | self.assertIs(bytes_type._decoded_counterpart, str_type) |
---|
809 | n/a | str_args = ('',) * num_args |
---|
810 | n/a | bytes_args = (b'',) * num_args |
---|
811 | n/a | str_result = str_type(*str_args) |
---|
812 | n/a | bytes_result = bytes_type(*bytes_args) |
---|
813 | n/a | encoding = 'ascii' |
---|
814 | n/a | errors = 'strict' |
---|
815 | n/a | self.assertEqual(str_result, str_args) |
---|
816 | n/a | self.assertEqual(bytes_result.decode(), str_args) |
---|
817 | n/a | self.assertEqual(bytes_result.decode(), str_result) |
---|
818 | n/a | self.assertEqual(bytes_result.decode(encoding), str_args) |
---|
819 | n/a | self.assertEqual(bytes_result.decode(encoding), str_result) |
---|
820 | n/a | self.assertEqual(bytes_result.decode(encoding, errors), str_args) |
---|
821 | n/a | self.assertEqual(bytes_result.decode(encoding, errors), str_result) |
---|
822 | n/a | self.assertEqual(bytes_result, bytes_args) |
---|
823 | n/a | self.assertEqual(str_result.encode(), bytes_args) |
---|
824 | n/a | self.assertEqual(str_result.encode(), bytes_result) |
---|
825 | n/a | self.assertEqual(str_result.encode(encoding), bytes_args) |
---|
826 | n/a | self.assertEqual(str_result.encode(encoding), bytes_result) |
---|
827 | n/a | self.assertEqual(str_result.encode(encoding, errors), bytes_args) |
---|
828 | n/a | self.assertEqual(str_result.encode(encoding, errors), bytes_result) |
---|
829 | n/a | |
---|
830 | n/a | def test_result_pairs(self): |
---|
831 | n/a | # Check encoding and decoding between result pairs |
---|
832 | n/a | result_types = [ |
---|
833 | n/a | urllib.parse.DefragResult, |
---|
834 | n/a | urllib.parse.SplitResult, |
---|
835 | n/a | urllib.parse.ParseResult, |
---|
836 | n/a | ] |
---|
837 | n/a | for result_type in result_types: |
---|
838 | n/a | self._check_result_type(result_type) |
---|
839 | n/a | |
---|
840 | n/a | def test_parse_qs_encoding(self): |
---|
841 | n/a | result = urllib.parse.parse_qs("key=\u0141%E9", encoding="latin-1") |
---|
842 | n/a | self.assertEqual(result, {'key': ['\u0141\xE9']}) |
---|
843 | n/a | result = urllib.parse.parse_qs("key=\u0141%C3%A9", encoding="utf-8") |
---|
844 | n/a | self.assertEqual(result, {'key': ['\u0141\xE9']}) |
---|
845 | n/a | result = urllib.parse.parse_qs("key=\u0141%C3%A9", encoding="ascii") |
---|
846 | n/a | self.assertEqual(result, {'key': ['\u0141\ufffd\ufffd']}) |
---|
847 | n/a | result = urllib.parse.parse_qs("key=\u0141%E9-", encoding="ascii") |
---|
848 | n/a | self.assertEqual(result, {'key': ['\u0141\ufffd-']}) |
---|
849 | n/a | result = urllib.parse.parse_qs("key=\u0141%E9-", encoding="ascii", |
---|
850 | n/a | errors="ignore") |
---|
851 | n/a | self.assertEqual(result, {'key': ['\u0141-']}) |
---|
852 | n/a | |
---|
853 | n/a | def test_parse_qsl_encoding(self): |
---|
854 | n/a | result = urllib.parse.parse_qsl("key=\u0141%E9", encoding="latin-1") |
---|
855 | n/a | self.assertEqual(result, [('key', '\u0141\xE9')]) |
---|
856 | n/a | result = urllib.parse.parse_qsl("key=\u0141%C3%A9", encoding="utf-8") |
---|
857 | n/a | self.assertEqual(result, [('key', '\u0141\xE9')]) |
---|
858 | n/a | result = urllib.parse.parse_qsl("key=\u0141%C3%A9", encoding="ascii") |
---|
859 | n/a | self.assertEqual(result, [('key', '\u0141\ufffd\ufffd')]) |
---|
860 | n/a | result = urllib.parse.parse_qsl("key=\u0141%E9-", encoding="ascii") |
---|
861 | n/a | self.assertEqual(result, [('key', '\u0141\ufffd-')]) |
---|
862 | n/a | result = urllib.parse.parse_qsl("key=\u0141%E9-", encoding="ascii", |
---|
863 | n/a | errors="ignore") |
---|
864 | n/a | self.assertEqual(result, [('key', '\u0141-')]) |
---|
865 | n/a | |
---|
866 | n/a | def test_urlencode_sequences(self): |
---|
867 | n/a | # Other tests incidentally urlencode things; test non-covered cases: |
---|
868 | n/a | # Sequence and object values. |
---|
869 | n/a | result = urllib.parse.urlencode({'a': [1, 2], 'b': (3, 4, 5)}, True) |
---|
870 | n/a | # we cannot rely on ordering here |
---|
871 | n/a | assert set(result.split('&')) == {'a=1', 'a=2', 'b=3', 'b=4', 'b=5'} |
---|
872 | n/a | |
---|
873 | n/a | class Trivial: |
---|
874 | n/a | def __str__(self): |
---|
875 | n/a | return 'trivial' |
---|
876 | n/a | |
---|
877 | n/a | result = urllib.parse.urlencode({'a': Trivial()}, True) |
---|
878 | n/a | self.assertEqual(result, 'a=trivial') |
---|
879 | n/a | |
---|
880 | n/a | def test_urlencode_quote_via(self): |
---|
881 | n/a | result = urllib.parse.urlencode({'a': 'some value'}) |
---|
882 | n/a | self.assertEqual(result, "a=some+value") |
---|
883 | n/a | result = urllib.parse.urlencode({'a': 'some value/another'}, |
---|
884 | n/a | quote_via=urllib.parse.quote) |
---|
885 | n/a | self.assertEqual(result, "a=some%20value%2Fanother") |
---|
886 | n/a | result = urllib.parse.urlencode({'a': 'some value/another'}, |
---|
887 | n/a | safe='/', quote_via=urllib.parse.quote) |
---|
888 | n/a | self.assertEqual(result, "a=some%20value/another") |
---|
889 | n/a | |
---|
890 | n/a | def test_quote_from_bytes(self): |
---|
891 | n/a | self.assertRaises(TypeError, urllib.parse.quote_from_bytes, 'foo') |
---|
892 | n/a | result = urllib.parse.quote_from_bytes(b'archaeological arcana') |
---|
893 | n/a | self.assertEqual(result, 'archaeological%20arcana') |
---|
894 | n/a | result = urllib.parse.quote_from_bytes(b'') |
---|
895 | n/a | self.assertEqual(result, '') |
---|
896 | n/a | |
---|
897 | n/a | def test_unquote_to_bytes(self): |
---|
898 | n/a | result = urllib.parse.unquote_to_bytes('abc%20def') |
---|
899 | n/a | self.assertEqual(result, b'abc def') |
---|
900 | n/a | result = urllib.parse.unquote_to_bytes('') |
---|
901 | n/a | self.assertEqual(result, b'') |
---|
902 | n/a | |
---|
903 | n/a | def test_quote_errors(self): |
---|
904 | n/a | self.assertRaises(TypeError, urllib.parse.quote, b'foo', |
---|
905 | n/a | encoding='utf-8') |
---|
906 | n/a | self.assertRaises(TypeError, urllib.parse.quote, b'foo', errors='strict') |
---|
907 | n/a | |
---|
908 | n/a | def test_issue14072(self): |
---|
909 | n/a | p1 = urllib.parse.urlsplit('tel:+31-641044153') |
---|
910 | n/a | self.assertEqual(p1.scheme, 'tel') |
---|
911 | n/a | self.assertEqual(p1.path, '+31-641044153') |
---|
912 | n/a | p2 = urllib.parse.urlsplit('tel:+31641044153') |
---|
913 | n/a | self.assertEqual(p2.scheme, 'tel') |
---|
914 | n/a | self.assertEqual(p2.path, '+31641044153') |
---|
915 | n/a | # assert the behavior for urlparse |
---|
916 | n/a | p1 = urllib.parse.urlparse('tel:+31-641044153') |
---|
917 | n/a | self.assertEqual(p1.scheme, 'tel') |
---|
918 | n/a | self.assertEqual(p1.path, '+31-641044153') |
---|
919 | n/a | p2 = urllib.parse.urlparse('tel:+31641044153') |
---|
920 | n/a | self.assertEqual(p2.scheme, 'tel') |
---|
921 | n/a | self.assertEqual(p2.path, '+31641044153') |
---|
922 | n/a | |
---|
923 | n/a | def test_telurl_params(self): |
---|
924 | n/a | p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516') |
---|
925 | n/a | self.assertEqual(p1.scheme, 'tel') |
---|
926 | n/a | self.assertEqual(p1.path, '123-4') |
---|
927 | n/a | self.assertEqual(p1.params, 'phone-context=+1-650-516') |
---|
928 | n/a | |
---|
929 | n/a | p1 = urllib.parse.urlparse('tel:+1-201-555-0123') |
---|
930 | n/a | self.assertEqual(p1.scheme, 'tel') |
---|
931 | n/a | self.assertEqual(p1.path, '+1-201-555-0123') |
---|
932 | n/a | self.assertEqual(p1.params, '') |
---|
933 | n/a | |
---|
934 | n/a | p1 = urllib.parse.urlparse('tel:7042;phone-context=example.com') |
---|
935 | n/a | self.assertEqual(p1.scheme, 'tel') |
---|
936 | n/a | self.assertEqual(p1.path, '7042') |
---|
937 | n/a | self.assertEqual(p1.params, 'phone-context=example.com') |
---|
938 | n/a | |
---|
939 | n/a | p1 = urllib.parse.urlparse('tel:863-1234;phone-context=+1-914-555') |
---|
940 | n/a | self.assertEqual(p1.scheme, 'tel') |
---|
941 | n/a | self.assertEqual(p1.path, '863-1234') |
---|
942 | n/a | self.assertEqual(p1.params, 'phone-context=+1-914-555') |
---|
943 | n/a | |
---|
944 | n/a | def test_Quoter_repr(self): |
---|
945 | n/a | quoter = urllib.parse.Quoter(urllib.parse._ALWAYS_SAFE) |
---|
946 | n/a | self.assertIn('Quoter', repr(quoter)) |
---|
947 | n/a | |
---|
948 | n/a | def test_all(self): |
---|
949 | n/a | expected = [] |
---|
950 | n/a | undocumented = { |
---|
951 | n/a | 'splitattr', 'splithost', 'splitnport', 'splitpasswd', |
---|
952 | n/a | 'splitport', 'splitquery', 'splittag', 'splittype', 'splituser', |
---|
953 | n/a | 'splitvalue', |
---|
954 | n/a | 'Quoter', 'ResultBase', 'clear_cache', 'to_bytes', 'unwrap', |
---|
955 | n/a | } |
---|
956 | n/a | for name in dir(urllib.parse): |
---|
957 | n/a | if name.startswith('_') or name in undocumented: |
---|
958 | n/a | continue |
---|
959 | n/a | object = getattr(urllib.parse, name) |
---|
960 | n/a | if getattr(object, '__module__', None) == 'urllib.parse': |
---|
961 | n/a | expected.append(name) |
---|
962 | n/a | self.assertCountEqual(urllib.parse.__all__, expected) |
---|
963 | n/a | |
---|
964 | n/a | |
---|
965 | n/a | class Utility_Tests(unittest.TestCase): |
---|
966 | n/a | """Testcase to test the various utility functions in the urllib.""" |
---|
967 | n/a | # In Python 2 this test class was in test_urllib. |
---|
968 | n/a | |
---|
969 | n/a | def test_splittype(self): |
---|
970 | n/a | splittype = urllib.parse.splittype |
---|
971 | n/a | self.assertEqual(splittype('type:opaquestring'), ('type', 'opaquestring')) |
---|
972 | n/a | self.assertEqual(splittype('opaquestring'), (None, 'opaquestring')) |
---|
973 | n/a | self.assertEqual(splittype(':opaquestring'), (None, ':opaquestring')) |
---|
974 | n/a | self.assertEqual(splittype('type:'), ('type', '')) |
---|
975 | n/a | self.assertEqual(splittype('type:opaque:string'), ('type', 'opaque:string')) |
---|
976 | n/a | |
---|
977 | n/a | def test_splithost(self): |
---|
978 | n/a | splithost = urllib.parse.splithost |
---|
979 | n/a | self.assertEqual(splithost('//www.example.org:80/foo/bar/baz.html'), |
---|
980 | n/a | ('www.example.org:80', '/foo/bar/baz.html')) |
---|
981 | n/a | self.assertEqual(splithost('//www.example.org:80'), |
---|
982 | n/a | ('www.example.org:80', '')) |
---|
983 | n/a | self.assertEqual(splithost('/foo/bar/baz.html'), |
---|
984 | n/a | (None, '/foo/bar/baz.html')) |
---|
985 | n/a | |
---|
986 | n/a | def test_splituser(self): |
---|
987 | n/a | splituser = urllib.parse.splituser |
---|
988 | n/a | self.assertEqual(splituser('User:Pass@www.python.org:080'), |
---|
989 | n/a | ('User:Pass', 'www.python.org:080')) |
---|
990 | n/a | self.assertEqual(splituser('@www.python.org:080'), |
---|
991 | n/a | ('', 'www.python.org:080')) |
---|
992 | n/a | self.assertEqual(splituser('www.python.org:080'), |
---|
993 | n/a | (None, 'www.python.org:080')) |
---|
994 | n/a | self.assertEqual(splituser('User:Pass@'), |
---|
995 | n/a | ('User:Pass', '')) |
---|
996 | n/a | self.assertEqual(splituser('User@example.com:Pass@www.python.org:080'), |
---|
997 | n/a | ('User@example.com:Pass', 'www.python.org:080')) |
---|
998 | n/a | |
---|
999 | n/a | def test_splitpasswd(self): |
---|
1000 | n/a | # Some of the password examples are not sensible, but it is added to |
---|
1001 | n/a | # confirming to RFC2617 and addressing issue4675. |
---|
1002 | n/a | splitpasswd = urllib.parse.splitpasswd |
---|
1003 | n/a | self.assertEqual(splitpasswd('user:ab'), ('user', 'ab')) |
---|
1004 | n/a | self.assertEqual(splitpasswd('user:a\nb'), ('user', 'a\nb')) |
---|
1005 | n/a | self.assertEqual(splitpasswd('user:a\tb'), ('user', 'a\tb')) |
---|
1006 | n/a | self.assertEqual(splitpasswd('user:a\rb'), ('user', 'a\rb')) |
---|
1007 | n/a | self.assertEqual(splitpasswd('user:a\fb'), ('user', 'a\fb')) |
---|
1008 | n/a | self.assertEqual(splitpasswd('user:a\vb'), ('user', 'a\vb')) |
---|
1009 | n/a | self.assertEqual(splitpasswd('user:a:b'), ('user', 'a:b')) |
---|
1010 | n/a | self.assertEqual(splitpasswd('user:a b'), ('user', 'a b')) |
---|
1011 | n/a | self.assertEqual(splitpasswd('user 2:ab'), ('user 2', 'ab')) |
---|
1012 | n/a | self.assertEqual(splitpasswd('user+1:a+b'), ('user+1', 'a+b')) |
---|
1013 | n/a | self.assertEqual(splitpasswd('user:'), ('user', '')) |
---|
1014 | n/a | self.assertEqual(splitpasswd('user'), ('user', None)) |
---|
1015 | n/a | self.assertEqual(splitpasswd(':ab'), ('', 'ab')) |
---|
1016 | n/a | |
---|
1017 | n/a | def test_splitport(self): |
---|
1018 | n/a | splitport = urllib.parse.splitport |
---|
1019 | n/a | self.assertEqual(splitport('parrot:88'), ('parrot', '88')) |
---|
1020 | n/a | self.assertEqual(splitport('parrot'), ('parrot', None)) |
---|
1021 | n/a | self.assertEqual(splitport('parrot:'), ('parrot', None)) |
---|
1022 | n/a | self.assertEqual(splitport('127.0.0.1'), ('127.0.0.1', None)) |
---|
1023 | n/a | self.assertEqual(splitport('parrot:cheese'), ('parrot:cheese', None)) |
---|
1024 | n/a | self.assertEqual(splitport('[::1]:88'), ('[::1]', '88')) |
---|
1025 | n/a | self.assertEqual(splitport('[::1]'), ('[::1]', None)) |
---|
1026 | n/a | self.assertEqual(splitport(':88'), ('', '88')) |
---|
1027 | n/a | |
---|
1028 | n/a | def test_splitnport(self): |
---|
1029 | n/a | splitnport = urllib.parse.splitnport |
---|
1030 | n/a | self.assertEqual(splitnport('parrot:88'), ('parrot', 88)) |
---|
1031 | n/a | self.assertEqual(splitnport('parrot'), ('parrot', -1)) |
---|
1032 | n/a | self.assertEqual(splitnport('parrot', 55), ('parrot', 55)) |
---|
1033 | n/a | self.assertEqual(splitnport('parrot:'), ('parrot', -1)) |
---|
1034 | n/a | self.assertEqual(splitnport('parrot:', 55), ('parrot', 55)) |
---|
1035 | n/a | self.assertEqual(splitnport('127.0.0.1'), ('127.0.0.1', -1)) |
---|
1036 | n/a | self.assertEqual(splitnport('127.0.0.1', 55), ('127.0.0.1', 55)) |
---|
1037 | n/a | self.assertEqual(splitnport('parrot:cheese'), ('parrot', None)) |
---|
1038 | n/a | self.assertEqual(splitnport('parrot:cheese', 55), ('parrot', None)) |
---|
1039 | n/a | |
---|
1040 | n/a | def test_splitquery(self): |
---|
1041 | n/a | # Normal cases are exercised by other tests; ensure that we also |
---|
1042 | n/a | # catch cases with no port specified (testcase ensuring coverage) |
---|
1043 | n/a | splitquery = urllib.parse.splitquery |
---|
1044 | n/a | self.assertEqual(splitquery('http://python.org/fake?foo=bar'), |
---|
1045 | n/a | ('http://python.org/fake', 'foo=bar')) |
---|
1046 | n/a | self.assertEqual(splitquery('http://python.org/fake?foo=bar?'), |
---|
1047 | n/a | ('http://python.org/fake?foo=bar', '')) |
---|
1048 | n/a | self.assertEqual(splitquery('http://python.org/fake'), |
---|
1049 | n/a | ('http://python.org/fake', None)) |
---|
1050 | n/a | self.assertEqual(splitquery('?foo=bar'), ('', 'foo=bar')) |
---|
1051 | n/a | |
---|
1052 | n/a | def test_splittag(self): |
---|
1053 | n/a | splittag = urllib.parse.splittag |
---|
1054 | n/a | self.assertEqual(splittag('http://example.com?foo=bar#baz'), |
---|
1055 | n/a | ('http://example.com?foo=bar', 'baz')) |
---|
1056 | n/a | self.assertEqual(splittag('http://example.com?foo=bar#'), |
---|
1057 | n/a | ('http://example.com?foo=bar', '')) |
---|
1058 | n/a | self.assertEqual(splittag('#baz'), ('', 'baz')) |
---|
1059 | n/a | self.assertEqual(splittag('http://example.com?foo=bar'), |
---|
1060 | n/a | ('http://example.com?foo=bar', None)) |
---|
1061 | n/a | self.assertEqual(splittag('http://example.com?foo=bar#baz#boo'), |
---|
1062 | n/a | ('http://example.com?foo=bar#baz', 'boo')) |
---|
1063 | n/a | |
---|
1064 | n/a | def test_splitattr(self): |
---|
1065 | n/a | splitattr = urllib.parse.splitattr |
---|
1066 | n/a | self.assertEqual(splitattr('/path;attr1=value1;attr2=value2'), |
---|
1067 | n/a | ('/path', ['attr1=value1', 'attr2=value2'])) |
---|
1068 | n/a | self.assertEqual(splitattr('/path;'), ('/path', [''])) |
---|
1069 | n/a | self.assertEqual(splitattr(';attr1=value1;attr2=value2'), |
---|
1070 | n/a | ('', ['attr1=value1', 'attr2=value2'])) |
---|
1071 | n/a | self.assertEqual(splitattr('/path'), ('/path', [])) |
---|
1072 | n/a | |
---|
1073 | n/a | def test_splitvalue(self): |
---|
1074 | n/a | # Normal cases are exercised by other tests; test pathological cases |
---|
1075 | n/a | # with no key/value pairs. (testcase ensuring coverage) |
---|
1076 | n/a | splitvalue = urllib.parse.splitvalue |
---|
1077 | n/a | self.assertEqual(splitvalue('foo=bar'), ('foo', 'bar')) |
---|
1078 | n/a | self.assertEqual(splitvalue('foo='), ('foo', '')) |
---|
1079 | n/a | self.assertEqual(splitvalue('=bar'), ('', 'bar')) |
---|
1080 | n/a | self.assertEqual(splitvalue('foobar'), ('foobar', None)) |
---|
1081 | n/a | self.assertEqual(splitvalue('foo=bar=baz'), ('foo', 'bar=baz')) |
---|
1082 | n/a | |
---|
1083 | n/a | def test_to_bytes(self): |
---|
1084 | n/a | result = urllib.parse.to_bytes('http://www.python.org') |
---|
1085 | n/a | self.assertEqual(result, 'http://www.python.org') |
---|
1086 | n/a | self.assertRaises(UnicodeError, urllib.parse.to_bytes, |
---|
1087 | n/a | 'http://www.python.org/medi\u00e6val') |
---|
1088 | n/a | |
---|
1089 | n/a | def test_unwrap(self): |
---|
1090 | n/a | url = urllib.parse.unwrap('<URL:type://host/path>') |
---|
1091 | n/a | self.assertEqual(url, 'type://host/path') |
---|
1092 | n/a | |
---|
1093 | n/a | |
---|
1094 | n/a | if __name__ == "__main__": |
---|
1095 | n/a | unittest.main() |
---|