|
8 | 8 | RFC2396_BASE = "http://a/b/c/d;p?q" |
9 | 9 |
|
10 | 10 | class UrlParseTestCase(unittest.TestCase): |
11 | | - def test_frags(self): |
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')), |
| 11 | + |
| 12 | + def checkRoundtrips(self, url, parsed, split): |
| 13 | + result = urlparse.urlparse(url) |
| 14 | + self.assertEqual(result, parsed) |
| 15 | + # put it back together and it should be the same |
| 16 | + result2 = urlparse.urlunparse(result) |
| 17 | + self.assertEqual(result2, url) |
| 18 | + |
| 19 | + # check the roundtrip using urlsplit() as well |
| 20 | + result = urlparse.urlsplit(url) |
| 21 | + self.assertEqual(result, split) |
| 22 | + result2 = urlparse.urlunsplit(result) |
| 23 | + self.assertEqual(result2, url) |
| 24 | + |
| 25 | + def test_roundtrips(self): |
| 26 | + testcases = [ |
25 | 27 | ('file:///tmp/junk.txt', |
26 | 28 | ('file', '', '/tmp/junk.txt', '', '', ''), |
27 | 29 | ('file', '', '/tmp/junk.txt', '', '')), |
28 | 30 | ('imap://mail.python.org/mbox1', |
29 | 31 | ('imap', 'mail.python.org', '/mbox1', '', '', ''), |
30 | 32 | ('imap', 'mail.python.org', '/mbox1', '', '')), |
31 | 33 | ('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', '', '')), |
34 | | - ]: |
35 | | - result = urlparse.urlparse(url) |
36 | | - self.assertEqual(result, parsed) |
37 | | - # put it back together and it should be the same |
38 | | - result2 = urlparse.urlunparse(result) |
39 | | - self.assertEqual(result2, url) |
40 | | - |
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) |
| 34 | + ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
| 35 | + '', '', ''), |
| 36 | + ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf', |
| 37 | + '', '')), |
| 38 | + ] |
| 39 | + for url, parsed, split in testcases: |
| 40 | + self.checkRoundtrips(url, parsed, split) |
| 41 | + |
| 42 | + def test_http_roundtrips(self): |
| 43 | + # urlparse.urlsplit treats 'http:' as an optimized special case, |
| 44 | + # so we test both 'http:' and 'https:' in all the following. |
| 45 | + # Three cheers for white box knowledge! |
| 46 | + testcases = [ |
| 47 | + ('://www.python.org', |
| 48 | + ('www.python.org', '', '', '', ''), |
| 49 | + ('www.python.org', '', '', '')), |
| 50 | + ('://www.python.org#abc', |
| 51 | + ('www.python.org', '', '', '', 'abc'), |
| 52 | + ('www.python.org', '', '', 'abc')), |
| 53 | + ('://www.python.org?q=abc', |
| 54 | + ('www.python.org', '', '', 'q=abc', ''), |
| 55 | + ('www.python.org', '', 'q=abc', '')), |
| 56 | + ('://www.python.org/#abc', |
| 57 | + ('www.python.org', '/', '', '', 'abc'), |
| 58 | + ('www.python.org', '/', '', 'abc')), |
| 59 | + ('://a/b/c/d;p?q#f', |
| 60 | + ('a', '/b/c/d', 'p', 'q', 'f'), |
| 61 | + ('a', '/b/c/d;p', 'q', 'f')), |
| 62 | + ] |
| 63 | + for scheme in ('http', 'https'): |
| 64 | + for url, parsed, split in testcases: |
| 65 | + url = scheme + url |
| 66 | + parsed = (scheme,) + parsed |
| 67 | + split = (scheme,) + split |
| 68 | + self.checkRoundtrips(url, parsed, split) |
46 | 69 |
|
47 | 70 | def checkJoin(self, base, relurl, expected): |
48 | 71 | self.assertEqual(urlparse.urljoin(base, relurl), expected, |
|
0 commit comments