3 from test
import test_support
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 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')),
19 ('http', 'a', '/b/c/d', 'p', 'q', 'f')),
20 ('file:///tmp/junk.txt',
21 ('file', '', '/tmp/junk.txt', '', '', '')),
23 result
= urlparse
.urlparse(url
)
24 self
.assertEqual(result
, expected
)
25 # put it back together and it should be the same
26 result2
= urlparse
.urlunparse(result
)
27 self
.assertEqual(result2
, url
)
29 def checkJoin(self
, base
, relurl
, expected
):
30 self
.assertEqual(urlparse
.urljoin(base
, relurl
), expected
)
32 def test_RFC1808(self
):
33 # "normal" cases from RFC 1808:
34 self
.checkJoin(RFC1808_BASE
, 'g:h', 'g:h')
35 self
.checkJoin(RFC1808_BASE
, 'g', 'http://a/b/c/g')
36 self
.checkJoin(RFC1808_BASE
, './g', 'http://a/b/c/g')
37 self
.checkJoin(RFC1808_BASE
, 'g/', 'http://a/b/c/g/')
38 self
.checkJoin(RFC1808_BASE
, '/g', 'http://a/g')
39 self
.checkJoin(RFC1808_BASE
, '//g', 'http://g')
40 self
.checkJoin(RFC1808_BASE
, '?y', 'http://a/b/c/d;p?y')
41 self
.checkJoin(RFC1808_BASE
, 'g?y', 'http://a/b/c/g?y')
42 self
.checkJoin(RFC1808_BASE
, 'g?y/./x', 'http://a/b/c/g?y/./x')
43 self
.checkJoin(RFC1808_BASE
, '#s', 'http://a/b/c/d;p?q#s')
44 self
.checkJoin(RFC1808_BASE
, 'g#s', 'http://a/b/c/g#s')
45 self
.checkJoin(RFC1808_BASE
, 'g#s/./x', 'http://a/b/c/g#s/./x')
46 self
.checkJoin(RFC1808_BASE
, 'g?y#s', 'http://a/b/c/g?y#s')
47 self
.checkJoin(RFC1808_BASE
, ';x', 'http://a/b/c/d;x')
48 self
.checkJoin(RFC1808_BASE
, 'g;x', 'http://a/b/c/g;x')
49 self
.checkJoin(RFC1808_BASE
, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
50 self
.checkJoin(RFC1808_BASE
, '.', 'http://a/b/c/')
51 self
.checkJoin(RFC1808_BASE
, './', 'http://a/b/c/')
52 self
.checkJoin(RFC1808_BASE
, '..', 'http://a/b/')
53 self
.checkJoin(RFC1808_BASE
, '../', 'http://a/b/')
54 self
.checkJoin(RFC1808_BASE
, '../g', 'http://a/b/g')
55 self
.checkJoin(RFC1808_BASE
, '../..', 'http://a/')
56 self
.checkJoin(RFC1808_BASE
, '../../', 'http://a/')
57 self
.checkJoin(RFC1808_BASE
, '../../g', 'http://a/g')
59 # "abnormal" cases from RFC 1808:
60 self
.checkJoin(RFC1808_BASE
, '', 'http://a/b/c/d;p?q#f')
61 self
.checkJoin(RFC1808_BASE
, '../../../g', 'http://a/../g')
62 self
.checkJoin(RFC1808_BASE
, '../../../../g', 'http://a/../../g')
63 self
.checkJoin(RFC1808_BASE
, '/./g', 'http://a/./g')
64 self
.checkJoin(RFC1808_BASE
, '/../g', 'http://a/../g')
65 self
.checkJoin(RFC1808_BASE
, 'g.', 'http://a/b/c/g.')
66 self
.checkJoin(RFC1808_BASE
, '.g', 'http://a/b/c/.g')
67 self
.checkJoin(RFC1808_BASE
, 'g..', 'http://a/b/c/g..')
68 self
.checkJoin(RFC1808_BASE
, '..g', 'http://a/b/c/..g')
69 self
.checkJoin(RFC1808_BASE
, './../g', 'http://a/b/g')
70 self
.checkJoin(RFC1808_BASE
, './g/.', 'http://a/b/c/g/')
71 self
.checkJoin(RFC1808_BASE
, 'g/./h', 'http://a/b/c/g/h')
72 self
.checkJoin(RFC1808_BASE
, 'g/../h', 'http://a/b/c/h')
74 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
75 # so we'll not actually run these tests (which expect 1808 behavior).
76 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
77 #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
79 def test_RFC2396(self
):
82 ### urlparse.py as of v 1.32 fails on these two
83 #self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
84 #self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
86 self
.checkJoin(RFC2396_BASE
, 'g:h', 'g:h')
87 self
.checkJoin(RFC2396_BASE
, 'g', 'http://a/b/c/g')
88 self
.checkJoin(RFC2396_BASE
, './g', 'http://a/b/c/g')
89 self
.checkJoin(RFC2396_BASE
, 'g/', 'http://a/b/c/g/')
90 self
.checkJoin(RFC2396_BASE
, '/g', 'http://a/g')
91 self
.checkJoin(RFC2396_BASE
, '//g', 'http://g')
92 self
.checkJoin(RFC2396_BASE
, 'g?y', 'http://a/b/c/g?y')
93 self
.checkJoin(RFC2396_BASE
, '#s', 'http://a/b/c/d;p?q#s')
94 self
.checkJoin(RFC2396_BASE
, 'g#s', 'http://a/b/c/g#s')
95 self
.checkJoin(RFC2396_BASE
, 'g?y#s', 'http://a/b/c/g?y#s')
96 self
.checkJoin(RFC2396_BASE
, 'g;x', 'http://a/b/c/g;x')
97 self
.checkJoin(RFC2396_BASE
, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
98 self
.checkJoin(RFC2396_BASE
, '.', 'http://a/b/c/')
99 self
.checkJoin(RFC2396_BASE
, './', 'http://a/b/c/')
100 self
.checkJoin(RFC2396_BASE
, '..', 'http://a/b/')
101 self
.checkJoin(RFC2396_BASE
, '../', 'http://a/b/')
102 self
.checkJoin(RFC2396_BASE
, '../g', 'http://a/b/g')
103 self
.checkJoin(RFC2396_BASE
, '../..', 'http://a/')
104 self
.checkJoin(RFC2396_BASE
, '../../', 'http://a/')
105 self
.checkJoin(RFC2396_BASE
, '../../g', 'http://a/g')
106 self
.checkJoin(RFC2396_BASE
, '', RFC2396_BASE
)
107 self
.checkJoin(RFC2396_BASE
, '../../../g', 'http://a/../g')
108 self
.checkJoin(RFC2396_BASE
, '../../../../g', 'http://a/../../g')
109 self
.checkJoin(RFC2396_BASE
, '/./g', 'http://a/./g')
110 self
.checkJoin(RFC2396_BASE
, '/../g', 'http://a/../g')
111 self
.checkJoin(RFC2396_BASE
, 'g.', 'http://a/b/c/g.')
112 self
.checkJoin(RFC2396_BASE
, '.g', 'http://a/b/c/.g')
113 self
.checkJoin(RFC2396_BASE
, 'g..', 'http://a/b/c/g..')
114 self
.checkJoin(RFC2396_BASE
, '..g', 'http://a/b/c/..g')
115 self
.checkJoin(RFC2396_BASE
, './../g', 'http://a/b/g')
116 self
.checkJoin(RFC2396_BASE
, './g/.', 'http://a/b/c/g/')
117 self
.checkJoin(RFC2396_BASE
, 'g/./h', 'http://a/b/c/g/h')
118 self
.checkJoin(RFC2396_BASE
, 'g/../h', 'http://a/b/c/h')
119 self
.checkJoin(RFC2396_BASE
, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
120 self
.checkJoin(RFC2396_BASE
, 'g;x=1/../y', 'http://a/b/c/y')
121 self
.checkJoin(RFC2396_BASE
, 'g?y/./x', 'http://a/b/c/g?y/./x')
122 self
.checkJoin(RFC2396_BASE
, 'g?y/../x', 'http://a/b/c/g?y/../x')
123 self
.checkJoin(RFC2396_BASE
, 'g#s/./x', 'http://a/b/c/g#s/./x')
124 self
.checkJoin(RFC2396_BASE
, 'g#s/../x', 'http://a/b/c/g#s/../x')
127 test_support
.run_unittest(UrlParseTestCase
)
129 if __name__
== "__main__":