2002-03-23 01:32:10 -04:00
|
|
|
#! /usr/bin/env python
|
2001-01-05 01:57:04 -04:00
|
|
|
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2002-03-23 01:32:10 -04:00
|
|
|
import unittest
|
|
|
|
import urlparse
|
2001-01-05 01:57:04 -04:00
|
|
|
|
|
|
|
RFC1808_BASE = "http://a/b/c/d;p?q#f"
|
2002-03-23 01:32:10 -04:00
|
|
|
RFC2396_BASE = "http://a/b/c/d;p?q"
|
|
|
|
|
|
|
|
class UrlParseTestCase(unittest.TestCase):
|
2005-01-09 11:29:10 -04:00
|
|
|
|
|
|
|
def checkRoundtrips(self, url, parsed, split):
|
|
|
|
result = urlparse.urlparse(url)
|
|
|
|
self.assertEqual(result, parsed)
|
|
|
|
# put it back together and it should be the same
|
|
|
|
result2 = urlparse.urlunparse(result)
|
|
|
|
self.assertEqual(result2, url)
|
|
|
|
|
|
|
|
# check the roundtrip using urlsplit() as well
|
|
|
|
result = urlparse.urlsplit(url)
|
|
|
|
self.assertEqual(result, split)
|
|
|
|
result2 = urlparse.urlunsplit(result)
|
|
|
|
self.assertEqual(result2, url)
|
|
|
|
|
|
|
|
def test_roundtrips(self):
|
|
|
|
testcases = [
|
2002-10-16 18:02:36 -03:00
|
|
|
('file:///tmp/junk.txt',
|
|
|
|
('file', '', '/tmp/junk.txt', '', '', ''),
|
|
|
|
('file', '', '/tmp/junk.txt', '', '')),
|
2003-01-06 02:58:31 -04:00
|
|
|
('imap://mail.python.org/mbox1',
|
|
|
|
('imap', 'mail.python.org', '/mbox1', '', '', ''),
|
|
|
|
('imap', 'mail.python.org', '/mbox1', '', '')),
|
2003-01-06 16:27:03 -04:00
|
|
|
('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
|
2005-01-09 11:29:10 -04:00
|
|
|
('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
|
|
|
|
'', '', ''),
|
|
|
|
('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
|
|
|
|
'', '')),
|
|
|
|
]
|
|
|
|
for url, parsed, split in testcases:
|
|
|
|
self.checkRoundtrips(url, parsed, split)
|
|
|
|
|
|
|
|
def test_http_roundtrips(self):
|
|
|
|
# urlparse.urlsplit treats 'http:' as an optimized special case,
|
|
|
|
# so we test both 'http:' and 'https:' in all the following.
|
|
|
|
# Three cheers for white box knowledge!
|
|
|
|
testcases = [
|
|
|
|
('://www.python.org',
|
|
|
|
('www.python.org', '', '', '', ''),
|
|
|
|
('www.python.org', '', '', '')),
|
|
|
|
('://www.python.org#abc',
|
|
|
|
('www.python.org', '', '', '', 'abc'),
|
|
|
|
('www.python.org', '', '', 'abc')),
|
|
|
|
('://www.python.org?q=abc',
|
|
|
|
('www.python.org', '', '', 'q=abc', ''),
|
|
|
|
('www.python.org', '', 'q=abc', '')),
|
|
|
|
('://www.python.org/#abc',
|
|
|
|
('www.python.org', '/', '', '', 'abc'),
|
|
|
|
('www.python.org', '/', '', 'abc')),
|
|
|
|
('://a/b/c/d;p?q#f',
|
|
|
|
('a', '/b/c/d', 'p', 'q', 'f'),
|
|
|
|
('a', '/b/c/d;p', 'q', 'f')),
|
|
|
|
]
|
|
|
|
for scheme in ('http', 'https'):
|
|
|
|
for url, parsed, split in testcases:
|
|
|
|
url = scheme + url
|
|
|
|
parsed = (scheme,) + parsed
|
|
|
|
split = (scheme,) + split
|
|
|
|
self.checkRoundtrips(url, parsed, split)
|
2002-10-16 18:02:36 -03:00
|
|
|
|
2002-03-23 01:32:10 -04:00
|
|
|
def checkJoin(self, base, relurl, expected):
|
2002-10-14 16:59:54 -03:00
|
|
|
self.assertEqual(urlparse.urljoin(base, relurl), expected,
|
|
|
|
(base, relurl, expected))
|
|
|
|
|
|
|
|
def test_unparse_parse(self):
|
|
|
|
for u in ['Python', './Python']:
|
2002-10-16 18:02:36 -03:00
|
|
|
self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
|
2002-10-14 16:59:54 -03:00
|
|
|
self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
|
2002-03-23 01:32:10 -04:00
|
|
|
|
|
|
|
def test_RFC1808(self):
|
|
|
|
# "normal" cases from RFC 1808:
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
|
|
|
|
self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
|
|
|
|
self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
|
|
|
|
self.checkJoin(RFC1808_BASE, '//g', 'http://g')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
|
|
|
|
self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
|
|
|
|
self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
|
|
|
|
self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
|
|
|
|
self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
|
|
|
|
self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
|
|
|
|
self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
|
|
|
|
self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
|
|
|
|
self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
|
|
|
|
self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
|
|
|
|
|
|
|
|
# "abnormal" cases from RFC 1808:
|
|
|
|
self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
|
|
|
|
self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
|
|
|
|
self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
|
|
|
|
self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
|
|
|
|
self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
|
|
|
|
self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
|
|
|
|
self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
|
|
|
|
self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
|
|
|
|
self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
|
|
|
|
self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
|
2001-01-05 01:57:04 -04:00
|
|
|
|
2002-03-23 01:32:10 -04:00
|
|
|
# RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
|
|
|
|
# so we'll not actually run these tests (which expect 1808 behavior).
|
|
|
|
#self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
|
|
|
|
#self.checkJoin(RFC1808_BASE, 'http:', 'http:')
|
2002-03-18 09:06:00 -04:00
|
|
|
|
2002-03-23 01:32:10 -04:00
|
|
|
def test_RFC2396(self):
|
|
|
|
# cases from RFC 2396
|
2001-01-05 01:57:04 -04:00
|
|
|
|
2003-10-12 01:29:10 -03:00
|
|
|
self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
|
|
|
|
self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
|
2001-01-05 01:57:04 -04:00
|
|
|
|
2002-03-23 01:32:10 -04:00
|
|
|
self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
|
|
|
|
self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
|
|
|
|
self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
|
|
|
|
self.checkJoin(RFC2396_BASE, '//g', 'http://g')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
|
|
|
|
self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
|
|
|
|
self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
|
|
|
|
self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
|
|
|
|
self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
|
|
|
|
self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
|
|
|
|
self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
|
|
|
|
self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
|
|
|
|
self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
|
|
|
|
self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
|
|
|
|
self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
|
|
|
|
self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
|
|
|
|
self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
|
|
|
|
self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
|
|
|
|
self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
|
|
|
|
self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
|
|
|
|
self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
|
|
|
|
self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
|
|
|
|
self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
|
|
|
|
self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
|
2001-01-05 01:57:04 -04:00
|
|
|
|
2002-10-16 18:02:36 -03:00
|
|
|
def test_urldefrag(self):
|
|
|
|
for url, defrag, frag in [
|
|
|
|
('http://python.org#frag', 'http://python.org', 'frag'),
|
|
|
|
('http://python.org', 'http://python.org', ''),
|
|
|
|
('http://python.org/#frag', 'http://python.org/', 'frag'),
|
|
|
|
('http://python.org/', 'http://python.org/', ''),
|
|
|
|
('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
|
|
|
|
('http://python.org/?q', 'http://python.org/?q', ''),
|
|
|
|
('http://python.org/p#frag', 'http://python.org/p', 'frag'),
|
|
|
|
('http://python.org/p?q', 'http://python.org/p?q', ''),
|
|
|
|
(RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
|
|
|
|
(RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
|
|
|
|
]:
|
|
|
|
self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
|
|
|
|
|
2002-03-23 01:32:10 -04:00
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(UrlParseTestCase)
|
2001-01-05 01:57:04 -04:00
|
|
|
|
2002-03-23 01:32:10 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|