Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test import test_support |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 4 | import unittest |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 5 | import urlparse |
| 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" |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 9 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 10 | class UrlParseTestCase(unittest.TestCase): |
| 11 | def test_frags(self): |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 12 | 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 Norwitz | 68b539e | 2003-01-06 06:58:31 +0000 | [diff] [blame] | 28 | ('imap://mail.python.org/mbox1', |
| 29 | ('imap', 'mail.python.org', '/mbox1', '', '', ''), |
| 30 | ('imap', 'mail.python.org', '/mbox1', '', '')), |
Skip Montanaro | f09b88e | 2003-01-06 20:27:03 +0000 | [diff] [blame] | 31 | ('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 Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 34 | ]: |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 35 | result = urlparse.urlparse(url) |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 36 | self.assertEqual(result, parsed) |
Neal Norwitz | 7dfb6e2 | 2002-09-25 19:20:12 +0000 | [diff] [blame] | 37 | # put it back together and it should be the same |
| 38 | result2 = urlparse.urlunparse(result) |
| 39 | self.assertEqual(result2, url) |
Michael W. Hudson | bd3e771 | 2002-03-18 13:06:00 +0000 | [diff] [blame] | 40 | |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 41 | # 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 Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 47 | def checkJoin(self, base, relurl, expected): |
Guido van Rossum | bbc0568 | 2002-10-14 19:59:54 +0000 | [diff] [blame] | 48 | 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 Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 53 | self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u) |
Guido van Rossum | bbc0568 | 2002-10-14 19:59:54 +0000 | [diff] [blame] | 54 | self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u) |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 55 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 56 | 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') |
| 64 | self.checkJoin(RFC1808_BASE, '?y', 'http://a/b/c/d;p?y') |
| 65 | self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y') |
| 66 | self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
| 67 | self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s') |
| 68 | self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s') |
| 69 | self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
| 70 | self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
| 71 | self.checkJoin(RFC1808_BASE, ';x', 'http://a/b/c/d;x') |
| 72 | self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x') |
| 73 | self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
| 74 | self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/') |
| 75 | self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/') |
| 76 | self.checkJoin(RFC1808_BASE, '..', 'http://a/b/') |
| 77 | self.checkJoin(RFC1808_BASE, '../', 'http://a/b/') |
| 78 | self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g') |
| 79 | self.checkJoin(RFC1808_BASE, '../..', 'http://a/') |
| 80 | self.checkJoin(RFC1808_BASE, '../../', 'http://a/') |
| 81 | self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g') |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 82 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 83 | # "abnormal" cases from RFC 1808: |
| 84 | self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f') |
| 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/./g') |
| 88 | self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../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/c/g..') |
| 92 | self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g') |
| 93 | self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g') |
| 94 | self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/') |
| 95 | self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h') |
| 96 | self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h') |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 97 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 98 | # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808), |
| 99 | # so we'll not actually run these tests (which expect 1808 behavior). |
| 100 | #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g') |
| 101 | #self.checkJoin(RFC1808_BASE, 'http:', 'http:') |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 102 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 103 | def test_RFC2396(self): |
| 104 | # cases from RFC 2396 |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 105 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 106 | ### urlparse.py as of v 1.32 fails on these two |
| 107 | #self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y') |
| 108 | #self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x') |
| 109 | |
| 110 | self.checkJoin(RFC2396_BASE, 'g:h', 'g:h') |
| 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/g') |
| 115 | self.checkJoin(RFC2396_BASE, '//g', 'http://g') |
| 116 | self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y') |
| 117 | self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s') |
| 118 | self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s') |
| 119 | self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s') |
| 120 | self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x') |
| 121 | self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') |
| 122 | self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/') |
| 123 | self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/') |
| 124 | self.checkJoin(RFC2396_BASE, '..', 'http://a/b/') |
| 125 | self.checkJoin(RFC2396_BASE, '../', 'http://a/b/') |
| 126 | self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g') |
| 127 | self.checkJoin(RFC2396_BASE, '../..', 'http://a/') |
| 128 | self.checkJoin(RFC2396_BASE, '../../', 'http://a/') |
| 129 | self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g') |
| 130 | self.checkJoin(RFC2396_BASE, '', RFC2396_BASE) |
| 131 | self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g') |
| 132 | self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g') |
| 133 | self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g') |
| 134 | self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g') |
| 135 | self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.') |
| 136 | self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g') |
| 137 | self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..') |
| 138 | self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g') |
| 139 | self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g') |
| 140 | self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/') |
| 141 | self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h') |
| 142 | self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h') |
| 143 | self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y') |
| 144 | self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y') |
| 145 | self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') |
| 146 | self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x') |
| 147 | self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') |
| 148 | self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x') |
| 149 | |
Fred Drake | 7070565 | 2002-10-16 21:02:36 +0000 | [diff] [blame] | 150 | def test_urldefrag(self): |
| 151 | for url, defrag, frag in [ |
| 152 | ('http://python.org#frag', 'http://python.org', 'frag'), |
| 153 | ('http://python.org', 'http://python.org', ''), |
| 154 | ('http://python.org/#frag', 'http://python.org/', 'frag'), |
| 155 | ('http://python.org/', 'http://python.org/', ''), |
| 156 | ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'), |
| 157 | ('http://python.org/?q', 'http://python.org/?q', ''), |
| 158 | ('http://python.org/p#frag', 'http://python.org/p', 'frag'), |
| 159 | ('http://python.org/p?q', 'http://python.org/p?q', ''), |
| 160 | (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'), |
| 161 | (RFC2396_BASE, 'http://a/b/c/d;p?q', ''), |
| 162 | ]: |
| 163 | self.assertEqual(urlparse.urldefrag(url), (defrag, frag)) |
| 164 | |
Skip Montanaro | 6ec967d | 2002-03-23 05:32:10 +0000 | [diff] [blame] | 165 | def test_main(): |
| 166 | test_support.run_unittest(UrlParseTestCase) |
| 167 | |
| 168 | if __name__ == "__main__": |
| 169 | test_main() |