blob: 7d7f4588f6c96b7bc6c4226913a8d37353b19617 [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', '', '')),
28 ]:
Skip Montanaro6ec967d2002-03-23 05:32:10 +000029 result = urlparse.urlparse(url)
Fred Drake70705652002-10-16 21:02:36 +000030 self.assertEqual(result, parsed)
Neal Norwitz7dfb6e22002-09-25 19:20:12 +000031 # put it back together and it should be the same
32 result2 = urlparse.urlunparse(result)
33 self.assertEqual(result2, url)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +000034
Fred Drake70705652002-10-16 21:02:36 +000035 # check the roundtrip using urlsplit() as well
36 result = urlparse.urlsplit(url)
37 self.assertEqual(result, split)
38 result2 = urlparse.urlunsplit(result)
39 self.assertEqual(result2, url)
40
Skip Montanaro6ec967d2002-03-23 05:32:10 +000041 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +000042 self.assertEqual(urlparse.urljoin(base, relurl), expected,
43 (base, relurl, expected))
44
45 def test_unparse_parse(self):
46 for u in ['Python', './Python']:
Fred Drake70705652002-10-16 21:02:36 +000047 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
Guido van Rossumbbc05682002-10-14 19:59:54 +000048 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +000049
Skip Montanaro6ec967d2002-03-23 05:32:10 +000050 def test_RFC1808(self):
51 # "normal" cases from RFC 1808:
52 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
53 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
54 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
55 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
56 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
57 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
58 self.checkJoin(RFC1808_BASE, '?y', 'http://a/b/c/d;p?y')
59 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
60 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
61 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
62 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
63 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
64 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
65 self.checkJoin(RFC1808_BASE, ';x', 'http://a/b/c/d;x')
66 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
67 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
68 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
69 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
70 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
71 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
72 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
73 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
74 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
75 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +000076
Skip Montanaro6ec967d2002-03-23 05:32:10 +000077 # "abnormal" cases from RFC 1808:
78 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
79 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
80 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
81 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
82 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../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/b/c/g..')
86 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
87 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
88 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
89 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
90 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +000091
Skip Montanaro6ec967d2002-03-23 05:32:10 +000092 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
93 # so we'll not actually run these tests (which expect 1808 behavior).
94 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
95 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +000096
Skip Montanaro6ec967d2002-03-23 05:32:10 +000097 def test_RFC2396(self):
98 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +000099
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000100 ### urlparse.py as of v 1.32 fails on these two
101 #self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
102 #self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
103
104 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
105 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
106 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
107 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
108 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
109 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
110 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
111 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
112 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
113 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
114 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
115 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
116 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
117 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
118 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
119 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
120 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
121 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
122 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
123 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
124 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
125 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
126 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
127 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
128 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
129 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
130 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
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/g')
134 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
135 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
136 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
137 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
138 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
139 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
140 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
141 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
142 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
143
Fred Drake70705652002-10-16 21:02:36 +0000144 def test_urldefrag(self):
145 for url, defrag, frag in [
146 ('http://python.org#frag', 'http://python.org', 'frag'),
147 ('http://python.org', 'http://python.org', ''),
148 ('http://python.org/#frag', 'http://python.org/', 'frag'),
149 ('http://python.org/', 'http://python.org/', ''),
150 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
151 ('http://python.org/?q', 'http://python.org/?q', ''),
152 ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
153 ('http://python.org/p?q', 'http://python.org/p?q', ''),
154 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
155 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
156 ]:
157 self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
158
Skip Montanaro6ec967d2002-03-23 05:32:10 +0000159def test_main():
160 test_support.run_unittest(UrlParseTestCase)
161
162if __name__ == "__main__":
163 test_main()