Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 4 | import unittest |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 5 | import urllib.parse |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 6 | |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 7 | RFC1808_BASE = "http://a/b/c/d;p?q#f" |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 8 | RFC2396_BASE = "http://a/b/c/d;p?q" |
Senthil Kumaran | 06f29cd | 2010-05-07 04:24:30 +0000 | [diff] [blame] | 9 | RFC3986_BASE = 'http://a/b/c/d;p?q' |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 10 | |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 11 | # A list of test cases. Each test case is a a two-tuple that contains |
| 12 | # a string with the query and a dictionary with the expected result. |
| 13 | |
| 14 | parse_qsl_test_cases = [ |
| 15 | ("", []), |
| 16 | ("&", []), |
| 17 | ("&&", []), |
| 18 | ("=", [('', '')]), |
| 19 | ("=a", [('', 'a')]), |
| 20 | ("a", [('a', '')]), |
| 21 | ("a=", [('a', '')]), |
| 22 | ("a=", [('a', '')]), |
| 23 | ("&a=b", [('a', 'b')]), |
| 24 | ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]), |
| 25 | ("a=1&a=2", [('a', '1'), ('a', '2')]), |
| 26 | ] |
| 27 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 28 | class UrlParseTestCase(unittest.TestCase): |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 29 | |
| 30 | def checkRoundtrips(self, url, parsed, split): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 31 | result = urllib.parse.urlparse(url) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 32 | self.assertEqual(result, parsed) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | t = (result.scheme, result.netloc, result.path, |
| 34 | result.params, result.query, result.fragment) |
| 35 | self.assertEqual(t, parsed) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 36 | # put it back together and it should be the same |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 37 | result2 = urllib.parse.urlunparse(result) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 38 | self.assertEqual(result2, url) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | self.assertEqual(result2, result.geturl()) |
| 40 | |
| 41 | # the result of geturl() is a fixpoint; we can always parse it |
| 42 | # again to get the same result: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 43 | result3 = urllib.parse.urlparse(result.geturl()) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 44 | self.assertEqual(result3.geturl(), result.geturl()) |
| 45 | self.assertEqual(result3, result) |
| 46 | self.assertEqual(result3.scheme, result.scheme) |
| 47 | self.assertEqual(result3.netloc, result.netloc) |
| 48 | self.assertEqual(result3.path, result.path) |
| 49 | self.assertEqual(result3.params, result.params) |
| 50 | self.assertEqual(result3.query, result.query) |
| 51 | self.assertEqual(result3.fragment, result.fragment) |
| 52 | self.assertEqual(result3.username, result.username) |
| 53 | self.assertEqual(result3.password, result.password) |
| 54 | self.assertEqual(result3.hostname, result.hostname) |
| 55 | self.assertEqual(result3.port, result.port) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 56 | |
| 57 | # check the roundtrip using urlsplit() as well |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 58 | result = urllib.parse.urlsplit(url) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 59 | self.assertEqual(result, split) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 60 | t = (result.scheme, result.netloc, result.path, |
| 61 | result.query, result.fragment) |
| 62 | self.assertEqual(t, split) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 63 | result2 = urllib.parse.urlunsplit(result) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 64 | self.assertEqual(result2, url) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 65 | self.assertEqual(result2, result.geturl()) |
| 66 | |
| 67 | # check the fixpoint property of re-parsing the result of geturl() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 68 | result3 = urllib.parse.urlsplit(result.geturl()) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 69 | self.assertEqual(result3.geturl(), result.geturl()) |
| 70 | self.assertEqual(result3, result) |
| 71 | self.assertEqual(result3.scheme, result.scheme) |
| 72 | self.assertEqual(result3.netloc, result.netloc) |
| 73 | self.assertEqual(result3.path, result.path) |
| 74 | self.assertEqual(result3.query, result.query) |
| 75 | self.assertEqual(result3.fragment, result.fragment) |
| 76 | self.assertEqual(result3.username, result.username) |
| 77 | self.assertEqual(result3.password, result.password) |
| 78 | self.assertEqual(result3.hostname, result.hostname) |
| 79 | self.assertEqual(result3.port, result.port) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 80 | |
Facundo Batista | c469d4c | 2008-09-03 22:49:01 +0000 | [diff] [blame] | 81 | def test_qsl(self): |
| 82 | for orig, expect in parse_qsl_test_cases: |
| 83 | result = urllib.parse.parse_qsl(orig, keep_blank_values=True) |
| 84 | self.assertEqual(result, expect, "Error parsing %s" % repr(orig)) |
| 85 | |
| 86 | |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 87 | def test_roundtrips(self): |
| 88 | testcases = [ |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 89 | ('file:///tmp/junk.txt', |
| 90 | ('file', '', '/tmp/junk.txt', '', '', ''), |
| 91 | ('file', '', '/tmp/junk.txt', '', '')), |
Neal Norwitz | 68b539e | 2003-01-06 06:58:31 +0000 | [diff] [blame] | 92 | ('imap://mail.python.org/mbox1', |
| 93 | ('imap', 'mail.python.org', '/mbox1', '', '', ''), |
| 94 | ('imap', 'mail.python.org', '/mbox1', '', '')), |
Skip Montanaro | f09b88e | 2003-01-06 20:27:03 +0000 | [diff] [blame] | 95 | ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf', |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 96 | ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
| 97 | '', '', ''), |
| 98 | ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
| 99 | '', '')), |
Senthil Kumaran | eaaec27 | 2009-03-30 21:54:41 +0000 | [diff] [blame] | 100 | ('nfs://server/path/to/file.txt', |
| 101 | ('nfs', 'server', '/path/to/file.txt', '', '', ''), |
| 102 | ('nfs', 'server', '/path/to/file.txt', '', '')), |
Fred Drake | 50747fc | 2005-07-29 15:56:32 +0000 | [diff] [blame] | 103 | ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/', |
| 104 | ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/', |
| 105 | '', '', ''), |
| 106 | ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/', |
Senthil Kumaran | d4cd188 | 2010-05-13 03:43:13 +0000 | [diff] [blame^] | 107 | '', '')), |
| 108 | ('git+ssh://git@github.com/user/project.git', |
| 109 | ('git+ssh', 'git@github.com','/user/project.git', |
| 110 | '','',''), |
| 111 | ('git+ssh', 'git@github.com','/user/project.git', |
| 112 | '', '')) |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 113 | ] |
| 114 | for url, parsed, split in testcases: |
| 115 | self.checkRoundtrips(url, parsed, split) |
Michael W. Hudson | bd3e771 | 2002-03-18 13:06:00 +0000 | [diff] [blame] | 116 | |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 117 | def test_http_roundtrips(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 118 | # urllib.parse.urlsplit treats 'http:' as an optimized special case, |
Johannes Gijsbers | 41e4faa | 2005-01-09 15:29:10 +0000 | [diff] [blame] | 119 | # so we test both 'http:' and 'https:' in all the following. |
| 120 | # Three cheers for white box knowledge! |
| 121 | testcases = [ |
| 122 | ('://www.python.org', |
| 123 | ('www.python.org', '', '', '', ''), |
| 124 | ('www.python.org', '', '', '')), |
| 125 | ('://www.python.org#abc', |
| 126 | ('www.python.org', '', '', '', 'abc'), |
| 127 | ('www.python.org', '', '', 'abc')), |
| 128 | ('://www.python.org?q=abc', |
| 129 | ('www.python.org', '', '', 'q=abc', ''), |
| 130 | ('www.python.org', '', 'q=abc', '')), |
| 131 | ('://www.python.org/#abc', |
| 132 | ('www.python.org', '/', '', '', 'abc'), |
| 133 | ('www.python.org', '/', '', 'abc')), |
| 134 | ('://a/b/c/d;p?q#f', |
| 135 | ('a', '/b/c/d', 'p', 'q', 'f'), |
| 136 | ('a', '/b/c/d;p', 'q', 'f')), |
| 137 | ] |
| 138 | for scheme in ('http', 'https'): |
| 139 | for url, parsed, split in testcases: |
| 140 | url = scheme + url |
| 141 | parsed = (scheme,) + parsed |
| 142 | split = (scheme,) + split |
| 143 | self.checkRoundtrips(url, parsed, split) |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 144 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 145 | def checkJoin(self, base, relurl, expected): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 146 | self.assertEqual(urllib.parse.urljoin(base, relurl), expected, |
Guido van Rossum | bbc0568 | 2002-10-14 19:59:54 +0000 | [diff] [blame] | 147 | (base, relurl, expected)) |
| 148 | |
| 149 | def test_unparse_parse(self): |
Senthil Kumaran | 8be2211 | 2010-04-12 07:06:04 +0000 | [diff] [blame] | 150 | for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 151 | self.assertEqual(urllib.parse.urlunsplit(urllib.parse.urlsplit(u)), u) |
| 152 | self.assertEqual(urllib.parse.urlunparse(urllib.parse.urlparse(u)), u) |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 153 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 154 | def test_RFC1808(self): |
| 155 | # "normal" cases from RFC 1808: |
| 156 | self.checkJoin(RFC1808_BASE, 'g:h', 'g:h') |
| 157 | self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g') |
| 158 | self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g') |
| 159 | self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/') |
| 160 | self.checkJoin(RFC1808_BASE, '/g', 'http://a/g') |
| 161 | self.checkJoin(RFC1808_BASE, '//g', 'http://g') |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 162 | self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y') |
| 163 | self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
| 164 | self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s') |
| 165 | self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s') |
| 166 | self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
| 167 | self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 168 | self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x') |
| 169 | self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
| 170 | self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/') |
| 171 | self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/') |
| 172 | self.checkJoin(RFC1808_BASE, '..', 'http://a/b/') |
| 173 | self.checkJoin(RFC1808_BASE, '../', 'http://a/b/') |
| 174 | self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g') |
| 175 | self.checkJoin(RFC1808_BASE, '../..', 'http://a/') |
| 176 | self.checkJoin(RFC1808_BASE, '../../', 'http://a/') |
| 177 | self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g') |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 178 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 179 | # "abnormal" cases from RFC 1808: |
| 180 | self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f') |
| 181 | self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g') |
| 182 | self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g') |
| 183 | self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g') |
| 184 | self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g') |
| 185 | self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.') |
| 186 | self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g') |
| 187 | self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..') |
| 188 | self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g') |
| 189 | self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g') |
| 190 | self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/') |
| 191 | self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h') |
| 192 | self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h') |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 193 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 194 | # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808), |
| 195 | # so we'll not actually run these tests (which expect 1808 behavior). |
| 196 | #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g') |
| 197 | #self.checkJoin(RFC1808_BASE, 'http:', 'http:') |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 198 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 199 | def test_RFC2396(self): |
| 200 | # cases from RFC 2396 |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 201 | self.checkJoin(RFC2396_BASE, 'g:h', 'g:h') |
| 202 | self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g') |
| 203 | self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g') |
| 204 | self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/') |
| 205 | self.checkJoin(RFC2396_BASE, '/g', 'http://a/g') |
| 206 | self.checkJoin(RFC2396_BASE, '//g', 'http://g') |
| 207 | self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y') |
| 208 | self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s') |
| 209 | self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s') |
| 210 | self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
| 211 | self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x') |
| 212 | self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
| 213 | self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/') |
| 214 | self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/') |
| 215 | self.checkJoin(RFC2396_BASE, '..', 'http://a/b/') |
| 216 | self.checkJoin(RFC2396_BASE, '../', 'http://a/b/') |
| 217 | self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g') |
| 218 | self.checkJoin(RFC2396_BASE, '../..', 'http://a/') |
| 219 | self.checkJoin(RFC2396_BASE, '../../', 'http://a/') |
| 220 | self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g') |
| 221 | self.checkJoin(RFC2396_BASE, '', RFC2396_BASE) |
| 222 | self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g') |
| 223 | self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g') |
| 224 | self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g') |
| 225 | self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g') |
| 226 | self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.') |
| 227 | self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g') |
| 228 | self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..') |
| 229 | self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g') |
| 230 | self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g') |
| 231 | self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/') |
| 232 | self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h') |
| 233 | self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h') |
| 234 | self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y') |
| 235 | self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y') |
| 236 | self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
| 237 | self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x') |
| 238 | self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
| 239 | self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x') |
| 240 | |
Facundo Batista | 23e3856 | 2008-08-14 16:55:14 +0000 | [diff] [blame] | 241 | def test_RFC3986(self): |
Senthil Kumaran | 06f29cd | 2010-05-07 04:24:30 +0000 | [diff] [blame] | 242 | # Test cases from RFC3986 |
Facundo Batista | 23e3856 | 2008-08-14 16:55:14 +0000 | [diff] [blame] | 243 | self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y') |
| 244 | self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x') |
Senthil Kumaran | 06f29cd | 2010-05-07 04:24:30 +0000 | [diff] [blame] | 245 | self.checkJoin(RFC3986_BASE, 'g:h','g:h') |
| 246 | self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g') |
| 247 | self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g') |
| 248 | self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/') |
| 249 | self.checkJoin(RFC3986_BASE, '/g','http://a/g') |
| 250 | self.checkJoin(RFC3986_BASE, '//g','http://g') |
| 251 | self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y') |
| 252 | self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y') |
| 253 | self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s') |
| 254 | self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s') |
| 255 | self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s') |
| 256 | self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x') |
| 257 | self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x') |
| 258 | self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s') |
| 259 | self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q') |
| 260 | self.checkJoin(RFC3986_BASE, '.','http://a/b/c/') |
| 261 | self.checkJoin(RFC3986_BASE, './','http://a/b/c/') |
| 262 | self.checkJoin(RFC3986_BASE, '..','http://a/b/') |
| 263 | self.checkJoin(RFC3986_BASE, '../','http://a/b/') |
| 264 | self.checkJoin(RFC3986_BASE, '../g','http://a/b/g') |
| 265 | self.checkJoin(RFC3986_BASE, '../..','http://a/') |
| 266 | self.checkJoin(RFC3986_BASE, '../../','http://a/') |
| 267 | self.checkJoin(RFC3986_BASE, '../../g','http://a/g') |
| 268 | |
| 269 | #Abnormal Examples |
| 270 | # The 'abnormal scenarios' are incompatible with RFC2986 parsing |
| 271 | # Tests are here for reference. |
| 272 | |
| 273 | #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g') |
| 274 | #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g') |
| 275 | #self.checkJoin(RFC3986_BASE, '/./g','http://a/g') |
| 276 | #self.checkJoin(RFC3986_BASE, '/../g','http://a/g') |
| 277 | |
| 278 | self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.') |
| 279 | self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g') |
| 280 | self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..') |
| 281 | self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g') |
| 282 | self.checkJoin(RFC3986_BASE, './../g','http://a/b/g') |
| 283 | self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/') |
| 284 | self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h') |
| 285 | self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h') |
| 286 | self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y') |
| 287 | self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y') |
| 288 | self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x') |
| 289 | self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x') |
| 290 | self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x') |
| 291 | self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x') |
| 292 | #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser |
| 293 | self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') #relaxed parser |
Facundo Batista | 23e3856 | 2008-08-14 16:55:14 +0000 | [diff] [blame] | 294 | |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 295 | def test_urldefrag(self): |
| 296 | for url, defrag, frag in [ |
| 297 | ('http://python.org#frag', 'http://python.org', 'frag'), |
| 298 | ('http://python.org', 'http://python.org', ''), |
| 299 | ('http://python.org/#frag', 'http://python.org/', 'frag'), |
| 300 | ('http://python.org/', 'http://python.org/', ''), |
| 301 | ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'), |
| 302 | ('http://python.org/?q', 'http://python.org/?q', ''), |
| 303 | ('http://python.org/p#frag', 'http://python.org/p', 'frag'), |
| 304 | ('http://python.org/p?q', 'http://python.org/p?q', ''), |
| 305 | (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'), |
| 306 | (RFC2396_BASE, 'http://a/b/c/d;p?q', ''), |
| 307 | ]: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 308 | self.assertEqual(urllib.parse.urldefrag(url), (defrag, frag)) |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 309 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 310 | def test_urlsplit_attributes(self): |
| 311 | url = "HTTP://WWW.PYTHON.ORG/doc/#frag" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 312 | p = urllib.parse.urlsplit(url) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 313 | self.assertEqual(p.scheme, "http") |
| 314 | self.assertEqual(p.netloc, "WWW.PYTHON.ORG") |
| 315 | self.assertEqual(p.path, "/doc/") |
| 316 | self.assertEqual(p.query, "") |
| 317 | self.assertEqual(p.fragment, "frag") |
| 318 | self.assertEqual(p.username, None) |
| 319 | self.assertEqual(p.password, None) |
| 320 | self.assertEqual(p.hostname, "www.python.org") |
| 321 | self.assertEqual(p.port, None) |
| 322 | # geturl() won't return exactly the original URL in this case |
| 323 | # since the scheme is always case-normalized |
| 324 | #self.assertEqual(p.geturl(), url) |
| 325 | |
| 326 | url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 327 | p = urllib.parse.urlsplit(url) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 328 | self.assertEqual(p.scheme, "http") |
| 329 | self.assertEqual(p.netloc, "User:Pass@www.python.org:080") |
| 330 | self.assertEqual(p.path, "/doc/") |
| 331 | self.assertEqual(p.query, "query=yes") |
| 332 | self.assertEqual(p.fragment, "frag") |
| 333 | self.assertEqual(p.username, "User") |
| 334 | self.assertEqual(p.password, "Pass") |
| 335 | self.assertEqual(p.hostname, "www.python.org") |
| 336 | self.assertEqual(p.port, 80) |
| 337 | self.assertEqual(p.geturl(), url) |
| 338 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 339 | # Addressing issue1698, which suggests Username can contain |
| 340 | # "@" characters. Though not RFC compliant, many ftp sites allow |
| 341 | # and request email addresses as usernames. |
| 342 | |
| 343 | url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 344 | p = urllib.parse.urlsplit(url) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 345 | self.assertEqual(p.scheme, "http") |
| 346 | self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080") |
| 347 | self.assertEqual(p.path, "/doc/") |
| 348 | self.assertEqual(p.query, "query=yes") |
| 349 | self.assertEqual(p.fragment, "frag") |
| 350 | self.assertEqual(p.username, "User@example.com") |
| 351 | self.assertEqual(p.password, "Pass") |
| 352 | self.assertEqual(p.hostname, "www.python.org") |
| 353 | self.assertEqual(p.port, 80) |
| 354 | self.assertEqual(p.geturl(), url) |
| 355 | |
| 356 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 357 | def test_attributes_bad_port(self): |
| 358 | """Check handling of non-integer ports.""" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 359 | p = urllib.parse.urlsplit("http://www.example.net:foo") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | self.assertEqual(p.netloc, "www.example.net:foo") |
| 361 | self.assertRaises(ValueError, lambda: p.port) |
| 362 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 363 | p = urllib.parse.urlparse("http://www.example.net:foo") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 364 | self.assertEqual(p.netloc, "www.example.net:foo") |
| 365 | self.assertRaises(ValueError, lambda: p.port) |
| 366 | |
| 367 | def test_attributes_without_netloc(self): |
| 368 | # This example is straight from RFC 3261. It looks like it |
| 369 | # should allow the username, hostname, and port to be filled |
| 370 | # in, but doesn't. Since it's a URI and doesn't use the |
| 371 | # scheme://netloc syntax, the netloc and related attributes |
| 372 | # should be left empty. |
| 373 | uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 374 | p = urllib.parse.urlsplit(uri) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | self.assertEqual(p.netloc, "") |
| 376 | self.assertEqual(p.username, None) |
| 377 | self.assertEqual(p.password, None) |
| 378 | self.assertEqual(p.hostname, None) |
| 379 | self.assertEqual(p.port, None) |
| 380 | self.assertEqual(p.geturl(), uri) |
| 381 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 382 | p = urllib.parse.urlparse(uri) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 383 | self.assertEqual(p.netloc, "") |
| 384 | self.assertEqual(p.username, None) |
| 385 | self.assertEqual(p.password, None) |
| 386 | self.assertEqual(p.hostname, None) |
| 387 | self.assertEqual(p.port, None) |
| 388 | self.assertEqual(p.geturl(), uri) |
| 389 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 390 | def test_noslash(self): |
| 391 | # Issue 1637: http://foo.com?query is legal |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 392 | self.assertEqual(urllib.parse.urlparse("http://example.com?blahblah=/foo"), |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 393 | ('http', 'example.com', '', '', 'blahblah=/foo', '')) |
| 394 | |
Facundo Batista | 2ac5de2 | 2008-07-07 18:24:11 +0000 | [diff] [blame] | 395 | def test_usingsys(self): |
| 396 | # Issue 3314: sys module is used in the error |
| 397 | self.assertRaises(TypeError, urllib.parse.urlencode, "foo") |
| 398 | |
Senthil Kumaran | a8dbb24 | 2010-02-19 07:45:03 +0000 | [diff] [blame] | 399 | def test_anyscheme(self): |
| 400 | # Issue 7904: s3://foo.com/stuff has netloc "foo.com". |
Ezio Melotti | 9603428 | 2010-02-19 14:51:06 +0000 | [diff] [blame] | 401 | self.assertEqual(urllib.parse.urlparse("s3://foo.com/stuff"), |
| 402 | ('s3', 'foo.com', '/stuff', '', '', '')) |
| 403 | self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff"), |
| 404 | ('x-newscheme', 'foo.com', '/stuff', '', '', '')) |
Senthil Kumaran | a8dbb24 | 2010-02-19 07:45:03 +0000 | [diff] [blame] | 405 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 406 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 407 | support.run_unittest(UrlParseTestCase) |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 408 | |
| 409 | if __name__ == "__main__": |
| 410 | test_main() |