blob: 8932b3c7be2402c4e044e50e0743628625594040 [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):
11 def test_frags(self):
Fred Drake70705652002-10-16 21:02:36 +000012 for url, parsed, split in [
13 ('http://www.python.org',
14 ('http', 'www.python.org', '', '', '', ''),
15 ('http', 'www.python.org', '', '', '')),
16 ('http://www.python.org#abc',
17 ('http', 'www.python.org', '', '', '', 'abc'),
18 ('http', 'www.python.org', '', '', 'abc')),
19 ('http://www.python.org/#abc',
20 ('http', 'www.python.org', '/', '', '', 'abc'),
21 ('http', 'www.python.org', '/', '', 'abc')),
22 (RFC1808_BASE,
23 ('http', 'a', '/b/c/d', 'p', 'q', 'f'),
24 ('http', 'a', '/b/c/d;p', 'q', 'f')),
25 ('file:///tmp/junk.txt',
26 ('file', '', '/tmp/junk.txt', '', '', ''),
27 ('file', '', '/tmp/junk.txt', '', '')),
Neal Norwitz68b539e2003-01-06 06:58:31 +000028 ('imap://mail.python.org/mbox1',
29 ('imap', 'mail.python.org', '/mbox1', '', '', ''),
30 ('imap', 'mail.python.org', '/mbox1', '', '')),
Skip Montanarof09b88e2003-01-06 20:27:03 +000031 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
32 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', '', '', ''),
33 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', '', '')),
Fred Drake70705652002-10-16 21:02:36 +000034 ]:
Skip Montanaro6ec967d2002-03-23 05:32:10 +000035 result = urlparse.urlparse(url)
Fred Drake70705652002-10-16 21:02:36 +000036 self.assertEqual(result, parsed)
Neal Norwitz7dfb6e22002-09-25 19:20:12 +000037 # put it back together and it should be the same
38 result2 = urlparse.urlunparse(result)
39 self.assertEqual(result2, url)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +000040
Fred Drake70705652002-10-16 21:02:36 +000041 # check the roundtrip using urlsplit() as well
42 result = urlparse.urlsplit(url)
43 self.assertEqual(result, split)
44 result2 = urlparse.urlunsplit(result)
45 self.assertEqual(result2, url)
46
Skip Montanaro6ec967d2002-03-23 05:32:10 +000047 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +000048 self.assertEqual(urlparse.urljoin(base, relurl), expected,
49 (base, relurl, expected))
50
51 def test_unparse_parse(self):
52 for u in ['Python', './Python']:
Fred Drake70705652002-10-16 21:02:36 +000053 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +000054 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +000055
Skip Montanaro6ec967d2002-03-23 05:32:10 +000056 def test_RFC1808(self):
57 # "normal" cases from RFC 1808:
58 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
59 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
60 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
61 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
62 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
63 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
Skip Montanaro6ec967d2002-03-23 05:32:10 +000064 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
65 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
66 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
67 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
68 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
69 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
Skip Montanaro6ec967d2002-03-23 05:32:10 +000070 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
71 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
72 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
73 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
74 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
75 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
76 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
77 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
78 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
79 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +000080
Skip Montanaro6ec967d2002-03-23 05:32:10 +000081 # "abnormal" cases from RFC 1808:
82 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
83 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
84 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
85 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
86 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
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/b/c/..g')
91 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
92 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
93 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
94 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +000095
Skip Montanaro6ec967d2002-03-23 05:32:10 +000096 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
97 # so we'll not actually run these tests (which expect 1808 behavior).
98 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
99 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +0000100
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000101 def test_RFC2396(self):
102 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +0000103
Brett Cannon82860df2003-10-12 04:29:10 +0000104 self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
105 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000106
107 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
108 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
109 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
110 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
111 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
112 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
113 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
114 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
115 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
116 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
117 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
118 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
119 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
120 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
121 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
122 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
123 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
124 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
125 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
126 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
127 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
128 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
129 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
130 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
131 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../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/b/c/g..')
135 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
136 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
137 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
138 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
139 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
140 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
141 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
142 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
143 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
144 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
145 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
146
Fred Drake70705652002-10-16 21:02:36 +0000147 def test_urldefrag(self):
148 for url, defrag, frag in [
149 ('http://python.org#frag', 'http://python.org', 'frag'),
150 ('http://python.org', 'http://python.org', ''),
151 ('http://python.org/#frag', 'http://python.org/', 'frag'),
152 ('http://python.org/', 'http://python.org/', ''),
153 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
154 ('http://python.org/?q', 'http://python.org/?q', ''),
155 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
156 ('http://python.org/p?q', 'http://python.org/p?q', ''),
157 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
158 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
159 ]:
160 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
161
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000162def test_main():
163 test_support.run_unittest(UrlParseTestCase)
164
165if __name__ == "__main__":
166 test_main()