3 from test
import test_support
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'
11 # A list of test cases. Each test case is a a two-tuple that contains
12 # a string with the query and a dictionary with the expected result.
14 parse_qsl_test_cases
= [
23 ("&a=b", [('a', 'b')]),
24 ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
25 ("a=1&a=2", [('a', '1'), ('a', '2')]),
28 class UrlParseTestCase(unittest
.TestCase
):
30 def checkRoundtrips(self
, url
, parsed
, split
):
31 result
= urlparse
.urlparse(url
)
32 self
.assertEqual(result
, parsed
)
33 t
= (result
.scheme
, result
.netloc
, result
.path
,
34 result
.params
, result
.query
, result
.fragment
)
35 self
.assertEqual(t
, parsed
)
36 # put it back together and it should be the same
37 result2
= urlparse
.urlunparse(result
)
38 self
.assertEqual(result2
, url
)
39 self
.assertEqual(result2
, result
.geturl())
41 # the result of geturl() is a fixpoint; we can always parse it
42 # again to get the same result:
43 result3
= urlparse
.urlparse(result
.geturl())
44 self
.assertEqual(result3
.geturl(), result
.geturl())
45 self
.assertEqual(result3
, result
)
46 self
.assertEqual(result3
.scheme
, result
.scheme
)
47 self
.assertEqual(result3
.netloc
, result
.netloc
)
48 self
.assertEqual(result3
.path
, result
.path
)
49 self
.assertEqual(result3
.params
, result
.params
)
50 self
.assertEqual(result3
.query
, result
.query
)
51 self
.assertEqual(result3
.fragment
, result
.fragment
)
52 self
.assertEqual(result3
.username
, result
.username
)
53 self
.assertEqual(result3
.password
, result
.password
)
54 self
.assertEqual(result3
.hostname
, result
.hostname
)
55 self
.assertEqual(result3
.port
, result
.port
)
57 # check the roundtrip using urlsplit() as well
58 result
= urlparse
.urlsplit(url
)
59 self
.assertEqual(result
, split
)
60 t
= (result
.scheme
, result
.netloc
, result
.path
,
61 result
.query
, result
.fragment
)
62 self
.assertEqual(t
, split
)
63 result2
= urlparse
.urlunsplit(result
)
64 self
.assertEqual(result2
, url
)
65 self
.assertEqual(result2
, result
.geturl())
67 # check the fixpoint property of re-parsing the result of geturl()
68 result3
= urlparse
.urlsplit(result
.geturl())
69 self
.assertEqual(result3
.geturl(), result
.geturl())
70 self
.assertEqual(result3
, result
)
71 self
.assertEqual(result3
.scheme
, result
.scheme
)
72 self
.assertEqual(result3
.netloc
, result
.netloc
)
73 self
.assertEqual(result3
.path
, result
.path
)
74 self
.assertEqual(result3
.query
, result
.query
)
75 self
.assertEqual(result3
.fragment
, result
.fragment
)
76 self
.assertEqual(result3
.username
, result
.username
)
77 self
.assertEqual(result3
.password
, result
.password
)
78 self
.assertEqual(result3
.hostname
, result
.hostname
)
79 self
.assertEqual(result3
.port
, result
.port
)
82 for orig
, expect
in parse_qsl_test_cases
:
83 result
= urlparse
.parse_qsl(orig
, keep_blank_values
=True)
84 self
.assertEqual(result
, expect
, "Error parsing %s" % repr(orig
))
86 def test_roundtrips(self
):
88 ('file:///tmp/junk.txt',
89 ('file', '', '/tmp/junk.txt', '', '', ''),
90 ('file', '', '/tmp/junk.txt', '', '')),
91 ('imap://mail.python.org/mbox1',
92 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
93 ('imap', 'mail.python.org', '/mbox1', '', '')),
94 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
95 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
97 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
99 ('nfs://server/path/to/file.txt',
100 ('nfs', 'server', '/path/to/file.txt', '', '', ''),
101 ('nfs', 'server', '/path/to/file.txt', '', '')),
102 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
103 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
105 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
107 ('git+ssh://git@github.com/user/project.git',
108 ('git+ssh', 'git@github.com','/user/project.git',
110 ('git+ssh', 'git@github.com','/user/project.git',
113 for url
, parsed
, split
in testcases
:
114 self
.checkRoundtrips(url
, parsed
, split
)
116 def test_http_roundtrips(self
):
117 # urlparse.urlsplit treats 'http:' as an optimized special case,
118 # so we test both 'http:' and 'https:' in all the following.
119 # Three cheers for white box knowledge!
121 ('://www.python.org',
122 ('www.python.org', '', '', '', ''),
123 ('www.python.org', '', '', '')),
124 ('://www.python.org#abc',
125 ('www.python.org', '', '', '', 'abc'),
126 ('www.python.org', '', '', 'abc')),
127 ('://www.python.org?q=abc',
128 ('www.python.org', '', '', 'q=abc', ''),
129 ('www.python.org', '', 'q=abc', '')),
130 ('://www.python.org/#abc',
131 ('www.python.org', '/', '', '', 'abc'),
132 ('www.python.org', '/', '', 'abc')),
134 ('a', '/b/c/d', 'p', 'q', 'f'),
135 ('a', '/b/c/d;p', 'q', 'f')),
137 for scheme
in ('http', 'https'):
138 for url
, parsed
, split
in testcases
:
140 parsed
= (scheme
,) + parsed
141 split
= (scheme
,) + split
142 self
.checkRoundtrips(url
, parsed
, split
)
144 def checkJoin(self
, base
, relurl
, expected
):
145 self
.assertEqual(urlparse
.urljoin(base
, relurl
), expected
,
146 (base
, relurl
, expected
))
148 def test_unparse_parse(self
):
149 for u
in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
150 self
.assertEqual(urlparse
.urlunsplit(urlparse
.urlsplit(u
)), u
)
151 self
.assertEqual(urlparse
.urlunparse(urlparse
.urlparse(u
)), u
)
153 def test_RFC1808(self
):
154 # "normal" cases from RFC 1808:
155 self
.checkJoin(RFC1808_BASE
, 'g:h', 'g:h')
156 self
.checkJoin(RFC1808_BASE
, 'g', 'http://a/b/c/g')
157 self
.checkJoin(RFC1808_BASE
, './g', 'http://a/b/c/g')
158 self
.checkJoin(RFC1808_BASE
, 'g/', 'http://a/b/c/g/')
159 self
.checkJoin(RFC1808_BASE
, '/g', 'http://a/g')
160 self
.checkJoin(RFC1808_BASE
, '//g', 'http://g')
161 self
.checkJoin(RFC1808_BASE
, 'g?y', 'http://a/b/c/g?y')
162 self
.checkJoin(RFC1808_BASE
, 'g?y/./x', 'http://a/b/c/g?y/./x')
163 self
.checkJoin(RFC1808_BASE
, '#s', 'http://a/b/c/d;p?q#s')
164 self
.checkJoin(RFC1808_BASE
, 'g#s', 'http://a/b/c/g#s')
165 self
.checkJoin(RFC1808_BASE
, 'g#s/./x', 'http://a/b/c/g#s/./x')
166 self
.checkJoin(RFC1808_BASE
, 'g?y#s', 'http://a/b/c/g?y#s')
167 self
.checkJoin(RFC1808_BASE
, 'g;x', 'http://a/b/c/g;x')
168 self
.checkJoin(RFC1808_BASE
, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
169 self
.checkJoin(RFC1808_BASE
, '.', 'http://a/b/c/')
170 self
.checkJoin(RFC1808_BASE
, './', 'http://a/b/c/')
171 self
.checkJoin(RFC1808_BASE
, '..', 'http://a/b/')
172 self
.checkJoin(RFC1808_BASE
, '../', 'http://a/b/')
173 self
.checkJoin(RFC1808_BASE
, '../g', 'http://a/b/g')
174 self
.checkJoin(RFC1808_BASE
, '../..', 'http://a/')
175 self
.checkJoin(RFC1808_BASE
, '../../', 'http://a/')
176 self
.checkJoin(RFC1808_BASE
, '../../g', 'http://a/g')
178 # "abnormal" cases from RFC 1808:
179 self
.checkJoin(RFC1808_BASE
, '', 'http://a/b/c/d;p?q#f')
180 self
.checkJoin(RFC1808_BASE
, '../../../g', 'http://a/../g')
181 self
.checkJoin(RFC1808_BASE
, '../../../../g', 'http://a/../../g')
182 self
.checkJoin(RFC1808_BASE
, '/./g', 'http://a/./g')
183 self
.checkJoin(RFC1808_BASE
, '/../g', 'http://a/../g')
184 self
.checkJoin(RFC1808_BASE
, 'g.', 'http://a/b/c/g.')
185 self
.checkJoin(RFC1808_BASE
, '.g', 'http://a/b/c/.g')
186 self
.checkJoin(RFC1808_BASE
, 'g..', 'http://a/b/c/g..')
187 self
.checkJoin(RFC1808_BASE
, '..g', 'http://a/b/c/..g')
188 self
.checkJoin(RFC1808_BASE
, './../g', 'http://a/b/g')
189 self
.checkJoin(RFC1808_BASE
, './g/.', 'http://a/b/c/g/')
190 self
.checkJoin(RFC1808_BASE
, 'g/./h', 'http://a/b/c/g/h')
191 self
.checkJoin(RFC1808_BASE
, 'g/../h', 'http://a/b/c/h')
193 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
194 # so we'll not actually run these tests (which expect 1808 behavior).
195 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
196 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
198 def test_RFC2396(self
):
199 # cases from RFC 2396
202 self
.checkJoin(RFC2396_BASE
, 'g:h', 'g:h')
203 self
.checkJoin(RFC2396_BASE
, 'g', 'http://a/b/c/g')
204 self
.checkJoin(RFC2396_BASE
, './g', 'http://a/b/c/g')
205 self
.checkJoin(RFC2396_BASE
, 'g/', 'http://a/b/c/g/')
206 self
.checkJoin(RFC2396_BASE
, '/g', 'http://a/g')
207 self
.checkJoin(RFC2396_BASE
, '//g', 'http://g')
208 self
.checkJoin(RFC2396_BASE
, 'g?y', 'http://a/b/c/g?y')
209 self
.checkJoin(RFC2396_BASE
, '#s', 'http://a/b/c/d;p?q#s')
210 self
.checkJoin(RFC2396_BASE
, 'g#s', 'http://a/b/c/g#s')
211 self
.checkJoin(RFC2396_BASE
, 'g?y#s', 'http://a/b/c/g?y#s')
212 self
.checkJoin(RFC2396_BASE
, 'g;x', 'http://a/b/c/g;x')
213 self
.checkJoin(RFC2396_BASE
, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
214 self
.checkJoin(RFC2396_BASE
, '.', 'http://a/b/c/')
215 self
.checkJoin(RFC2396_BASE
, './', 'http://a/b/c/')
216 self
.checkJoin(RFC2396_BASE
, '..', 'http://a/b/')
217 self
.checkJoin(RFC2396_BASE
, '../', 'http://a/b/')
218 self
.checkJoin(RFC2396_BASE
, '../g', 'http://a/b/g')
219 self
.checkJoin(RFC2396_BASE
, '../..', 'http://a/')
220 self
.checkJoin(RFC2396_BASE
, '../../', 'http://a/')
221 self
.checkJoin(RFC2396_BASE
, '../../g', 'http://a/g')
222 self
.checkJoin(RFC2396_BASE
, '', RFC2396_BASE
)
223 self
.checkJoin(RFC2396_BASE
, '../../../g', 'http://a/../g')
224 self
.checkJoin(RFC2396_BASE
, '../../../../g', 'http://a/../../g')
225 self
.checkJoin(RFC2396_BASE
, '/./g', 'http://a/./g')
226 self
.checkJoin(RFC2396_BASE
, '/../g', 'http://a/../g')
227 self
.checkJoin(RFC2396_BASE
, 'g.', 'http://a/b/c/g.')
228 self
.checkJoin(RFC2396_BASE
, '.g', 'http://a/b/c/.g')
229 self
.checkJoin(RFC2396_BASE
, 'g..', 'http://a/b/c/g..')
230 self
.checkJoin(RFC2396_BASE
, '..g', 'http://a/b/c/..g')
231 self
.checkJoin(RFC2396_BASE
, './../g', 'http://a/b/g')
232 self
.checkJoin(RFC2396_BASE
, './g/.', 'http://a/b/c/g/')
233 self
.checkJoin(RFC2396_BASE
, 'g/./h', 'http://a/b/c/g/h')
234 self
.checkJoin(RFC2396_BASE
, 'g/../h', 'http://a/b/c/h')
235 self
.checkJoin(RFC2396_BASE
, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
236 self
.checkJoin(RFC2396_BASE
, 'g;x=1/../y', 'http://a/b/c/y')
237 self
.checkJoin(RFC2396_BASE
, 'g?y/./x', 'http://a/b/c/g?y/./x')
238 self
.checkJoin(RFC2396_BASE
, 'g?y/../x', 'http://a/b/c/g?y/../x')
239 self
.checkJoin(RFC2396_BASE
, 'g#s/./x', 'http://a/b/c/g#s/./x')
240 self
.checkJoin(RFC2396_BASE
, 'g#s/../x', 'http://a/b/c/g#s/../x')
242 def test_RFC3986(self
):
243 # Test cases from RFC3986
244 self
.checkJoin(RFC3986_BASE
, '?y','http://a/b/c/d;p?y')
245 self
.checkJoin(RFC2396_BASE
, ';x', 'http://a/b/c/;x')
246 self
.checkJoin(RFC3986_BASE
, 'g:h','g:h')
247 self
.checkJoin(RFC3986_BASE
, 'g','http://a/b/c/g')
248 self
.checkJoin(RFC3986_BASE
, './g','http://a/b/c/g')
249 self
.checkJoin(RFC3986_BASE
, 'g/','http://a/b/c/g/')
250 self
.checkJoin(RFC3986_BASE
, '/g','http://a/g')
251 self
.checkJoin(RFC3986_BASE
, '//g','http://g')
252 self
.checkJoin(RFC3986_BASE
, '?y','http://a/b/c/d;p?y')
253 self
.checkJoin(RFC3986_BASE
, 'g?y','http://a/b/c/g?y')
254 self
.checkJoin(RFC3986_BASE
, '#s','http://a/b/c/d;p?q#s')
255 self
.checkJoin(RFC3986_BASE
, 'g#s','http://a/b/c/g#s')
256 self
.checkJoin(RFC3986_BASE
, 'g?y#s','http://a/b/c/g?y#s')
257 self
.checkJoin(RFC3986_BASE
, ';x','http://a/b/c/;x')
258 self
.checkJoin(RFC3986_BASE
, 'g;x','http://a/b/c/g;x')
259 self
.checkJoin(RFC3986_BASE
, 'g;x?y#s','http://a/b/c/g;x?y#s')
260 self
.checkJoin(RFC3986_BASE
, '','http://a/b/c/d;p?q')
261 self
.checkJoin(RFC3986_BASE
, '.','http://a/b/c/')
262 self
.checkJoin(RFC3986_BASE
, './','http://a/b/c/')
263 self
.checkJoin(RFC3986_BASE
, '..','http://a/b/')
264 self
.checkJoin(RFC3986_BASE
, '../','http://a/b/')
265 self
.checkJoin(RFC3986_BASE
, '../g','http://a/b/g')
266 self
.checkJoin(RFC3986_BASE
, '../..','http://a/')
267 self
.checkJoin(RFC3986_BASE
, '../../','http://a/')
268 self
.checkJoin(RFC3986_BASE
, '../../g','http://a/g')
272 # The 'abnormal scenarios' are incompatible with RFC2986 parsing
273 # Tests are here for reference.
275 #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
276 #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
277 #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
278 #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
280 self
.checkJoin(RFC3986_BASE
, 'g.','http://a/b/c/g.')
281 self
.checkJoin(RFC3986_BASE
, '.g','http://a/b/c/.g')
282 self
.checkJoin(RFC3986_BASE
, 'g..','http://a/b/c/g..')
283 self
.checkJoin(RFC3986_BASE
, '..g','http://a/b/c/..g')
284 self
.checkJoin(RFC3986_BASE
, './../g','http://a/b/g')
285 self
.checkJoin(RFC3986_BASE
, './g/.','http://a/b/c/g/')
286 self
.checkJoin(RFC3986_BASE
, 'g/./h','http://a/b/c/g/h')
287 self
.checkJoin(RFC3986_BASE
, 'g/../h','http://a/b/c/h')
288 self
.checkJoin(RFC3986_BASE
, 'g;x=1/./y','http://a/b/c/g;x=1/y')
289 self
.checkJoin(RFC3986_BASE
, 'g;x=1/../y','http://a/b/c/y')
290 self
.checkJoin(RFC3986_BASE
, 'g?y/./x','http://a/b/c/g?y/./x')
291 self
.checkJoin(RFC3986_BASE
, 'g?y/../x','http://a/b/c/g?y/../x')
292 self
.checkJoin(RFC3986_BASE
, 'g#s/./x','http://a/b/c/g#s/./x')
293 self
.checkJoin(RFC3986_BASE
, 'g#s/../x','http://a/b/c/g#s/../x')
294 #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
295 self
.checkJoin(RFC3986_BASE
, 'http:g','http://a/b/c/g') # relaxed parser
297 def test_RFC2732(self
):
298 for url
, hostname
, port
in [
299 ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
300 ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
301 ('http://[::1]:5432/foo/', '::1', 5432),
302 ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
303 ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
304 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
305 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
306 ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
307 ('http://[::ffff:12.34.56.78]:5432/foo/',
308 '::ffff:12.34.56.78', 5432),
309 ('http://Test.python.org/foo/', 'test.python.org', None),
310 ('http://12.34.56.78/foo/', '12.34.56.78', None),
311 ('http://[::1]/foo/', '::1', None),
312 ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
313 ('http://[dead:beef::]/foo/', 'dead:beef::', None),
314 ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
315 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
316 ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
317 ('http://[::ffff:12.34.56.78]/foo/',
318 '::ffff:12.34.56.78', None),
320 urlparsed
= urlparse
.urlparse(url
)
321 self
.assertEqual((urlparsed
.hostname
, urlparsed
.port
) , (hostname
, port
))
324 'http://::12.34.56.78]/',
326 'ftp://[::1/foo/bad]/bad',
327 'http://[::1/foo/bad]/bad',
328 'http://[::ffff:12.34.56.78']:
329 self
.assertRaises(ValueError, urlparse
.urlparse
, invalid_url
)
331 def test_urldefrag(self
):
332 for url
, defrag
, frag
in [
333 ('http://python.org#frag', 'http://python.org', 'frag'),
334 ('http://python.org', 'http://python.org', ''),
335 ('http://python.org/#frag', 'http://python.org/', 'frag'),
336 ('http://python.org/', 'http://python.org/', ''),
337 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
338 ('http://python.org/?q', 'http://python.org/?q', ''),
339 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
340 ('http://python.org/p?q', 'http://python.org/p?q', ''),
341 (RFC1808_BASE
, 'http://a/b/c/d;p?q', 'f'),
342 (RFC2396_BASE
, 'http://a/b/c/d;p?q', ''),
344 self
.assertEqual(urlparse
.urldefrag(url
), (defrag
, frag
))
346 def test_urlsplit_attributes(self
):
347 url
= "HTTP://WWW.PYTHON.ORG/doc/#frag"
348 p
= urlparse
.urlsplit(url
)
349 self
.assertEqual(p
.scheme
, "http")
350 self
.assertEqual(p
.netloc
, "WWW.PYTHON.ORG")
351 self
.assertEqual(p
.path
, "/doc/")
352 self
.assertEqual(p
.query
, "")
353 self
.assertEqual(p
.fragment
, "frag")
354 self
.assertEqual(p
.username
, None)
355 self
.assertEqual(p
.password
, None)
356 self
.assertEqual(p
.hostname
, "www.python.org")
357 self
.assertEqual(p
.port
, None)
358 # geturl() won't return exactly the original URL in this case
359 # since the scheme is always case-normalized
360 #self.assertEqual(p.geturl(), url)
362 url
= "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
363 p
= urlparse
.urlsplit(url
)
364 self
.assertEqual(p
.scheme
, "http")
365 self
.assertEqual(p
.netloc
, "User:Pass@www.python.org:080")
366 self
.assertEqual(p
.path
, "/doc/")
367 self
.assertEqual(p
.query
, "query=yes")
368 self
.assertEqual(p
.fragment
, "frag")
369 self
.assertEqual(p
.username
, "User")
370 self
.assertEqual(p
.password
, "Pass")
371 self
.assertEqual(p
.hostname
, "www.python.org")
372 self
.assertEqual(p
.port
, 80)
373 self
.assertEqual(p
.geturl(), url
)
375 # Addressing issue1698, which suggests Username can contain
376 # "@" characters. Though not RFC compliant, many ftp sites allow
377 # and request email addresses as usernames.
379 url
= "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
380 p
= urlparse
.urlsplit(url
)
381 self
.assertEqual(p
.scheme
, "http")
382 self
.assertEqual(p
.netloc
, "User@example.com:Pass@www.python.org:080")
383 self
.assertEqual(p
.path
, "/doc/")
384 self
.assertEqual(p
.query
, "query=yes")
385 self
.assertEqual(p
.fragment
, "frag")
386 self
.assertEqual(p
.username
, "User@example.com")
387 self
.assertEqual(p
.password
, "Pass")
388 self
.assertEqual(p
.hostname
, "www.python.org")
389 self
.assertEqual(p
.port
, 80)
390 self
.assertEqual(p
.geturl(), url
)
393 def test_attributes_bad_port(self
):
394 """Check handling of non-integer ports."""
395 p
= urlparse
.urlsplit("http://www.example.net:foo")
396 self
.assertEqual(p
.netloc
, "www.example.net:foo")
397 self
.assertRaises(ValueError, lambda: p
.port
)
399 p
= urlparse
.urlparse("http://www.example.net:foo")
400 self
.assertEqual(p
.netloc
, "www.example.net:foo")
401 self
.assertRaises(ValueError, lambda: p
.port
)
403 def test_attributes_without_netloc(self
):
404 # This example is straight from RFC 3261. It looks like it
405 # should allow the username, hostname, and port to be filled
406 # in, but doesn't. Since it's a URI and doesn't use the
407 # scheme://netloc syntax, the netloc and related attributes
408 # should be left empty.
409 uri
= "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
410 p
= urlparse
.urlsplit(uri
)
411 self
.assertEqual(p
.netloc
, "")
412 self
.assertEqual(p
.username
, None)
413 self
.assertEqual(p
.password
, None)
414 self
.assertEqual(p
.hostname
, None)
415 self
.assertEqual(p
.port
, None)
416 self
.assertEqual(p
.geturl(), uri
)
418 p
= urlparse
.urlparse(uri
)
419 self
.assertEqual(p
.netloc
, "")
420 self
.assertEqual(p
.username
, None)
421 self
.assertEqual(p
.password
, None)
422 self
.assertEqual(p
.hostname
, None)
423 self
.assertEqual(p
.port
, None)
424 self
.assertEqual(p
.geturl(), uri
)
426 def test_caching(self
):
427 # Test case for bug #1313119
428 uri
= "http://example.com/doc/"
429 unicode_uri
= unicode(uri
)
431 urlparse
.urlparse(unicode_uri
)
432 p
= urlparse
.urlparse(uri
)
433 self
.assertEqual(type(p
.scheme
), type(uri
))
434 self
.assertEqual(type(p
.hostname
), type(uri
))
435 self
.assertEqual(type(p
.path
), type(uri
))
437 def test_noslash(self
):
438 # Issue 1637: http://foo.com?query is legal
439 self
.assertEqual(urlparse
.urlparse("http://example.com?blahblah=/foo"),
440 ('http', 'example.com', '', '', 'blahblah=/foo', ''))
442 def test_anyscheme(self
):
443 # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
444 self
.assertEqual(urlparse
.urlparse("s3://foo.com/stuff"),
445 ('s3','foo.com','/stuff','','',''))
446 self
.assertEqual(urlparse
.urlparse("x-newscheme://foo.com/stuff"),
447 ('x-newscheme','foo.com','/stuff','','',''))
452 test_support
.run_unittest(UrlParseTestCase
)
454 if __name__
== "__main__":