blob: 04572ba60df9b36dba8faed54f7a71f8e2c76c30 [file] [log] [blame]
Skip Montanaro6ec967d2002-03-23 05:32:10 +00001#! /usr/bin/env python
2
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
Skip Montanaro6ec967d2002-03-23 05:32:10 +00004import unittest
Fred Drakea4d18a02001-01-05 05:57:04 +00005import urlparse
6
Fred Drakea4d18a02001-01-05 05:57:04 +00007RFC1808_BASE = "http://a/b/c/d;p?q#f"
Skip Montanaro6ec967d2002-03-23 05:32:10 +00008RFC2396_BASE = "http://a/b/c/d;p?q"
Fred Drakea4d18a02001-01-05 05:57:04 +00009
Skip Montanaro6ec967d2002-03-23 05:32:10 +000010class UrlParseTestCase(unittest.TestCase):
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000011
12 def checkRoundtrips(self, url, parsed, split):
13 result = urlparse.urlparse(url)
14 self.assertEqual(result, parsed)
15 # put it back together and it should be the same
16 result2 = urlparse.urlunparse(result)
17 self.assertEqual(result2, url)
18
19 # check the roundtrip using urlsplit() as well
20 result = urlparse.urlsplit(url)
21 self.assertEqual(result, split)
22 result2 = urlparse.urlunsplit(result)
23 self.assertEqual(result2, url)
24
25 def test_roundtrips(self):
26 testcases = [
Fred Drake70705652002-10-16 21:02:36 +000027 ('file:///tmp/junk.txt',
28 ('file', '', '/tmp/junk.txt', '', '', ''),
29 ('file', '', '/tmp/junk.txt', '', '')),
Neal Norwitz68b539e2003-01-06 06:58:31 +000030 ('imap://mail.python.org/mbox1',
31 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
32 ('imap', 'mail.python.org', '/mbox1', '', '')),
Skip Montanarof09b88e2003-01-06 20:27:03 +000033 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000034 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
35 '', '', ''),
36 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
37 '', '')),
38 ]
39 for url, parsed, split in testcases:
40 self.checkRoundtrips(url, parsed, split)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +000041
Johannes Gijsbers41e4faa2005-01-09 15:29:10 +000042 def test_http_roundtrips(self):
43 # urlparse.urlsplit treats 'http:' as an optimized special case,
44 # so we test both 'http:' and 'https:' in all the following.
45 # Three cheers for white box knowledge!
46 testcases = [
47 ('://www.python.org',
48 ('www.python.org', '', '', '', ''),
49 ('www.python.org', '', '', '')),
50 ('://www.python.org#abc',
51 ('www.python.org', '', '', '', 'abc'),
52 ('www.python.org', '', '', 'abc')),
53 ('://www.python.org?q=abc',
54 ('www.python.org', '', '', 'q=abc', ''),
55 ('www.python.org', '', 'q=abc', '')),
56 ('://www.python.org/#abc',
57 ('www.python.org', '/', '', '', 'abc'),
58 ('www.python.org', '/', '', 'abc')),
59 ('://a/b/c/d;p?q#f',
60 ('a', '/b/c/d', 'p', 'q', 'f'),
61 ('a', '/b/c/d;p', 'q', 'f')),
62 ]
63 for scheme in ('http', 'https'):
64 for url, parsed, split in testcases:
65 url = scheme + url
66 parsed = (scheme,) + parsed
67 split = (scheme,) + split
68 self.checkRoundtrips(url, parsed, split)
Fred Drake70705652002-10-16 21:02:36 +000069
Skip Montanaro6ec967d2002-03-23 05:32:10 +000070 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +000071 self.assertEqual(urlparse.urljoin(base, relurl), expected,
72 (base, relurl, expected))
73
74 def test_unparse_parse(self):
75 for u in ['Python', './Python']:
Fred Drake70705652002-10-16 21:02:36 +000076 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +000077 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +000078
Skip Montanaro6ec967d2002-03-23 05:32:10 +000079 def test_RFC1808(self):
80 # "normal" cases from RFC 1808:
81 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
82 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
83 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
84 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
85 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
86 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +000087 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
88 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
89 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
90 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
91 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
92 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +000093 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
94 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
95 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
96 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
97 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
98 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
99 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
100 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
101 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
102 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +0000103
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000104 # "abnormal" cases from RFC 1808:
105 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
106 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
107 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
108 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
109 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
110 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
111 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
112 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
113 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
114 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
115 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
116 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
117 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +0000118
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000119 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
120 # so we'll not actually run these tests (which expect 1808 behavior).
121 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
122 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000123
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000124 def test_RFC2396(self):
125 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +0000126
Brett Cannon82860df2003-10-12 04:29:10 +0000127 self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
128 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000129
130 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
131 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
132 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
133 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
134 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
135 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
136 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
137 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
138 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
139 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
140 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
141 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
142 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
143 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
144 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
145 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
146 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
147 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
148 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
149 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
150 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
151 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
152 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
153 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
154 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
155 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
156 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
157 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
158 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
159 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
160 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
161 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
162 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
163 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
164 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
165 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
166 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
167 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
168 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
169
Fred Drake70705652002-10-16 21:02:36 +0000170 def test_urldefrag(self):
171 for url, defrag, frag in [
172 ('http://python.org#frag', 'http://python.org', 'frag'),
173 ('http://python.org', 'http://python.org', ''),
174 ('http://python.org/#frag', 'http://python.org/', 'frag'),
175 ('http://python.org/', 'http://python.org/', ''),
176 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
177 ('http://python.org/?q', 'http://python.org/?q', ''),
178 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
179 ('http://python.org/p?q', 'http://python.org/p?q', ''),
180 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
181 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
182 ]:
183 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
184
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000185def test_main():
186 test_support.run_unittest(UrlParseTestCase)
187
188if __name__ == "__main__":
189 test_main()