Debian Codegraph @

cpython27/Lib/test/test_urlparse.py

raw symbols SCIP
1 from test import test_support
2 import sys
3 import unicodedata
4 import unittest
5 import urlparse
6
7 RFC1808_BASE = "http://a/b/c/d;p?q#f"
8 RFC2396_BASE = "http://a/b/c/d;p?q"
9 RFC3986_BASE = 'http://a/b/c/d;p?q'
10 SIMPLE_BASE = 'http://a/b/c/d'
11
12 # A list of test cases. Each test case is a two-tuple that contains
13 # a string with the query and a dictionary with the expected result.
14
15 parse_qsl_test_cases = [
16 ("", []),
17 ("&", []),
18 ("&&", []),
19 ("=", [('', '')]),
20 ("=a", [('', 'a')]),
21 ("a", [('a', '')]),
22 ("a=", [('a', '')]),
23 ("a=", [('a', '')]),
24 ("&a=b", [('a', 'b')]),
25 ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
26 ("a=1&a=2", [('a', '1'), ('a', '2')]),
27 (";", []),
28 (";;", []),
29 (";a=b", [('a', 'b')]),
30 ("a=a+b;b=b+c", [('a', 'a b'), ('b', 'b c')]),
31 ("a=1;a=2", [('a', '1'), ('a', '2')]),
32 (b";", []),
33 (b";;", []),
34 (b";a=b", [(b'a', b'b')]),
35 (b"a=a+b;b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
36 (b"a=1;a=2", [(b'a', b'1'), (b'a', b'2')]),
37 ]
38
39 parse_qs_test_cases = [
40 ("", {}),
41 ("&", {}),
42 ("&&", {}),
43 ("=", {'': ['']}),
44 ("=a", {'': ['a']}),
45 ("a", {'a': ['']}),
46 ("a=", {'a': ['']}),
47 ("&a=b", {'a': ['b']}),
48 ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
49 ("a=1&a=2", {'a': ['1', '2']}),
50 (b"", {}),
51 (b"&", {}),
52 (b"&&", {}),
53 (b"=", {b'': [b'']}),
54 (b"=a", {b'': [b'a']}),
55 (b"a", {b'a': [b'']}),
56 (b"a=", {b'a': [b'']}),
57 (b"&a=b", {b'a': [b'b']}),
58 (b"a=a+b&b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
59 (b"a=1&a=2", {b'a': [b'1', b'2']}),
60 (";", {}),
61 (";;", {}),
62 (";a=b", {'a': ['b']}),
63 ("a=a+b;b=b+c", {'a': ['a b'], 'b': ['b c']}),
64 ("a=1;a=2", {'a': ['1', '2']}),
65 (b";", {}),
66 (b";;", {}),
67 (b";a=b", {b'a': [b'b']}),
68 (b"a=a+b;b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
69 (b"a=1;a=2", {b'a': [b'1', b'2']}),
70 ]
71
72 class UrlParseTestCase(unittest.TestCase):
73
74 def checkRoundtrips(self, url, parsed, split):
75 result = urlparse.urlparse(url)
76 self.assertEqual(result, parsed)
77 t = (result.scheme, result.netloc, result.path,
78 result.params, result.query, result.fragment)
79 self.assertEqual(t, parsed)
80 # put it back together and it should be the same
81 result2 = urlparse.urlunparse(result)
82 self.assertEqual(result2, url)
83 self.assertEqual(result2, result.geturl())
84
85 # the result of geturl() is a fixpoint; we can always parse it
86 # again to get the same result:
87 result3 = urlparse.urlparse(result.geturl())
88 self.assertEqual(result3.geturl(), result.geturl())
89 self.assertEqual(result3, result)
90 self.assertEqual(result3.scheme, result.scheme)
91 self.assertEqual(result3.netloc, result.netloc)
92 self.assertEqual(result3.path, result.path)
93 self.assertEqual(result3.params, result.params)
94 self.assertEqual(result3.query, result.query)
95 self.assertEqual(result3.fragment, result.fragment)
96 self.assertEqual(result3.username, result.username)
97 self.assertEqual(result3.password, result.password)
98 self.assertEqual(result3.hostname, result.hostname)
99 self.assertEqual(result3.port, result.port)
100
101 # check the roundtrip using urlsplit() as well
102 result = urlparse.urlsplit(url)
103 self.assertEqual(result, split)
104 t = (result.scheme, result.netloc, result.path,
105 result.query, result.fragment)
106 self.assertEqual(t, split)
107 result2 = urlparse.urlunsplit(result)
108 self.assertEqual(result2, url)
109 self.assertEqual(result2, result.geturl())
110
111 # check the fixpoint property of re-parsing the result of geturl()
112 result3 = urlparse.urlsplit(result.geturl())
113 self.assertEqual(result3.geturl(), result.geturl())
114 self.assertEqual(result3, result)
115 self.assertEqual(result3.scheme, result.scheme)
116 self.assertEqual(result3.netloc, result.netloc)
117 self.assertEqual(result3.path, result.path)
118 self.assertEqual(result3.query, result.query)
119 self.assertEqual(result3.fragment, result.fragment)
120 self.assertEqual(result3.username, result.username)
121 self.assertEqual(result3.password, result.password)
122 self.assertEqual(result3.hostname, result.hostname)
123 self.assertEqual(result3.port, result.port)
124
125 def test_qsl(self):
126 for orig, expect in parse_qsl_test_cases:
127 result = urlparse.parse_qsl(orig, keep_blank_values=True)
128 self.assertEqual(result, expect, "Error parsing %r" % orig)
129 expect_without_blanks = [v for v in expect if len(v[1])]
130 result = urlparse.parse_qsl(orig, keep_blank_values=False)
131 self.assertEqual(result, expect_without_blanks,
132 "Error parsing %r" % orig)
133
134 def test_qs(self):
135 for orig, expect in parse_qs_test_cases:
136 result = urlparse.parse_qs(orig, keep_blank_values=True)
137 self.assertEqual(result, expect, "Error parsing %r" % orig)
138 expect_without_blanks = dict(
139 [(v, expect[v]) for v in expect if len(expect[v][0])])
140 result = urlparse.parse_qs(orig, keep_blank_values=False)
141 self.assertEqual(result, expect_without_blanks,
142 "Error parsing %r" % orig)
143
144 def test_roundtrips(self):
145 testcases = [
146 ('file:///tmp/junk.txt',
147 ('file', '', '/tmp/junk.txt', '', '', ''),
148 ('file', '', '/tmp/junk.txt', '', '')),
149 ('imap://mail.python.org/mbox1',
150 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
151 ('imap', 'mail.python.org', '/mbox1', '', '')),
152 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
153 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
154 '', '', ''),
155 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
156 '', '')),
157 ('nfs://server/path/to/file.txt',
158 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
159 ('nfs', 'server', '/path/to/file.txt', '', '')),
160 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
161 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
162 '', '', ''),
163 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
164 '', '')),
165 ('git+ssh://[email protected]/user/project.git',
166 ('git+ssh', '[email protected]','/user/project.git',
167 '','',''),
168 ('git+ssh', '[email protected]','/user/project.git',
169 '', ''))
170 ]
171 for url, parsed, split in testcases:
172 self.checkRoundtrips(url, parsed, split)
173
174 def test_http_roundtrips(self):
175 # urlparse.urlsplit treats 'http:' as an optimized special case,
176 # so we test both 'http:' and 'https:' in all the following.
177 # Three cheers for white box knowledge!
178 testcases = [
179 ('://www.python.org',
180 ('www.python.org', '', '', '', ''),
181 ('www.python.org', '', '', '')),
182 ('://www.python.org#abc',
183 ('www.python.org', '', '', '', 'abc'),
184 ('www.python.org', '', '', 'abc')),
185 ('://www.python.org?q=abc',
186 ('www.python.org', '', '', 'q=abc', ''),
187 ('www.python.org', '', 'q=abc', '')),
188 ('://www.python.org/#abc',
189 ('www.python.org', '/', '', '', 'abc'),
190 ('www.python.org', '/', '', 'abc')),
191 ('://a/b/c/d;p?q#f',
192 ('a', '/b/c/d', 'p', 'q', 'f'),
193 ('a', '/b/c/d;p', 'q', 'f')),
194 ]
195 for scheme in ('http', 'https'):
196 for url, parsed, split in testcases:
197 url = scheme + url
198 parsed = (scheme,) + parsed
199 split = (scheme,) + split
200 self.checkRoundtrips(url, parsed, split)
201
202 def checkJoin(self, base, relurl, expected):
203 self.assertEqual(urlparse.urljoin(base, relurl), expected,
204 (base, relurl, expected))
205
206 def test_unparse_parse(self):
207 for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
208 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
209 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
210
211 def test_RFC1808(self):
212 # "normal" cases from RFC 1808:
213 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
214 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
215 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
216 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
217 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
218 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
219 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
220 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
221 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
222 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
223 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
224 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
225 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
226 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
227 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
228 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
229 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
230 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
231 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
232 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
233 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
234 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
235
236 # "abnormal" cases from RFC 1808:
237 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
238 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
239 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
240 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
241 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
242 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
243 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
244 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
245 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
246 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
247 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
248 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
249 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
250
251 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
252 # so we'll not actually run these tests (which expect 1808 behavior).
253 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
254 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
255
256 def test_RFC2368(self):
257 # Issue 11467: path that starts with a number is not parsed correctly
258 self.assertEqual(urlparse.urlparse('mailto:[email protected]'),
259 ('mailto', '', '[email protected]', '', '', ''))
260
261 def test_RFC2396(self):
262 # cases from RFC 2396
263 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
264 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
265 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
266 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
267 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
268 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
269 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
270 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
271 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
272 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
273 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
274 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
275 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
276 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
277 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
278 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
279 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
280 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
281 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
282 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
283 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
284 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
285 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
286 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
287 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
288 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
289 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
290 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
291 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
292 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
293 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
294 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
295 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
296 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
297 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
298 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
299 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
300 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
301 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
302
303 def test_RFC3986(self):
304 # Test cases from RFC3986
305 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
306 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
307 self.checkJoin(RFC3986_BASE, 'g:h','g:h')
308 self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
309 self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
310 self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
311 self.checkJoin(RFC3986_BASE, '/g','http://a/g')
312 self.checkJoin(RFC3986_BASE, '//g','http://g')
313 self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
314 self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
315 self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
316 self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
317 self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
318 self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
319 self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
320 self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
321 self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
322 self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
323 self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
324 self.checkJoin(RFC3986_BASE, '..','http://a/b/')
325 self.checkJoin(RFC3986_BASE, '../','http://a/b/')
326 self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
327 self.checkJoin(RFC3986_BASE, '../..','http://a/')
328 self.checkJoin(RFC3986_BASE, '../../','http://a/')
329 self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
330
331 #Abnormal Examples
332
333 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
334 # Tests are here for reference.
335
336 #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
337 #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
338 #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
339 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
340
341 self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
342 self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
343 self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
344 self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
345 self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
346 self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
347 self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
348 self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
349 self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
350 self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
351 self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
352 self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
353 self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
354 self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
355 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
356 self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') # relaxed parser
357
358 # Test for issue9721
359 self.checkJoin('http://a/b/c/de', ';x','http://a/b/c/;x')
360
361 def test_urljoins(self):
362 self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
363 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
364 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
365 self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
366 self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
367 self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
368 self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
369 self.checkJoin(SIMPLE_BASE, '//g','http://g')
370 self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
371 self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
372 self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
373 self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
374 self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
375 self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
376 self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
377 self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
378 self.checkJoin(SIMPLE_BASE, '../..','http://a/')
379 self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
380 self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
381 self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
382 self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
383 self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
384 self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
385 self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
386 self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
387 self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
388 self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
389 self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
390 self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
391 self.checkJoin('http:///', '..','http:///')
392 self.checkJoin('', 'http://a/b/c/g?y/./x','http://a/b/c/g?y/./x')
393 self.checkJoin('', 'http://a/./g', 'http://a/./g')
394 self.checkJoin('svn://pathtorepo/dir1','dir2','svn://pathtorepo/dir2')
395 self.checkJoin('svn+ssh://pathtorepo/dir1','dir2','svn+ssh://pathtorepo/dir2')
396
397 def test_RFC2732(self):
398 for url, hostname, port in [
399 ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
400 ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
401 ('http://[::1]:5432/foo/', '::1', 5432),
402 ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
403 ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
404 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
405 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
406 ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
407 ('http://[::ffff:12.34.56.78]:5432/foo/',
408 '::ffff:12.34.56.78', 5432),
409 ('http://Test.python.org/foo/', 'test.python.org', None),
410 ('http://12.34.56.78/foo/', '12.34.56.78', None),
411 ('http://[::1]/foo/', '::1', None),
412 ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
413 ('http://[dead:beef::]/foo/', 'dead:beef::', None),
414 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
415 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
416 ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
417 ('http://[::ffff:12.34.56.78]/foo/',
418 '::ffff:12.34.56.78', None),
419 ('http://Test.python.org:/foo/', 'test.python.org', None),
420 ('http://12.34.56.78:/foo/', '12.34.56.78', None),
421 ('http://[::1]:/foo/', '::1', None),
422 ('http://[dead:beef::1]:/foo/', 'dead:beef::1', None),
423 ('http://[dead:beef::]:/foo/', 'dead:beef::', None),
424 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:/foo/',
425 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
426 ('http://[::12.34.56.78]:/foo/', '::12.34.56.78', None),
427 ('http://[::ffff:12.34.56.78]:/foo/',
428 '::ffff:12.34.56.78', None),
429 ]:
430 urlparsed = urlparse.urlparse(url)
431 self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port))
432
433 for invalid_url in [
434 'http://::12.34.56.78]/',
435 'http://[::1/foo/',
436 'ftp://[::1/foo/bad]/bad',
437 'http://[::1/foo/bad]/bad',
438 'http://[::ffff:12.34.56.78']:
439 self.assertRaises(ValueError, urlparse.urlparse, invalid_url)
440
441 def test_urldefrag(self):
442 for url, defrag, frag in [
443 ('http://python.org#frag', 'http://python.org', 'frag'),
444 ('http://python.org', 'http://python.org', ''),
445 ('http://python.org/#frag', 'http://python.org/', 'frag'),
446 ('http://python.org/', 'http://python.org/', ''),
447 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
448 ('http://python.org/?q', 'http://python.org/?q', ''),
449 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
450 ('http://python.org/p?q', 'http://python.org/p?q', ''),
451 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
452 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
453 ]:
454 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
455
456 def test_urlsplit_attributes(self):
457 url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
458 p = urlparse.urlsplit(url)
459 self.assertEqual(p.scheme, "http")
460 self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
461 self.assertEqual(p.path, "/doc/")
462 self.assertEqual(p.query, "")
463 self.assertEqual(p.fragment, "frag")
464 self.assertEqual(p.username, None)
465 self.assertEqual(p.password, None)
466 self.assertEqual(p.hostname, "www.python.org")
467 self.assertEqual(p.port, None)
468 # geturl() won't return exactly the original URL in this case
469 # since the scheme is always case-normalized
470 #self.assertEqual(p.geturl(), url)
471
472 url = "http://User:[email protected]:080/doc/?query=yes#frag"
473 p = urlparse.urlsplit(url)
474 self.assertEqual(p.scheme, "http")
475 self.assertEqual(p.netloc, "User:[email protected]:080")
476 self.assertEqual(p.path, "/doc/")
477 self.assertEqual(p.query, "query=yes")
478 self.assertEqual(p.fragment, "frag")
479 self.assertEqual(p.username, "User")
480 self.assertEqual(p.password, "Pass")
481 self.assertEqual(p.hostname, "www.python.org")
482 self.assertEqual(p.port, 80)
483 self.assertEqual(p.geturl(), url)
484
485 # Addressing issue1698, which suggests Username can contain
486 # "@" characters. Though not RFC compliant, many ftp sites allow
487 # and request email addresses as usernames.
488
489 url = "http://[email protected]:[email protected]:080/doc/?query=yes#frag"
490 p = urlparse.urlsplit(url)
491 self.assertEqual(p.scheme, "http")
492 self.assertEqual(p.netloc, "[email protected]:[email protected]:080")
493 self.assertEqual(p.path, "/doc/")
494 self.assertEqual(p.query, "query=yes")
495 self.assertEqual(p.fragment, "frag")
496 self.assertEqual(p.username, "[email protected]")
497 self.assertEqual(p.password, "Pass")
498 self.assertEqual(p.hostname, "www.python.org")
499 self.assertEqual(p.port, 80)
500 self.assertEqual(p.geturl(), url)
501
502 # Verify an illegal port of value greater than 65535 is set as None
503 url = "http://www.python.org:65536"
504 p = urlparse.urlsplit(url)
505 self.assertEqual(p.port, None)
506
507 def test_issue14072(self):
508 p1 = urlparse.urlsplit('tel:+31-641044153')
509 self.assertEqual(p1.scheme, 'tel')
510 self.assertEqual(p1.path, '+31-641044153')
511
512 p2 = urlparse.urlsplit('tel:+31641044153')
513 self.assertEqual(p2.scheme, 'tel')
514 self.assertEqual(p2.path, '+31641044153')
515
516 # Assert for urlparse
517 p1 = urlparse.urlparse('tel:+31-641044153')
518 self.assertEqual(p1.scheme, 'tel')
519 self.assertEqual(p1.path, '+31-641044153')
520
521 p2 = urlparse.urlparse('tel:+31641044153')
522 self.assertEqual(p2.scheme, 'tel')
523 self.assertEqual(p2.path, '+31641044153')
524
525
526 def test_telurl_params(self):
527 p1 = urlparse.urlparse('tel:123-4;phone-context=+1-650-516')
528 self.assertEqual(p1.scheme, 'tel')
529 self.assertEqual(p1.path, '123-4')
530 self.assertEqual(p1.params, 'phone-context=+1-650-516')
531
532 p1 = urlparse.urlparse('tel:+1-201-555-0123')
533 self.assertEqual(p1.scheme, 'tel')
534 self.assertEqual(p1.path, '+1-201-555-0123')
535 self.assertEqual(p1.params, '')
536
537 p1 = urlparse.urlparse('tel:7042;phone-context=example.com')
538 self.assertEqual(p1.scheme, 'tel')
539 self.assertEqual(p1.path, '7042')
540 self.assertEqual(p1.params, 'phone-context=example.com')
541
542 p1 = urlparse.urlparse('tel:863-1234;phone-context=+1-914-555')
543 self.assertEqual(p1.scheme, 'tel')
544 self.assertEqual(p1.path, '863-1234')
545 self.assertEqual(p1.params, 'phone-context=+1-914-555')
546
547
548 def test_attributes_bad_port(self):
549 """Check handling of non-integer ports."""
550 p = urlparse.urlsplit("http://www.example.net:foo")
551 self.assertEqual(p.netloc, "www.example.net:foo")
552 self.assertRaises(ValueError, lambda: p.port)
553
554 p = urlparse.urlparse("http://www.example.net:foo")
555 self.assertEqual(p.netloc, "www.example.net:foo")
556 self.assertRaises(ValueError, lambda: p.port)
557
558 def test_attributes_without_netloc(self):
559 # This example is straight from RFC 3261. It looks like it
560 # should allow the username, hostname, and port to be filled
561 # in, but doesn't. Since it's a URI and doesn't use the
562 # scheme://netloc syntax, the netloc and related attributes
563 # should be left empty.
564 uri = "sip:[email protected];maddr=239.255.255.1;ttl=15"
565 p = urlparse.urlsplit(uri)
566 self.assertEqual(p.netloc, "")
567 self.assertEqual(p.username, None)
568 self.assertEqual(p.password, None)
569 self.assertEqual(p.hostname, None)
570 self.assertEqual(p.port, None)
571 self.assertEqual(p.geturl(), uri)
572
573 p = urlparse.urlparse(uri)
574 self.assertEqual(p.netloc, "")
575 self.assertEqual(p.username, None)
576 self.assertEqual(p.password, None)
577 self.assertEqual(p.hostname, None)
578 self.assertEqual(p.port, None)
579 self.assertEqual(p.geturl(), uri)
580
581 def test_caching(self):
582 # Test case for bug #1313119
583 uri = "http://example.com/doc/"
584 unicode_uri = unicode(uri)
585
586 urlparse.urlparse(unicode_uri)
587 p = urlparse.urlparse(uri)
588 self.assertEqual(type(p.scheme), type(uri))
589 self.assertEqual(type(p.hostname), type(uri))
590 self.assertEqual(type(p.path), type(uri))
591
592 def test_noslash(self):
593 # Issue 1637: http://foo.com?query is legal
594 self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
595 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
596
597 def test_anyscheme(self):
598 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
599 self.assertEqual(urlparse.urlparse("s3://foo.com/stuff"),
600 ('s3','foo.com','/stuff','','',''))
601 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff"),
602 ('x-newscheme','foo.com','/stuff','','',''))
603 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff?query#fragment"),
604 ('x-newscheme','foo.com','/stuff','','query','fragment'))
605 self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff?query"),
606 ('x-newscheme','foo.com','/stuff','','query',''))
607
608 def test_withoutscheme(self):
609 # Test urlparse without scheme
610 # Issue 754016: urlparse goes wrong with IP:port without scheme
611 # RFC 1808 specifies that netloc should start with //, urlparse expects
612 # the same, otherwise it classifies the portion of url as path.
613 self.assertEqual(urlparse.urlparse("path"),
614 ('','','path','','',''))
615 self.assertEqual(urlparse.urlparse("//www.python.org:80"),
616 ('','www.python.org:80','','','',''))
617 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
618 ('http','www.python.org:80','','','',''))
619
620 def test_portseparator(self):
621 # Issue 754016 makes changes for port separator ':' from scheme separator
622 self.assertEqual(urlparse.urlparse("path:80"),
623 ('','','path:80','','',''))
624 self.assertEqual(urlparse.urlparse("http:"),('http','','','','',''))
625 self.assertEqual(urlparse.urlparse("https:"),('https','','','','',''))
626 self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
627 ('http','www.python.org:80','','','',''))
628
629 def test_urlsplit_normalization(self):
630 # Certain characters should never occur in the netloc,
631 # including under normalization.
632 # Ensure that ALL of them are detected and cause an error
633 illegal_chars = u'/:#?@'
634 hex_chars = {'{:04X}'.format(ord(c)) for c in illegal_chars}
635 denorm_chars = [
636 c for c in map(unichr, range(128, sys.maxunicode))
637 if (hex_chars & set(unicodedata.decomposition(c).split()))
638 and c not in illegal_chars
639 ]
640 # Sanity check that we found at least one such character
641 self.assertIn(u'\u2100', denorm_chars)
642 self.assertIn(u'\uFF03', denorm_chars)
643
644 # bpo-36742: Verify port separators are ignored when they
645 # existed prior to decomposition
646 urlparse.urlsplit(u'http://\u30d5\u309a:80')
647 with self.assertRaises(ValueError):
648 urlparse.urlsplit(u'http://\u30d5\u309a\ufe1380')
649
650 for scheme in [u"http", u"https", u"ftp"]:
651 for netloc in [u"netloc{}false.netloc", u"n{}user@netloc"]:
652 for c in denorm_chars:
653 url = u"{}://{}/path".format(scheme, netloc.format(c))
654 if test_support.verbose:
655 print "Checking %r" % url
656 with self.assertRaises(ValueError):
657 urlparse.urlsplit(url)
658
659 # check error message: invalid netloc must be formated with repr()
660 # to get an ASCII error message
661 with self.assertRaises(ValueError) as cm:
662 urlparse.urlsplit(u'http://example.com\uFF03@bing.com')
663 self.assertEqual(str(cm.exception),
664 "netloc u'example.com\\[email protected]' contains invalid characters "
665 "under NFKC normalization")
666 self.assertIsInstance(cm.exception.args[0], str)
667
668 def test_main():
669 test_support.run_unittest(UrlParseTestCase)
670
671 if __name__ == "__main__":
672 test_main()