0001 #! /usr/bin/env python 0002 0003 from test import test_support 0004 import unittest 0005 import urlparse 0006 0007 RFC1808_BASE = "http://a/b/c/d;p?q#f" 0008 RFC2396_BASE = "http://a/b/c/d;p?q" 0009 0010 class UrlParseTestCase(unittest.TestCase): 0011 def test_frags(self): 0012 for url, parsed, split in [ 0013 ('http://www.python.org', 0014 ('http', 'www.python.org', '', '', '', ''), 0015 ('http', 'www.python.org', '', '', '')), 0016 ('http://www.python.org#abc', 0017 ('http', 'www.python.org', '', '', '', 'abc'), 0018 ('http', 'www.python.org', '', '', 'abc')), 0019 ('http://www.python.org/#abc', 0020 ('http', 'www.python.org', '/', '', '', 'abc'), 0021 ('http', 'www.python.org', '/', '', 'abc')), 0022 (RFC1808_BASE, 0023 ('http', 'a', '/b/c/d', 'p', 'q', 'f'), 0024 ('http', 'a', '/b/c/d;p', 'q', 'f')), 0025 ('file:///tmp/junk.txt', 0026 ('file', '', '/tmp/junk.txt', '', '', ''), 0027 ('file', '', '/tmp/junk.txt', '', '')), 0028 ('imap://mail.python.org/mbox1', 0029 ('imap', 'mail.python.org', '/mbox1', '', '', ''), 0030 ('imap', 'mail.python.org', '/mbox1', '', '')), 0031 ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf', 0032 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', '', '', ''), 0033 ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', '', '')), 0034 ]: 0035 result = urlparse.urlparse(url) 0036 self.assertEqual(result, parsed) 0037 # put it back together and it should be the same 0038 result2 = urlparse.urlunparse(result) 0039 self.assertEqual(result2, url) 0040 0041 # check the roundtrip using urlsplit() as well 0042 result = urlparse.urlsplit(url) 0043 self.assertEqual(result, split) 0044 result2 = urlparse.urlunsplit(result) 0045 self.assertEqual(result2, url) 0046 0047 def checkJoin(self, base, relurl, expected): 0048 self.assertEqual(urlparse.urljoin(base, relurl), expected, 0049 (base, relurl, expected)) 0050 0051 def test_unparse_parse(self): 0052 for u in ['Python', './Python']: 0053 self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u) 0054 self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u) 0055 0056 def test_RFC1808(self): 0057 # "normal" cases from RFC 1808: 0058 self.checkJoin(RFC1808_BASE, 'g:h', 'g:h') 0059 self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g') 0060 self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g') 0061 self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/') 0062 self.checkJoin(RFC1808_BASE, '/g', 'http://a/g') 0063 self.checkJoin(RFC1808_BASE, '//g', 'http://g') 0064 self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y') 0065 self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') 0066 self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s') 0067 self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s') 0068 self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') 0069 self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s') 0070 self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x') 0071 self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') 0072 self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/') 0073 self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/') 0074 self.checkJoin(RFC1808_BASE, '..', 'http://a/b/') 0075 self.checkJoin(RFC1808_BASE, '../', 'http://a/b/') 0076 self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g') 0077 self.checkJoin(RFC1808_BASE, '../..', 'http://a/') 0078 self.checkJoin(RFC1808_BASE, '../../', 'http://a/') 0079 self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g') 0080 0081 # "abnormal" cases from RFC 1808: 0082 self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f') 0083 self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g') 0084 self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g') 0085 self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g') 0086 self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g') 0087 self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.') 0088 self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g') 0089 self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..') 0090 self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g') 0091 self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g') 0092 self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/') 0093 self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h') 0094 self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h') 0095 0096 # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808), 0097 # so we'll not actually run these tests (which expect 1808 behavior). 0098 #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g') 0099 #self.checkJoin(RFC1808_BASE, 'http:', 'http:') 0100 0101 def test_RFC2396(self): 0102 # cases from RFC 2396 0103 0104 self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y') 0105 self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x') 0106 0107 self.checkJoin(RFC2396_BASE, 'g:h', 'g:h') 0108 self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g') 0109 self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g') 0110 self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/') 0111 self.checkJoin(RFC2396_BASE, '/g', 'http://a/g') 0112 self.checkJoin(RFC2396_BASE, '//g', 'http://g') 0113 self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y') 0114 self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s') 0115 self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s') 0116 self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s') 0117 self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x') 0118 self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s') 0119 self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/') 0120 self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/') 0121 self.checkJoin(RFC2396_BASE, '..', 'http://a/b/') 0122 self.checkJoin(RFC2396_BASE, '../', 'http://a/b/') 0123 self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g') 0124 self.checkJoin(RFC2396_BASE, '../..', 'http://a/') 0125 self.checkJoin(RFC2396_BASE, '../../', 'http://a/') 0126 self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g') 0127 self.checkJoin(RFC2396_BASE, '', RFC2396_BASE) 0128 self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g') 0129 self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g') 0130 self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g') 0131 self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g') 0132 self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.') 0133 self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g') 0134 self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..') 0135 self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g') 0136 self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g') 0137 self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/') 0138 self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h') 0139 self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h') 0140 self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y') 0141 self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y') 0142 self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x') 0143 self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x') 0144 self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x') 0145 self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x') 0146 0147 def test_urldefrag(self): 0148 for url, defrag, frag in [ 0149 ('http://python.org#frag', 'http://python.org', 'frag'), 0150 ('http://python.org', 'http://python.org', ''), 0151 ('http://python.org/#frag', 'http://python.org/', 'frag'), 0152 ('http://python.org/', 'http://python.org/', ''), 0153 ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'), 0154 ('http://python.org/?q', 'http://python.org/?q', ''), 0155 ('http://python.org/p#frag', 'http://python.org/p', 'frag'), 0156 ('http://python.org/p?q', 'http://python.org/p?q', ''), 0157 (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'), 0158 (RFC2396_BASE, 'http://a/b/c/d;p?q', ''), 0159 ]: 0160 self.assertEqual(urlparse.urldefrag(url), (defrag, frag)) 0161 0162 def test_main(): 0163 test_support.run_unittest(UrlParseTestCase) 0164 0165 if __name__ == "__main__": 0166 test_main() 0167
Generated by PyXR 0.9.4