Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Lib / test / test_urlparse.py
blob39ada0692566b18e521aa75d9dce315de87fc1a0
1 #! /usr/bin/env python
3 from test import test_support
4 import unittest
5 import urlparse
7 RFC1808_BASE = "http://a/b/c/d;p?q#f"
8 RFC2396_BASE = "http://a/b/c/d;p?q"
10 class UrlParseTestCase(unittest.TestCase):
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)
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)
25 def test_roundtrips(self):
26 testcases = [
27 ('file:///tmp/junk.txt',
28 ('file', '', '/tmp/junk.txt', '', '', ''),
29 ('file', '', '/tmp/junk.txt', '', '')),
30 ('imap://mail.python.org/mbox1',
31 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
32 ('imap', 'mail.python.org', '/mbox1', '', '')),
33 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
34 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
35 '', '', ''),
36 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
37 '', '')),
38 ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
39 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
40 '', '', ''),
41 ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
42 '', ''))
44 for url, parsed, split in testcases:
45 self.checkRoundtrips(url, parsed, split)
47 def test_http_roundtrips(self):
48 # urlparse.urlsplit treats 'http:' as an optimized special case,
49 # so we test both 'http:' and 'https:' in all the following.
50 # Three cheers for white box knowledge!
51 testcases = [
52 ('://www.python.org',
53 ('www.python.org', '', '', '', ''),
54 ('www.python.org', '', '', '')),
55 ('://www.python.org#abc',
56 ('www.python.org', '', '', '', 'abc'),
57 ('www.python.org', '', '', 'abc')),
58 ('://www.python.org?q=abc',
59 ('www.python.org', '', '', 'q=abc', ''),
60 ('www.python.org', '', 'q=abc', '')),
61 ('://www.python.org/#abc',
62 ('www.python.org', '/', '', '', 'abc'),
63 ('www.python.org', '/', '', 'abc')),
64 ('://a/b/c/d;p?q#f',
65 ('a', '/b/c/d', 'p', 'q', 'f'),
66 ('a', '/b/c/d;p', 'q', 'f')),
68 for scheme in ('http', 'https'):
69 for url, parsed, split in testcases:
70 url = scheme + url
71 parsed = (scheme,) + parsed
72 split = (scheme,) + split
73 self.checkRoundtrips(url, parsed, split)
75 def checkJoin(self, base, relurl, expected):
76 self.assertEqual(urlparse.urljoin(base, relurl), expected,
77 (base, relurl, expected))
79 def test_unparse_parse(self):
80 for u in ['Python', './Python']:
81 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
82 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
84 def test_RFC1808(self):
85 # "normal" cases from RFC 1808:
86 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
87 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
88 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
89 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
90 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
91 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
92 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
93 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
94 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
95 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
96 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
97 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
98 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
99 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
100 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
101 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
102 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
103 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
104 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
105 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
106 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
107 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
109 # "abnormal" cases from RFC 1808:
110 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
111 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
112 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
113 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
114 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
115 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
116 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
117 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
118 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
119 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
120 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
121 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
122 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
124 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
125 # so we'll not actually run these tests (which expect 1808 behavior).
126 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
127 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
129 def test_RFC2396(self):
130 # cases from RFC 2396
132 self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
133 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
135 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
136 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
137 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
138 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
139 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
140 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
141 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
142 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
143 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
144 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
145 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
146 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
147 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
148 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
149 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
150 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
151 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
152 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
153 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
154 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
155 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
156 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
157 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
158 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
159 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
160 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
161 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
162 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
163 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
164 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
165 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
166 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
167 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
168 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
169 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
170 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
171 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
172 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
173 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
175 def test_urldefrag(self):
176 for url, defrag, frag in [
177 ('http://python.org#frag', 'http://python.org', 'frag'),
178 ('http://python.org', 'http://python.org', ''),
179 ('http://python.org/#frag', 'http://python.org/', 'frag'),
180 ('http://python.org/', 'http://python.org/', ''),
181 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
182 ('http://python.org/?q', 'http://python.org/?q', ''),
183 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
184 ('http://python.org/p?q', 'http://python.org/p?q', ''),
185 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
186 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
188 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
190 def test_main():
191 test_support.run_unittest(UrlParseTestCase)
193 if __name__ == "__main__":
194 test_main()