blob: 48c526bf394599b9d5d7ff424a7e213277e7e468 [file] [log] [blame]
Fred Drakea4d18a02001-01-05 05:57:04 +00001import urlparse
2
3errors = 0
4
5RFC1808_BASE = "http://a/b/c/d;p?q#f"
6
Michael W. Hudsonbd3e7712002-03-18 13:06:00 +00007for 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)
23print
24
Fred Drakea4d18a02001-01-05 05:57:04 +000025def 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
35print "urlparse.urljoin() tests"
36print
37
38# "normal" cases from RFC 1808:
39checkJoin('g:h', 'g:h')
40checkJoin('g', 'http://a/b/c/g')
41checkJoin('./g', 'http://a/b/c/g')
42checkJoin('g/', 'http://a/b/c/g/')
43checkJoin('/g', 'http://a/g')
44checkJoin('//g', 'http://g')
45checkJoin('?y', 'http://a/b/c/d;p?y')
46checkJoin('g?y', 'http://a/b/c/g?y')
47checkJoin('g?y/./x', 'http://a/b/c/g?y/./x')
48checkJoin('#s', 'http://a/b/c/d;p?q#s')
49checkJoin('g#s', 'http://a/b/c/g#s')
50checkJoin('g#s/./x', 'http://a/b/c/g#s/./x')
51checkJoin('g?y#s', 'http://a/b/c/g?y#s')
52checkJoin(';x', 'http://a/b/c/d;x')
53checkJoin('g;x', 'http://a/b/c/g;x')
54checkJoin('g;x?y#s', 'http://a/b/c/g;x?y#s')
55checkJoin('.', 'http://a/b/c/')
56checkJoin('./', 'http://a/b/c/')
57checkJoin('..', 'http://a/b/')
58checkJoin('../', 'http://a/b/')
59checkJoin('../g', 'http://a/b/g')
60checkJoin('../..', 'http://a/')
61checkJoin('../../', 'http://a/')
62checkJoin('../../g', 'http://a/g')
63
64# "abnormal" cases from RFC 1808:
65checkJoin('', 'http://a/b/c/d;p?q#f')
66checkJoin('../../../g', 'http://a/../g')
67checkJoin('../../../../g', 'http://a/../../g')
68checkJoin('/./g', 'http://a/./g')
69checkJoin('/../g', 'http://a/../g')
70checkJoin('g.', 'http://a/b/c/g.')
71checkJoin('.g', 'http://a/b/c/.g')
72checkJoin('g..', 'http://a/b/c/g..')
73checkJoin('..g', 'http://a/b/c/..g')
74checkJoin('./../g', 'http://a/b/g')
75checkJoin('./g/.', 'http://a/b/c/g/')
76checkJoin('g/./h', 'http://a/b/c/g/h')
77checkJoin('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
84print errors, "errors"