Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 1 | import urlparse |
| 2 | |
| 3 | errors = 0 |
| 4 | |
| 5 | RFC1808_BASE = "http://a/b/c/d;p?q#f" |
| 6 | |
Michael W. Hudson | bd3e771 | 2002-03-18 13:06:00 +0000 | [diff] [blame] | 7 | for url, expected in [('http://www.python.org', |
| 8 | ('http', 'www.python.org', '', '', '', '')), |
| 9 | ('http://www.python.org#abc', |
| 10 | ('http', 'www.python.org', '', '', '', 'abc')), |
| 11 | ('http://www.python.org/#abc', |
| 12 | ('http', 'www.python.org', '/', '', '', 'abc')), |
| 13 | (RFC1808_BASE, |
| 14 | ('http', 'a', '/b/c/d', 'p', 'q', 'f')), |
| 15 | ]: |
| 16 | result = urlparse.urlparse(url) |
| 17 | print "%-13s = %r" % (url, result) |
| 18 | if result != expected: |
| 19 | errors += 1 |
| 20 | print "urlparse(%r)" % url |
| 21 | print ("expected %r,\n" |
| 22 | " got %r") % (expected, result) |
| 23 | print |
| 24 | |
Fred Drake | a4d18a0 | 2001-01-05 05:57:04 +0000 | [diff] [blame] | 25 | def checkJoin(relurl, expected): |
| 26 | global errors |
| 27 | result = urlparse.urljoin(RFC1808_BASE, relurl) |
| 28 | print "%-13s = %r" % (relurl, result) |
| 29 | if result != expected: |
| 30 | errors += 1 |
| 31 | print "urljoin(%r, %r)" % (RFC1808_BASE, relurl) |
| 32 | print ("expected %r,\n" |
| 33 | " got %r") % (expected, result) |
| 34 | |
| 35 | print "urlparse.urljoin() tests" |
| 36 | print |
| 37 | |
| 38 | # "normal" cases from RFC 1808: |
| 39 | checkJoin('g:h', 'g:h') |
| 40 | checkJoin('g', 'http://a/b/c/g') |
| 41 | checkJoin('./g', 'http://a/b/c/g') |
| 42 | checkJoin('g/', 'http://a/b/c/g/') |
| 43 | checkJoin('/g', 'http://a/g') |
| 44 | checkJoin('//g', 'http://g') |
| 45 | checkJoin('?y', 'http://a/b/c/d;p?y') |
| 46 | checkJoin('g?y', 'http://a/b/c/g?y') |
| 47 | checkJoin('g?y/./x', 'http://a/b/c/g?y/./x') |
| 48 | checkJoin('#s', 'http://a/b/c/d;p?q#s') |
| 49 | checkJoin('g#s', 'http://a/b/c/g#s') |
| 50 | checkJoin('g#s/./x', 'http://a/b/c/g#s/./x') |
| 51 | checkJoin('g?y#s', 'http://a/b/c/g?y#s') |
| 52 | checkJoin(';x', 'http://a/b/c/d;x') |
| 53 | checkJoin('g;x', 'http://a/b/c/g;x') |
| 54 | checkJoin('g;x?y#s', 'http://a/b/c/g;x?y#s') |
| 55 | checkJoin('.', 'http://a/b/c/') |
| 56 | checkJoin('./', 'http://a/b/c/') |
| 57 | checkJoin('..', 'http://a/b/') |
| 58 | checkJoin('../', 'http://a/b/') |
| 59 | checkJoin('../g', 'http://a/b/g') |
| 60 | checkJoin('../..', 'http://a/') |
| 61 | checkJoin('../../', 'http://a/') |
| 62 | checkJoin('../../g', 'http://a/g') |
| 63 | |
| 64 | # "abnormal" cases from RFC 1808: |
| 65 | checkJoin('', 'http://a/b/c/d;p?q#f') |
| 66 | checkJoin('../../../g', 'http://a/../g') |
| 67 | checkJoin('../../../../g', 'http://a/../../g') |
| 68 | checkJoin('/./g', 'http://a/./g') |
| 69 | checkJoin('/../g', 'http://a/../g') |
| 70 | checkJoin('g.', 'http://a/b/c/g.') |
| 71 | checkJoin('.g', 'http://a/b/c/.g') |
| 72 | checkJoin('g..', 'http://a/b/c/g..') |
| 73 | checkJoin('..g', 'http://a/b/c/..g') |
| 74 | checkJoin('./../g', 'http://a/b/g') |
| 75 | checkJoin('./g/.', 'http://a/b/c/g/') |
| 76 | checkJoin('g/./h', 'http://a/b/c/g/h') |
| 77 | checkJoin('g/../h', 'http://a/b/c/h') |
| 78 | |
| 79 | # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808), |
| 80 | # so we'll not actually run these tests (which expect 1808 behavior). |
| 81 | #checkJoin('http:g', 'http:g') |
| 82 | #checkJoin('http:', 'http:') |
| 83 | |
| 84 | print errors, "errors" |