blob: 39f50e4ac074fb678b2c84c7aa9814ac219bee62 [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):
12 for url, expected in [('http://www.python.org',
13 ('http', 'www.python.org', '', '', '', '')),
14 ('http://www.python.org#abc',
15 ('http', 'www.python.org', '', '', '', 'abc')),
16 ('http://www.python.org/#abc',
17 ('http', 'www.python.org', '/', '', '', 'abc')),
18 (RFC1808_BASE,
19 ('http', 'a', '/b/c/d', 'p', 'q', 'f')),
Neal Norwitz7dfb6e22002-09-25 19:20:12 +000020 ('file:///tmp/junk.txt',
21 ('file', '', '/tmp/junk.txt', '', '', '')),
Skip Montanaro6ec967d2002-03-23 05:32:10 +000022 ]:
23 result = urlparse.urlparse(url)
24 self.assertEqual(result, expected)
Neal Norwitz7dfb6e22002-09-25 19:20:12 +000025 # put it back together and it should be the same
26 result2 = urlparse.urlunparse(result)
27 self.assertEqual(result2, url)
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +000028
Skip Montanaro6ec967d2002-03-23 05:32:10 +000029 def checkJoin(self, base, relurl, expected):
Guido van Rossumbbc05682002-10-14 19:59:54 +000030 self.assertEqual(urlparse.urljoin(base, relurl), expected,
31 (base, relurl, expected))
32
33 def test_unparse_parse(self):
34 for u in ['Python', './Python']:
35 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
Fred Drakea4d18a02001-01-05 05:57:04 +000036
Skip Montanaro6ec967d2002-03-23 05:32:10 +000037 def test_RFC1808(self):
38 # "normal" cases from RFC 1808:
39 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
40 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
41 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
42 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
43 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
44 self.checkJoin(RFC1808_BASE, '//g', 'http://g')
45 self.checkJoin(RFC1808_BASE, '?y', 'http://a/b/c/d;p?y')
46 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
47 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
48 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
49 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
50 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
51 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
52 self.checkJoin(RFC1808_BASE, ';x', 'http://a/b/c/d;x')
53 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
54 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
55 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
56 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
57 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
58 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
59 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
60 self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
61 self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
62 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
Fred Drakea4d18a02001-01-05 05:57:04 +000063
Skip Montanaro6ec967d2002-03-23 05:32:10 +000064 # "abnormal" cases from RFC 1808:
65 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
66 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
67 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
68 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
69 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
70 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
71 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
72 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
73 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
74 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
75 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
76 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
77 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
Fred Drakea4d18a02001-01-05 05:57:04 +000078
Skip Montanaro6ec967d2002-03-23 05:32:10 +000079 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
80 # so we'll not actually run these tests (which expect 1808 behavior).
81 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
82 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
Fred Drakea4d18a02001-01-05 05:57:04 +000083
Skip Montanaro6ec967d2002-03-23 05:32:10 +000084 def test_RFC2396(self):
85 # cases from RFC 2396
Fred Drakea4d18a02001-01-05 05:57:04 +000086
Skip Montanaro6ec967d2002-03-23 05:32:10 +000087 ### urlparse.py as of v 1.32 fails on these two
88 #self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
89 #self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
90
91 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
92 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
93 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
94 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
95 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
96 self.checkJoin(RFC2396_BASE, '//g', 'http://g')
97 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
98 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
99 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
100 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
101 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
102 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
103 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
104 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
105 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
106 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
107 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
108 self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
109 self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
110 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
111 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
112 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
113 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
114 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
115 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
116 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
117 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
118 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
119 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
120 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
121 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
122 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
123 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
124 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
125 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
126 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
127 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
128 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
129 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
130
131def test_main():
132 test_support.run_unittest(UrlParseTestCase)
133
134if __name__ == "__main__":
135 test_main()