added RFC 2396 tests from Aaron Swartz included in bug # 450225.
converted to use unittest
This commit is contained in:
parent
b1ba6b0044
commit
6ec967d066
@ -1,84 +1,126 @@
|
|||||||
|
#! /usr/bin/env python
|
||||||
|
|
||||||
|
import test_support
|
||||||
|
import unittest
|
||||||
import urlparse
|
import urlparse
|
||||||
|
|
||||||
errors = 0
|
|
||||||
|
|
||||||
RFC1808_BASE = "http://a/b/c/d;p?q#f"
|
RFC1808_BASE = "http://a/b/c/d;p?q#f"
|
||||||
|
RFC2396_BASE = "http://a/b/c/d;p?q"
|
||||||
|
|
||||||
for url, expected in [('http://www.python.org',
|
class UrlParseTestCase(unittest.TestCase):
|
||||||
('http', 'www.python.org', '', '', '', '')),
|
def test_frags(self):
|
||||||
('http://www.python.org#abc',
|
for url, expected in [('http://www.python.org',
|
||||||
('http', 'www.python.org', '', '', '', 'abc')),
|
('http', 'www.python.org', '', '', '', '')),
|
||||||
('http://www.python.org/#abc',
|
('http://www.python.org#abc',
|
||||||
('http', 'www.python.org', '/', '', '', 'abc')),
|
('http', 'www.python.org', '', '', '', 'abc')),
|
||||||
(RFC1808_BASE,
|
('http://www.python.org/#abc',
|
||||||
('http', 'a', '/b/c/d', 'p', 'q', 'f')),
|
('http', 'www.python.org', '/', '', '', 'abc')),
|
||||||
]:
|
(RFC1808_BASE,
|
||||||
result = urlparse.urlparse(url)
|
('http', 'a', '/b/c/d', 'p', 'q', 'f')),
|
||||||
print "%-13s = %r" % (url, result)
|
]:
|
||||||
if result != expected:
|
result = urlparse.urlparse(url)
|
||||||
errors += 1
|
self.assertEqual(result, expected)
|
||||||
print "urlparse(%r)" % url
|
|
||||||
print ("expected %r,\n"
|
|
||||||
" got %r") % (expected, result)
|
|
||||||
print
|
|
||||||
|
|
||||||
def checkJoin(relurl, expected):
|
def checkJoin(self, base, relurl, expected):
|
||||||
global errors
|
self.assertEqual(urlparse.urljoin(base, relurl), expected)
|
||||||
result = urlparse.urljoin(RFC1808_BASE, relurl)
|
|
||||||
print "%-13s = %r" % (relurl, result)
|
|
||||||
if result != expected:
|
|
||||||
errors += 1
|
|
||||||
print "urljoin(%r, %r)" % (RFC1808_BASE, relurl)
|
|
||||||
print ("expected %r,\n"
|
|
||||||
" got %r") % (expected, result)
|
|
||||||
|
|
||||||
print "urlparse.urljoin() tests"
|
def test_RFC1808(self):
|
||||||
print
|
# "normal" cases from RFC 1808:
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
|
||||||
|
self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
|
||||||
|
self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
|
||||||
|
self.checkJoin(RFC1808_BASE, '//g', 'http://g')
|
||||||
|
self.checkJoin(RFC1808_BASE, '?y', 'http://a/b/c/d;p?y')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
|
||||||
|
self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
|
||||||
|
self.checkJoin(RFC1808_BASE, ';x', 'http://a/b/c/d;x')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
|
||||||
|
self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
|
||||||
|
self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
|
||||||
|
self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
|
||||||
|
self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
|
||||||
|
self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
|
||||||
|
self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
|
||||||
|
self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
|
||||||
|
self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
|
||||||
|
self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
|
||||||
|
|
||||||
# "normal" cases from RFC 1808:
|
# "abnormal" cases from RFC 1808:
|
||||||
checkJoin('g:h', 'g:h')
|
self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
|
||||||
checkJoin('g', 'http://a/b/c/g')
|
self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
|
||||||
checkJoin('./g', 'http://a/b/c/g')
|
self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
|
||||||
checkJoin('g/', 'http://a/b/c/g/')
|
self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
|
||||||
checkJoin('/g', 'http://a/g')
|
self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
|
||||||
checkJoin('//g', 'http://g')
|
self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
|
||||||
checkJoin('?y', 'http://a/b/c/d;p?y')
|
self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
|
||||||
checkJoin('g?y', 'http://a/b/c/g?y')
|
self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
|
||||||
checkJoin('g?y/./x', 'http://a/b/c/g?y/./x')
|
self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
|
||||||
checkJoin('#s', 'http://a/b/c/d;p?q#s')
|
self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
|
||||||
checkJoin('g#s', 'http://a/b/c/g#s')
|
self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
|
||||||
checkJoin('g#s/./x', 'http://a/b/c/g#s/./x')
|
self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
|
||||||
checkJoin('g?y#s', 'http://a/b/c/g?y#s')
|
self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
|
||||||
checkJoin(';x', 'http://a/b/c/d;x')
|
|
||||||
checkJoin('g;x', 'http://a/b/c/g;x')
|
|
||||||
checkJoin('g;x?y#s', 'http://a/b/c/g;x?y#s')
|
|
||||||
checkJoin('.', 'http://a/b/c/')
|
|
||||||
checkJoin('./', 'http://a/b/c/')
|
|
||||||
checkJoin('..', 'http://a/b/')
|
|
||||||
checkJoin('../', 'http://a/b/')
|
|
||||||
checkJoin('../g', 'http://a/b/g')
|
|
||||||
checkJoin('../..', 'http://a/')
|
|
||||||
checkJoin('../../', 'http://a/')
|
|
||||||
checkJoin('../../g', 'http://a/g')
|
|
||||||
|
|
||||||
# "abnormal" cases from RFC 1808:
|
# RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
|
||||||
checkJoin('', 'http://a/b/c/d;p?q#f')
|
# so we'll not actually run these tests (which expect 1808 behavior).
|
||||||
checkJoin('../../../g', 'http://a/../g')
|
#self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
|
||||||
checkJoin('../../../../g', 'http://a/../../g')
|
#self.checkJoin(RFC1808_BASE, 'http:', 'http:')
|
||||||
checkJoin('/./g', 'http://a/./g')
|
|
||||||
checkJoin('/../g', 'http://a/../g')
|
|
||||||
checkJoin('g.', 'http://a/b/c/g.')
|
|
||||||
checkJoin('.g', 'http://a/b/c/.g')
|
|
||||||
checkJoin('g..', 'http://a/b/c/g..')
|
|
||||||
checkJoin('..g', 'http://a/b/c/..g')
|
|
||||||
checkJoin('./../g', 'http://a/b/g')
|
|
||||||
checkJoin('./g/.', 'http://a/b/c/g/')
|
|
||||||
checkJoin('g/./h', 'http://a/b/c/g/h')
|
|
||||||
checkJoin('g/../h', 'http://a/b/c/h')
|
|
||||||
|
|
||||||
# RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
|
def test_RFC2396(self):
|
||||||
# so we'll not actually run these tests (which expect 1808 behavior).
|
# cases from RFC 2396
|
||||||
#checkJoin('http:g', 'http:g')
|
|
||||||
#checkJoin('http:', 'http:')
|
### urlparse.py as of v 1.32 fails on these two
|
||||||
|
#self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
|
||||||
|
#self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
|
||||||
|
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
|
||||||
|
self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
|
||||||
|
self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
|
||||||
|
self.checkJoin(RFC2396_BASE, '//g', 'http://g')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
|
||||||
|
self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
|
||||||
|
self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
|
||||||
|
self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
|
||||||
|
self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
|
||||||
|
self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
|
||||||
|
self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
|
||||||
|
self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
|
||||||
|
self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
|
||||||
|
self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
|
||||||
|
self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
|
||||||
|
self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
|
||||||
|
self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
|
||||||
|
self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
|
||||||
|
self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
|
||||||
|
self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
|
||||||
|
self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
|
||||||
|
self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
|
||||||
|
self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
|
||||||
|
self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
test_support.run_unittest(UrlParseTestCase)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_main()
|
||||||
|
|
||||||
print errors, "errors"
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user