ยปCore Development>Code coverage>Lib/test/test_urlparse.py

Python code coverage for Lib/test/test_urlparse.py

#countcontent
1n/aimport unittest
2n/aimport urllib.parse
3n/a
4n/aRFC1808_BASE = "http://a/b/c/d;p?q#f"
5n/aRFC2396_BASE = "http://a/b/c/d;p?q"
6n/aRFC3986_BASE = 'http://a/b/c/d;p?q'
7n/aSIMPLE_BASE = 'http://a/b/c/d'
8n/a
9n/a# A list of test cases. Each test case is a two-tuple that contains
10n/a# a string with the query and a dictionary with the expected result.
11n/a
12n/aparse_qsl_test_cases = [
13n/a ("", []),
14n/a ("&", []),
15n/a ("&&", []),
16n/a ("=", [('', '')]),
17n/a ("=a", [('', 'a')]),
18n/a ("a", [('a', '')]),
19n/a ("a=", [('a', '')]),
20n/a ("&a=b", [('a', 'b')]),
21n/a ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
22n/a ("a=1&a=2", [('a', '1'), ('a', '2')]),
23n/a (b"", []),
24n/a (b"&", []),
25n/a (b"&&", []),
26n/a (b"=", [(b'', b'')]),
27n/a (b"=a", [(b'', b'a')]),
28n/a (b"a", [(b'a', b'')]),
29n/a (b"a=", [(b'a', b'')]),
30n/a (b"&a=b", [(b'a', b'b')]),
31n/a (b"a=a+b&b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
32n/a (b"a=1&a=2", [(b'a', b'1'), (b'a', b'2')]),
33n/a (";", []),
34n/a (";;", []),
35n/a (";a=b", [('a', 'b')]),
36n/a ("a=a+b;b=b+c", [('a', 'a b'), ('b', 'b c')]),
37n/a ("a=1;a=2", [('a', '1'), ('a', '2')]),
38n/a (b";", []),
39n/a (b";;", []),
40n/a (b";a=b", [(b'a', b'b')]),
41n/a (b"a=a+b;b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
42n/a (b"a=1;a=2", [(b'a', b'1'), (b'a', b'2')]),
43n/a]
44n/a
45n/aparse_qs_test_cases = [
46n/a ("", {}),
47n/a ("&", {}),
48n/a ("&&", {}),
49n/a ("=", {'': ['']}),
50n/a ("=a", {'': ['a']}),
51n/a ("a", {'a': ['']}),
52n/a ("a=", {'a': ['']}),
53n/a ("&a=b", {'a': ['b']}),
54n/a ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
55n/a ("a=1&a=2", {'a': ['1', '2']}),
56n/a (b"", {}),
57n/a (b"&", {}),
58n/a (b"&&", {}),
59n/a (b"=", {b'': [b'']}),
60n/a (b"=a", {b'': [b'a']}),
61n/a (b"a", {b'a': [b'']}),
62n/a (b"a=", {b'a': [b'']}),
63n/a (b"&a=b", {b'a': [b'b']}),
64n/a (b"a=a+b&b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
65n/a (b"a=1&a=2", {b'a': [b'1', b'2']}),
66n/a (";", {}),
67n/a (";;", {}),
68n/a (";a=b", {'a': ['b']}),
69n/a ("a=a+b;b=b+c", {'a': ['a b'], 'b': ['b c']}),
70n/a ("a=1;a=2", {'a': ['1', '2']}),
71n/a (b";", {}),
72n/a (b";;", {}),
73n/a (b";a=b", {b'a': [b'b']}),
74n/a (b"a=a+b;b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
75n/a (b"a=1;a=2", {b'a': [b'1', b'2']}),
76n/a]
77n/a
78n/aclass UrlParseTestCase(unittest.TestCase):
79n/a
80n/a def checkRoundtrips(self, url, parsed, split):
81n/a result = urllib.parse.urlparse(url)
82n/a self.assertEqual(result, parsed)
83n/a t = (result.scheme, result.netloc, result.path,
84n/a result.params, result.query, result.fragment)
85n/a self.assertEqual(t, parsed)
86n/a # put it back together and it should be the same
87n/a result2 = urllib.parse.urlunparse(result)
88n/a self.assertEqual(result2, url)
89n/a self.assertEqual(result2, result.geturl())
90n/a
91n/a # the result of geturl() is a fixpoint; we can always parse it
92n/a # again to get the same result:
93n/a result3 = urllib.parse.urlparse(result.geturl())
94n/a self.assertEqual(result3.geturl(), result.geturl())
95n/a self.assertEqual(result3, result)
96n/a self.assertEqual(result3.scheme, result.scheme)
97n/a self.assertEqual(result3.netloc, result.netloc)
98n/a self.assertEqual(result3.path, result.path)
99n/a self.assertEqual(result3.params, result.params)
100n/a self.assertEqual(result3.query, result.query)
101n/a self.assertEqual(result3.fragment, result.fragment)
102n/a self.assertEqual(result3.username, result.username)
103n/a self.assertEqual(result3.password, result.password)
104n/a self.assertEqual(result3.hostname, result.hostname)
105n/a self.assertEqual(result3.port, result.port)
106n/a
107n/a # check the roundtrip using urlsplit() as well
108n/a result = urllib.parse.urlsplit(url)
109n/a self.assertEqual(result, split)
110n/a t = (result.scheme, result.netloc, result.path,
111n/a result.query, result.fragment)
112n/a self.assertEqual(t, split)
113n/a result2 = urllib.parse.urlunsplit(result)
114n/a self.assertEqual(result2, url)
115n/a self.assertEqual(result2, result.geturl())
116n/a
117n/a # check the fixpoint property of re-parsing the result of geturl()
118n/a result3 = urllib.parse.urlsplit(result.geturl())
119n/a self.assertEqual(result3.geturl(), result.geturl())
120n/a self.assertEqual(result3, result)
121n/a self.assertEqual(result3.scheme, result.scheme)
122n/a self.assertEqual(result3.netloc, result.netloc)
123n/a self.assertEqual(result3.path, result.path)
124n/a self.assertEqual(result3.query, result.query)
125n/a self.assertEqual(result3.fragment, result.fragment)
126n/a self.assertEqual(result3.username, result.username)
127n/a self.assertEqual(result3.password, result.password)
128n/a self.assertEqual(result3.hostname, result.hostname)
129n/a self.assertEqual(result3.port, result.port)
130n/a
131n/a def test_qsl(self):
132n/a for orig, expect in parse_qsl_test_cases:
133n/a result = urllib.parse.parse_qsl(orig, keep_blank_values=True)
134n/a self.assertEqual(result, expect, "Error parsing %r" % orig)
135n/a expect_without_blanks = [v for v in expect if len(v[1])]
136n/a result = urllib.parse.parse_qsl(orig, keep_blank_values=False)
137n/a self.assertEqual(result, expect_without_blanks,
138n/a "Error parsing %r" % orig)
139n/a
140n/a def test_qs(self):
141n/a for orig, expect in parse_qs_test_cases:
142n/a result = urllib.parse.parse_qs(orig, keep_blank_values=True)
143n/a self.assertEqual(result, expect, "Error parsing %r" % orig)
144n/a expect_without_blanks = {v: expect[v]
145n/a for v in expect if len(expect[v][0])}
146n/a result = urllib.parse.parse_qs(orig, keep_blank_values=False)
147n/a self.assertEqual(result, expect_without_blanks,
148n/a "Error parsing %r" % orig)
149n/a
150n/a def test_roundtrips(self):
151n/a str_cases = [
152n/a ('file:///tmp/junk.txt',
153n/a ('file', '', '/tmp/junk.txt', '', '', ''),
154n/a ('file', '', '/tmp/junk.txt', '', '')),
155n/a ('imap://mail.python.org/mbox1',
156n/a ('imap', 'mail.python.org', '/mbox1', '', '', ''),
157n/a ('imap', 'mail.python.org', '/mbox1', '', '')),
158n/a ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
159n/a ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
160n/a '', '', ''),
161n/a ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
162n/a '', '')),
163n/a ('nfs://server/path/to/file.txt',
164n/a ('nfs', 'server', '/path/to/file.txt', '', '', ''),
165n/a ('nfs', 'server', '/path/to/file.txt', '', '')),
166n/a ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
167n/a ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
168n/a '', '', ''),
169n/a ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
170n/a '', '')),
171n/a ('git+ssh://git@github.com/user/project.git',
172n/a ('git+ssh', 'git@github.com','/user/project.git',
173n/a '','',''),
174n/a ('git+ssh', 'git@github.com','/user/project.git',
175n/a '', '')),
176n/a ]
177n/a def _encode(t):
178n/a return (t[0].encode('ascii'),
179n/a tuple(x.encode('ascii') for x in t[1]),
180n/a tuple(x.encode('ascii') for x in t[2]))
181n/a bytes_cases = [_encode(x) for x in str_cases]
182n/a for url, parsed, split in str_cases + bytes_cases:
183n/a self.checkRoundtrips(url, parsed, split)
184n/a
185n/a def test_http_roundtrips(self):
186n/a # urllib.parse.urlsplit treats 'http:' as an optimized special case,
187n/a # so we test both 'http:' and 'https:' in all the following.
188n/a # Three cheers for white box knowledge!
189n/a str_cases = [
190n/a ('://www.python.org',
191n/a ('www.python.org', '', '', '', ''),
192n/a ('www.python.org', '', '', '')),
193n/a ('://www.python.org#abc',
194n/a ('www.python.org', '', '', '', 'abc'),
195n/a ('www.python.org', '', '', 'abc')),
196n/a ('://www.python.org?q=abc',
197n/a ('www.python.org', '', '', 'q=abc', ''),
198n/a ('www.python.org', '', 'q=abc', '')),
199n/a ('://www.python.org/#abc',
200n/a ('www.python.org', '/', '', '', 'abc'),
201n/a ('www.python.org', '/', '', 'abc')),
202n/a ('://a/b/c/d;p?q#f',
203n/a ('a', '/b/c/d', 'p', 'q', 'f'),
204n/a ('a', '/b/c/d;p', 'q', 'f')),
205n/a ]
206n/a def _encode(t):
207n/a return (t[0].encode('ascii'),
208n/a tuple(x.encode('ascii') for x in t[1]),
209n/a tuple(x.encode('ascii') for x in t[2]))
210n/a bytes_cases = [_encode(x) for x in str_cases]
211n/a str_schemes = ('http', 'https')
212n/a bytes_schemes = (b'http', b'https')
213n/a str_tests = str_schemes, str_cases
214n/a bytes_tests = bytes_schemes, bytes_cases
215n/a for schemes, test_cases in (str_tests, bytes_tests):
216n/a for scheme in schemes:
217n/a for url, parsed, split in test_cases:
218n/a url = scheme + url
219n/a parsed = (scheme,) + parsed
220n/a split = (scheme,) + split
221n/a self.checkRoundtrips(url, parsed, split)
222n/a
223n/a def checkJoin(self, base, relurl, expected):
224n/a str_components = (base, relurl, expected)
225n/a self.assertEqual(urllib.parse.urljoin(base, relurl), expected)
226n/a bytes_components = baseb, relurlb, expectedb = [
227n/a x.encode('ascii') for x in str_components]
228n/a self.assertEqual(urllib.parse.urljoin(baseb, relurlb), expectedb)
229n/a
230n/a def test_unparse_parse(self):
231n/a str_cases = ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]
232n/a bytes_cases = [x.encode('ascii') for x in str_cases]
233n/a for u in str_cases + bytes_cases:
234n/a self.assertEqual(urllib.parse.urlunsplit(urllib.parse.urlsplit(u)), u)
235n/a self.assertEqual(urllib.parse.urlunparse(urllib.parse.urlparse(u)), u)
236n/a
237n/a def test_RFC1808(self):
238n/a # "normal" cases from RFC 1808:
239n/a self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
240n/a self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
241n/a self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
242n/a self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
243n/a self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
244n/a self.checkJoin(RFC1808_BASE, '//g', 'http://g')
245n/a self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
246n/a self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
247n/a self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
248n/a self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
249n/a self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
250n/a self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
251n/a self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
252n/a self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
253n/a self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
254n/a self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
255n/a self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
256n/a self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
257n/a self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
258n/a self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
259n/a self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
260n/a self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
261n/a
262n/a # "abnormal" cases from RFC 1808:
263n/a self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
264n/a self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
265n/a self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
266n/a self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
267n/a self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
268n/a self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
269n/a self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
270n/a self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
271n/a self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
272n/a
273n/a # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
274n/a # so we'll not actually run these tests (which expect 1808 behavior).
275n/a #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
276n/a #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
277n/a
278n/a # XXX: The following tests are no longer compatible with RFC3986
279n/a # self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
280n/a # self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
281n/a # self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
282n/a # self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
283n/a
284n/a
285n/a def test_RFC2368(self):
286n/a # Issue 11467: path that starts with a number is not parsed correctly
287n/a self.assertEqual(urllib.parse.urlparse('mailto:1337@example.org'),
288n/a ('mailto', '', '1337@example.org', '', '', ''))
289n/a
290n/a def test_RFC2396(self):
291n/a # cases from RFC 2396
292n/a
293n/a
294n/a self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
295n/a self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
296n/a self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
297n/a self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
298n/a self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
299n/a self.checkJoin(RFC2396_BASE, '//g', 'http://g')
300n/a self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
301n/a self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
302n/a self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
303n/a self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
304n/a self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
305n/a self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
306n/a self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
307n/a self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
308n/a self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
309n/a self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
310n/a self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
311n/a self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
312n/a self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
313n/a self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
314n/a self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
315n/a self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
316n/a self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
317n/a self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
318n/a self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
319n/a self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
320n/a self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
321n/a self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
322n/a self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
323n/a self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
324n/a self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
325n/a self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
326n/a self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
327n/a self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
328n/a self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
329n/a
330n/a # XXX: The following tests are no longer compatible with RFC3986
331n/a # self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
332n/a # self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
333n/a # self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
334n/a # self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
335n/a
336n/a
337n/a def test_RFC3986(self):
338n/a # Test cases from RFC3986
339n/a self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
340n/a self.checkJoin(RFC3986_BASE, ';x', 'http://a/b/c/;x')
341n/a self.checkJoin(RFC3986_BASE, 'g:h','g:h')
342n/a self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
343n/a self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
344n/a self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
345n/a self.checkJoin(RFC3986_BASE, '/g','http://a/g')
346n/a self.checkJoin(RFC3986_BASE, '//g','http://g')
347n/a self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
348n/a self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
349n/a self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
350n/a self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
351n/a self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
352n/a self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
353n/a self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
354n/a self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
355n/a self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
356n/a self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
357n/a self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
358n/a self.checkJoin(RFC3986_BASE, '..','http://a/b/')
359n/a self.checkJoin(RFC3986_BASE, '../','http://a/b/')
360n/a self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
361n/a self.checkJoin(RFC3986_BASE, '../..','http://a/')
362n/a self.checkJoin(RFC3986_BASE, '../../','http://a/')
363n/a self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
364n/a self.checkJoin(RFC3986_BASE, '../../../g', 'http://a/g')
365n/a
366n/a #Abnormal Examples
367n/a
368n/a # The 'abnormal scenarios' are incompatible with RFC2986 parsing
369n/a # Tests are here for reference.
370n/a
371n/a self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
372n/a self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
373n/a self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
374n/a self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
375n/a self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
376n/a self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
377n/a self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
378n/a self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
379n/a self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
380n/a self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
381n/a self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
382n/a self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
383n/a self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
384n/a self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
385n/a self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
386n/a self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
387n/a self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
388n/a self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
389n/a #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
390n/a self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') #relaxed parser
391n/a
392n/a # Test for issue9721
393n/a self.checkJoin('http://a/b/c/de', ';x','http://a/b/c/;x')
394n/a
395n/a def test_urljoins(self):
396n/a self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
397n/a self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
398n/a self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
399n/a self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
400n/a self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
401n/a self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
402n/a self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
403n/a self.checkJoin(SIMPLE_BASE, '//g','http://g')
404n/a self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
405n/a self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
406n/a self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
407n/a self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
408n/a self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
409n/a self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
410n/a self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
411n/a self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
412n/a self.checkJoin(SIMPLE_BASE, '../..','http://a/')
413n/a self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
414n/a self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
415n/a self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
416n/a self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
417n/a self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
418n/a self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
419n/a self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
420n/a self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
421n/a self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
422n/a self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
423n/a self.checkJoin('http:///', '..','http:///')
424n/a self.checkJoin('', 'http://a/b/c/g?y/./x','http://a/b/c/g?y/./x')
425n/a self.checkJoin('', 'http://a/./g', 'http://a/./g')
426n/a self.checkJoin('svn://pathtorepo/dir1', 'dir2', 'svn://pathtorepo/dir2')
427n/a self.checkJoin('svn+ssh://pathtorepo/dir1', 'dir2', 'svn+ssh://pathtorepo/dir2')
428n/a self.checkJoin('ws://a/b','g','ws://a/g')
429n/a self.checkJoin('wss://a/b','g','wss://a/g')
430n/a
431n/a # XXX: The following tests are no longer compatible with RFC3986
432n/a # self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
433n/a # self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
434n/a
435n/a # test for issue22118 duplicate slashes
436n/a self.checkJoin(SIMPLE_BASE + '/', 'foo', SIMPLE_BASE + '/foo')
437n/a
438n/a # Non-RFC-defined tests, covering variations of base and trailing
439n/a # slashes
440n/a self.checkJoin('http://a/b/c/d/e/', '../../f/g/', 'http://a/b/c/f/g/')
441n/a self.checkJoin('http://a/b/c/d/e', '../../f/g/', 'http://a/b/f/g/')
442n/a self.checkJoin('http://a/b/c/d/e/', '/../../f/g/', 'http://a/f/g/')
443n/a self.checkJoin('http://a/b/c/d/e', '/../../f/g/', 'http://a/f/g/')
444n/a self.checkJoin('http://a/b/c/d/e/', '../../f/g', 'http://a/b/c/f/g')
445n/a self.checkJoin('http://a/b/', '../../f/g/', 'http://a/f/g/')
446n/a
447n/a # issue 23703: don't duplicate filename
448n/a self.checkJoin('a', 'b', 'b')
449n/a
450n/a def test_RFC2732(self):
451n/a str_cases = [
452n/a ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
453n/a ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
454n/a ('http://[::1]:5432/foo/', '::1', 5432),
455n/a ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
456n/a ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
457n/a ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
458n/a 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
459n/a ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
460n/a ('http://[::ffff:12.34.56.78]:5432/foo/',
461n/a '::ffff:12.34.56.78', 5432),
462n/a ('http://Test.python.org/foo/', 'test.python.org', None),
463n/a ('http://12.34.56.78/foo/', '12.34.56.78', None),
464n/a ('http://[::1]/foo/', '::1', None),
465n/a ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
466n/a ('http://[dead:beef::]/foo/', 'dead:beef::', None),
467n/a ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
468n/a 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
469n/a ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
470n/a ('http://[::ffff:12.34.56.78]/foo/',
471n/a '::ffff:12.34.56.78', None),
472n/a ('http://Test.python.org:/foo/', 'test.python.org', None),
473n/a ('http://12.34.56.78:/foo/', '12.34.56.78', None),
474n/a ('http://[::1]:/foo/', '::1', None),
475n/a ('http://[dead:beef::1]:/foo/', 'dead:beef::1', None),
476n/a ('http://[dead:beef::]:/foo/', 'dead:beef::', None),
477n/a ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:/foo/',
478n/a 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
479n/a ('http://[::12.34.56.78]:/foo/', '::12.34.56.78', None),
480n/a ('http://[::ffff:12.34.56.78]:/foo/',
481n/a '::ffff:12.34.56.78', None),
482n/a ]
483n/a def _encode(t):
484n/a return t[0].encode('ascii'), t[1].encode('ascii'), t[2]
485n/a bytes_cases = [_encode(x) for x in str_cases]
486n/a for url, hostname, port in str_cases + bytes_cases:
487n/a urlparsed = urllib.parse.urlparse(url)
488n/a self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port))
489n/a
490n/a str_cases = [
491n/a 'http://::12.34.56.78]/',
492n/a 'http://[::1/foo/',
493n/a 'ftp://[::1/foo/bad]/bad',
494n/a 'http://[::1/foo/bad]/bad',
495n/a 'http://[::ffff:12.34.56.78']
496n/a bytes_cases = [x.encode('ascii') for x in str_cases]
497n/a for invalid_url in str_cases + bytes_cases:
498n/a self.assertRaises(ValueError, urllib.parse.urlparse, invalid_url)
499n/a
500n/a def test_urldefrag(self):
501n/a str_cases = [
502n/a ('http://python.org#frag', 'http://python.org', 'frag'),
503n/a ('http://python.org', 'http://python.org', ''),
504n/a ('http://python.org/#frag', 'http://python.org/', 'frag'),
505n/a ('http://python.org/', 'http://python.org/', ''),
506n/a ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
507n/a ('http://python.org/?q', 'http://python.org/?q', ''),
508n/a ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
509n/a ('http://python.org/p?q', 'http://python.org/p?q', ''),
510n/a (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
511n/a (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
512n/a ]
513n/a def _encode(t):
514n/a return type(t)(x.encode('ascii') for x in t)
515n/a bytes_cases = [_encode(x) for x in str_cases]
516n/a for url, defrag, frag in str_cases + bytes_cases:
517n/a result = urllib.parse.urldefrag(url)
518n/a self.assertEqual(result.geturl(), url)
519n/a self.assertEqual(result, (defrag, frag))
520n/a self.assertEqual(result.url, defrag)
521n/a self.assertEqual(result.fragment, frag)
522n/a
523n/a def test_urlsplit_attributes(self):
524n/a url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
525n/a p = urllib.parse.urlsplit(url)
526n/a self.assertEqual(p.scheme, "http")
527n/a self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
528n/a self.assertEqual(p.path, "/doc/")
529n/a self.assertEqual(p.query, "")
530n/a self.assertEqual(p.fragment, "frag")
531n/a self.assertEqual(p.username, None)
532n/a self.assertEqual(p.password, None)
533n/a self.assertEqual(p.hostname, "www.python.org")
534n/a self.assertEqual(p.port, None)
535n/a # geturl() won't return exactly the original URL in this case
536n/a # since the scheme is always case-normalized
537n/a # We handle this by ignoring the first 4 characters of the URL
538n/a self.assertEqual(p.geturl()[4:], url[4:])
539n/a
540n/a url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
541n/a p = urllib.parse.urlsplit(url)
542n/a self.assertEqual(p.scheme, "http")
543n/a self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
544n/a self.assertEqual(p.path, "/doc/")
545n/a self.assertEqual(p.query, "query=yes")
546n/a self.assertEqual(p.fragment, "frag")
547n/a self.assertEqual(p.username, "User")
548n/a self.assertEqual(p.password, "Pass")
549n/a self.assertEqual(p.hostname, "www.python.org")
550n/a self.assertEqual(p.port, 80)
551n/a self.assertEqual(p.geturl(), url)
552n/a
553n/a # Addressing issue1698, which suggests Username can contain
554n/a # "@" characters. Though not RFC compliant, many ftp sites allow
555n/a # and request email addresses as usernames.
556n/a
557n/a url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
558n/a p = urllib.parse.urlsplit(url)
559n/a self.assertEqual(p.scheme, "http")
560n/a self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
561n/a self.assertEqual(p.path, "/doc/")
562n/a self.assertEqual(p.query, "query=yes")
563n/a self.assertEqual(p.fragment, "frag")
564n/a self.assertEqual(p.username, "User@example.com")
565n/a self.assertEqual(p.password, "Pass")
566n/a self.assertEqual(p.hostname, "www.python.org")
567n/a self.assertEqual(p.port, 80)
568n/a self.assertEqual(p.geturl(), url)
569n/a
570n/a # And check them all again, only with bytes this time
571n/a url = b"HTTP://WWW.PYTHON.ORG/doc/#frag"
572n/a p = urllib.parse.urlsplit(url)
573n/a self.assertEqual(p.scheme, b"http")
574n/a self.assertEqual(p.netloc, b"WWW.PYTHON.ORG")
575n/a self.assertEqual(p.path, b"/doc/")
576n/a self.assertEqual(p.query, b"")
577n/a self.assertEqual(p.fragment, b"frag")
578n/a self.assertEqual(p.username, None)
579n/a self.assertEqual(p.password, None)
580n/a self.assertEqual(p.hostname, b"www.python.org")
581n/a self.assertEqual(p.port, None)
582n/a self.assertEqual(p.geturl()[4:], url[4:])
583n/a
584n/a url = b"http://User:Pass@www.python.org:080/doc/?query=yes#frag"
585n/a p = urllib.parse.urlsplit(url)
586n/a self.assertEqual(p.scheme, b"http")
587n/a self.assertEqual(p.netloc, b"User:Pass@www.python.org:080")
588n/a self.assertEqual(p.path, b"/doc/")
589n/a self.assertEqual(p.query, b"query=yes")
590n/a self.assertEqual(p.fragment, b"frag")
591n/a self.assertEqual(p.username, b"User")
592n/a self.assertEqual(p.password, b"Pass")
593n/a self.assertEqual(p.hostname, b"www.python.org")
594n/a self.assertEqual(p.port, 80)
595n/a self.assertEqual(p.geturl(), url)
596n/a
597n/a url = b"http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
598n/a p = urllib.parse.urlsplit(url)
599n/a self.assertEqual(p.scheme, b"http")
600n/a self.assertEqual(p.netloc, b"User@example.com:Pass@www.python.org:080")
601n/a self.assertEqual(p.path, b"/doc/")
602n/a self.assertEqual(p.query, b"query=yes")
603n/a self.assertEqual(p.fragment, b"frag")
604n/a self.assertEqual(p.username, b"User@example.com")
605n/a self.assertEqual(p.password, b"Pass")
606n/a self.assertEqual(p.hostname, b"www.python.org")
607n/a self.assertEqual(p.port, 80)
608n/a self.assertEqual(p.geturl(), url)
609n/a
610n/a # Verify an illegal port raises ValueError
611n/a url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag"
612n/a p = urllib.parse.urlsplit(url)
613n/a with self.assertRaisesRegex(ValueError, "out of range"):
614n/a p.port
615n/a
616n/a def test_attributes_bad_port(self):
617n/a """Check handling of invalid ports."""
618n/a for bytes in (False, True):
619n/a for parse in (urllib.parse.urlsplit, urllib.parse.urlparse):
620n/a for port in ("foo", "1.5", "-1", "0x10"):
621n/a with self.subTest(bytes=bytes, parse=parse, port=port):
622n/a netloc = "www.example.net:" + port
623n/a url = "http://" + netloc
624n/a if bytes:
625n/a netloc = netloc.encode("ascii")
626n/a url = url.encode("ascii")
627n/a p = parse(url)
628n/a self.assertEqual(p.netloc, netloc)
629n/a with self.assertRaises(ValueError):
630n/a p.port
631n/a
632n/a def test_attributes_without_netloc(self):
633n/a # This example is straight from RFC 3261. It looks like it
634n/a # should allow the username, hostname, and port to be filled
635n/a # in, but doesn't. Since it's a URI and doesn't use the
636n/a # scheme://netloc syntax, the netloc and related attributes
637n/a # should be left empty.
638n/a uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
639n/a p = urllib.parse.urlsplit(uri)
640n/a self.assertEqual(p.netloc, "")
641n/a self.assertEqual(p.username, None)
642n/a self.assertEqual(p.password, None)
643n/a self.assertEqual(p.hostname, None)
644n/a self.assertEqual(p.port, None)
645n/a self.assertEqual(p.geturl(), uri)
646n/a
647n/a p = urllib.parse.urlparse(uri)
648n/a self.assertEqual(p.netloc, "")
649n/a self.assertEqual(p.username, None)
650n/a self.assertEqual(p.password, None)
651n/a self.assertEqual(p.hostname, None)
652n/a self.assertEqual(p.port, None)
653n/a self.assertEqual(p.geturl(), uri)
654n/a
655n/a # You guessed it, repeating the test with bytes input
656n/a uri = b"sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
657n/a p = urllib.parse.urlsplit(uri)
658n/a self.assertEqual(p.netloc, b"")
659n/a self.assertEqual(p.username, None)
660n/a self.assertEqual(p.password, None)
661n/a self.assertEqual(p.hostname, None)
662n/a self.assertEqual(p.port, None)
663n/a self.assertEqual(p.geturl(), uri)
664n/a
665n/a p = urllib.parse.urlparse(uri)
666n/a self.assertEqual(p.netloc, b"")
667n/a self.assertEqual(p.username, None)
668n/a self.assertEqual(p.password, None)
669n/a self.assertEqual(p.hostname, None)
670n/a self.assertEqual(p.port, None)
671n/a self.assertEqual(p.geturl(), uri)
672n/a
673n/a def test_noslash(self):
674n/a # Issue 1637: http://foo.com?query is legal
675n/a self.assertEqual(urllib.parse.urlparse("http://example.com?blahblah=/foo"),
676n/a ('http', 'example.com', '', '', 'blahblah=/foo', ''))
677n/a self.assertEqual(urllib.parse.urlparse(b"http://example.com?blahblah=/foo"),
678n/a (b'http', b'example.com', b'', b'', b'blahblah=/foo', b''))
679n/a
680n/a def test_withoutscheme(self):
681n/a # Test urlparse without scheme
682n/a # Issue 754016: urlparse goes wrong with IP:port without scheme
683n/a # RFC 1808 specifies that netloc should start with //, urlparse expects
684n/a # the same, otherwise it classifies the portion of url as path.
685n/a self.assertEqual(urllib.parse.urlparse("path"),
686n/a ('','','path','','',''))
687n/a self.assertEqual(urllib.parse.urlparse("//www.python.org:80"),
688n/a ('','www.python.org:80','','','',''))
689n/a self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"),
690n/a ('http','www.python.org:80','','','',''))
691n/a # Repeat for bytes input
692n/a self.assertEqual(urllib.parse.urlparse(b"path"),
693n/a (b'',b'',b'path',b'',b'',b''))
694n/a self.assertEqual(urllib.parse.urlparse(b"//www.python.org:80"),
695n/a (b'',b'www.python.org:80',b'',b'',b'',b''))
696n/a self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"),
697n/a (b'http',b'www.python.org:80',b'',b'',b'',b''))
698n/a
699n/a def test_portseparator(self):
700n/a # Issue 754016 makes changes for port separator ':' from scheme separator
701n/a self.assertEqual(urllib.parse.urlparse("path:80"),
702n/a ('','','path:80','','',''))
703n/a self.assertEqual(urllib.parse.urlparse("http:"),('http','','','','',''))
704n/a self.assertEqual(urllib.parse.urlparse("https:"),('https','','','','',''))
705n/a self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"),
706n/a ('http','www.python.org:80','','','',''))
707n/a # As usual, need to check bytes input as well
708n/a self.assertEqual(urllib.parse.urlparse(b"path:80"),
709n/a (b'',b'',b'path:80',b'',b'',b''))
710n/a self.assertEqual(urllib.parse.urlparse(b"http:"),(b'http',b'',b'',b'',b'',b''))
711n/a self.assertEqual(urllib.parse.urlparse(b"https:"),(b'https',b'',b'',b'',b'',b''))
712n/a self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"),
713n/a (b'http',b'www.python.org:80',b'',b'',b'',b''))
714n/a
715n/a def test_usingsys(self):
716n/a # Issue 3314: sys module is used in the error
717n/a self.assertRaises(TypeError, urllib.parse.urlencode, "foo")
718n/a
719n/a def test_anyscheme(self):
720n/a # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
721n/a self.assertEqual(urllib.parse.urlparse("s3://foo.com/stuff"),
722n/a ('s3', 'foo.com', '/stuff', '', '', ''))
723n/a self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff"),
724n/a ('x-newscheme', 'foo.com', '/stuff', '', '', ''))
725n/a self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query#fragment"),
726n/a ('x-newscheme', 'foo.com', '/stuff', '', 'query', 'fragment'))
727n/a self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query"),
728n/a ('x-newscheme', 'foo.com', '/stuff', '', 'query', ''))
729n/a
730n/a # And for bytes...
731n/a self.assertEqual(urllib.parse.urlparse(b"s3://foo.com/stuff"),
732n/a (b's3', b'foo.com', b'/stuff', b'', b'', b''))
733n/a self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff"),
734n/a (b'x-newscheme', b'foo.com', b'/stuff', b'', b'', b''))
735n/a self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query#fragment"),
736n/a (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b'fragment'))
737n/a self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query"),
738n/a (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b''))
739n/a
740n/a def test_default_scheme(self):
741n/a # Exercise the scheme parameter of urlparse() and urlsplit()
742n/a for func in (urllib.parse.urlparse, urllib.parse.urlsplit):
743n/a with self.subTest(function=func):
744n/a result = func("http://example.net/", "ftp")
745n/a self.assertEqual(result.scheme, "http")
746n/a result = func(b"http://example.net/", b"ftp")
747n/a self.assertEqual(result.scheme, b"http")
748n/a self.assertEqual(func("path", "ftp").scheme, "ftp")
749n/a self.assertEqual(func("path", scheme="ftp").scheme, "ftp")
750n/a self.assertEqual(func(b"path", scheme=b"ftp").scheme, b"ftp")
751n/a self.assertEqual(func("path").scheme, "")
752n/a self.assertEqual(func(b"path").scheme, b"")
753n/a self.assertEqual(func(b"path", "").scheme, b"")
754n/a
755n/a def test_parse_fragments(self):
756n/a # Exercise the allow_fragments parameter of urlparse() and urlsplit()
757n/a tests = (
758n/a ("http:#frag", "path"),
759n/a ("//example.net#frag", "path"),
760n/a ("index.html#frag", "path"),
761n/a (";a=b#frag", "params"),
762n/a ("?a=b#frag", "query"),
763n/a ("#frag", "path"),
764n/a )
765n/a for url, attr in tests:
766n/a for func in (urllib.parse.urlparse, urllib.parse.urlsplit):
767n/a if attr == "params" and func is urllib.parse.urlsplit:
768n/a attr = "path"
769n/a with self.subTest(url=url, function=func):
770n/a result = func(url, allow_fragments=False)
771n/a self.assertEqual(result.fragment, "")
772n/a self.assertTrue(getattr(result, attr).endswith("#frag"))
773n/a self.assertEqual(func(url, "", False).fragment, "")
774n/a
775n/a result = func(url, allow_fragments=True)
776n/a self.assertEqual(result.fragment, "frag")
777n/a self.assertFalse(getattr(result, attr).endswith("frag"))
778n/a self.assertEqual(func(url, "", True).fragment, "frag")
779n/a self.assertEqual(func(url).fragment, "frag")
780n/a
781n/a def test_mixed_types_rejected(self):
782n/a # Several functions that process either strings or ASCII encoded bytes
783n/a # accept multiple arguments. Check they reject mixed type input
784n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
785n/a urllib.parse.urlparse("www.python.org", b"http")
786n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
787n/a urllib.parse.urlparse(b"www.python.org", "http")
788n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
789n/a urllib.parse.urlsplit("www.python.org", b"http")
790n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
791n/a urllib.parse.urlsplit(b"www.python.org", "http")
792n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
793n/a urllib.parse.urlunparse(( b"http", "www.python.org","","","",""))
794n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
795n/a urllib.parse.urlunparse(("http", b"www.python.org","","","",""))
796n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
797n/a urllib.parse.urlunsplit((b"http", "www.python.org","","",""))
798n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
799n/a urllib.parse.urlunsplit(("http", b"www.python.org","","",""))
800n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
801n/a urllib.parse.urljoin("http://python.org", b"http://python.org")
802n/a with self.assertRaisesRegex(TypeError, "Cannot mix str"):
803n/a urllib.parse.urljoin(b"http://python.org", "http://python.org")
804n/a
805n/a def _check_result_type(self, str_type):
806n/a num_args = len(str_type._fields)
807n/a bytes_type = str_type._encoded_counterpart
808n/a self.assertIs(bytes_type._decoded_counterpart, str_type)
809n/a str_args = ('',) * num_args
810n/a bytes_args = (b'',) * num_args
811n/a str_result = str_type(*str_args)
812n/a bytes_result = bytes_type(*bytes_args)
813n/a encoding = 'ascii'
814n/a errors = 'strict'
815n/a self.assertEqual(str_result, str_args)
816n/a self.assertEqual(bytes_result.decode(), str_args)
817n/a self.assertEqual(bytes_result.decode(), str_result)
818n/a self.assertEqual(bytes_result.decode(encoding), str_args)
819n/a self.assertEqual(bytes_result.decode(encoding), str_result)
820n/a self.assertEqual(bytes_result.decode(encoding, errors), str_args)
821n/a self.assertEqual(bytes_result.decode(encoding, errors), str_result)
822n/a self.assertEqual(bytes_result, bytes_args)
823n/a self.assertEqual(str_result.encode(), bytes_args)
824n/a self.assertEqual(str_result.encode(), bytes_result)
825n/a self.assertEqual(str_result.encode(encoding), bytes_args)
826n/a self.assertEqual(str_result.encode(encoding), bytes_result)
827n/a self.assertEqual(str_result.encode(encoding, errors), bytes_args)
828n/a self.assertEqual(str_result.encode(encoding, errors), bytes_result)
829n/a
830n/a def test_result_pairs(self):
831n/a # Check encoding and decoding between result pairs
832n/a result_types = [
833n/a urllib.parse.DefragResult,
834n/a urllib.parse.SplitResult,
835n/a urllib.parse.ParseResult,
836n/a ]
837n/a for result_type in result_types:
838n/a self._check_result_type(result_type)
839n/a
840n/a def test_parse_qs_encoding(self):
841n/a result = urllib.parse.parse_qs("key=\u0141%E9", encoding="latin-1")
842n/a self.assertEqual(result, {'key': ['\u0141\xE9']})
843n/a result = urllib.parse.parse_qs("key=\u0141%C3%A9", encoding="utf-8")
844n/a self.assertEqual(result, {'key': ['\u0141\xE9']})
845n/a result = urllib.parse.parse_qs("key=\u0141%C3%A9", encoding="ascii")
846n/a self.assertEqual(result, {'key': ['\u0141\ufffd\ufffd']})
847n/a result = urllib.parse.parse_qs("key=\u0141%E9-", encoding="ascii")
848n/a self.assertEqual(result, {'key': ['\u0141\ufffd-']})
849n/a result = urllib.parse.parse_qs("key=\u0141%E9-", encoding="ascii",
850n/a errors="ignore")
851n/a self.assertEqual(result, {'key': ['\u0141-']})
852n/a
853n/a def test_parse_qsl_encoding(self):
854n/a result = urllib.parse.parse_qsl("key=\u0141%E9", encoding="latin-1")
855n/a self.assertEqual(result, [('key', '\u0141\xE9')])
856n/a result = urllib.parse.parse_qsl("key=\u0141%C3%A9", encoding="utf-8")
857n/a self.assertEqual(result, [('key', '\u0141\xE9')])
858n/a result = urllib.parse.parse_qsl("key=\u0141%C3%A9", encoding="ascii")
859n/a self.assertEqual(result, [('key', '\u0141\ufffd\ufffd')])
860n/a result = urllib.parse.parse_qsl("key=\u0141%E9-", encoding="ascii")
861n/a self.assertEqual(result, [('key', '\u0141\ufffd-')])
862n/a result = urllib.parse.parse_qsl("key=\u0141%E9-", encoding="ascii",
863n/a errors="ignore")
864n/a self.assertEqual(result, [('key', '\u0141-')])
865n/a
866n/a def test_urlencode_sequences(self):
867n/a # Other tests incidentally urlencode things; test non-covered cases:
868n/a # Sequence and object values.
869n/a result = urllib.parse.urlencode({'a': [1, 2], 'b': (3, 4, 5)}, True)
870n/a # we cannot rely on ordering here
871n/a assert set(result.split('&')) == {'a=1', 'a=2', 'b=3', 'b=4', 'b=5'}
872n/a
873n/a class Trivial:
874n/a def __str__(self):
875n/a return 'trivial'
876n/a
877n/a result = urllib.parse.urlencode({'a': Trivial()}, True)
878n/a self.assertEqual(result, 'a=trivial')
879n/a
880n/a def test_urlencode_quote_via(self):
881n/a result = urllib.parse.urlencode({'a': 'some value'})
882n/a self.assertEqual(result, "a=some+value")
883n/a result = urllib.parse.urlencode({'a': 'some value/another'},
884n/a quote_via=urllib.parse.quote)
885n/a self.assertEqual(result, "a=some%20value%2Fanother")
886n/a result = urllib.parse.urlencode({'a': 'some value/another'},
887n/a safe='/', quote_via=urllib.parse.quote)
888n/a self.assertEqual(result, "a=some%20value/another")
889n/a
890n/a def test_quote_from_bytes(self):
891n/a self.assertRaises(TypeError, urllib.parse.quote_from_bytes, 'foo')
892n/a result = urllib.parse.quote_from_bytes(b'archaeological arcana')
893n/a self.assertEqual(result, 'archaeological%20arcana')
894n/a result = urllib.parse.quote_from_bytes(b'')
895n/a self.assertEqual(result, '')
896n/a
897n/a def test_unquote_to_bytes(self):
898n/a result = urllib.parse.unquote_to_bytes('abc%20def')
899n/a self.assertEqual(result, b'abc def')
900n/a result = urllib.parse.unquote_to_bytes('')
901n/a self.assertEqual(result, b'')
902n/a
903n/a def test_quote_errors(self):
904n/a self.assertRaises(TypeError, urllib.parse.quote, b'foo',
905n/a encoding='utf-8')
906n/a self.assertRaises(TypeError, urllib.parse.quote, b'foo', errors='strict')
907n/a
908n/a def test_issue14072(self):
909n/a p1 = urllib.parse.urlsplit('tel:+31-641044153')
910n/a self.assertEqual(p1.scheme, 'tel')
911n/a self.assertEqual(p1.path, '+31-641044153')
912n/a p2 = urllib.parse.urlsplit('tel:+31641044153')
913n/a self.assertEqual(p2.scheme, 'tel')
914n/a self.assertEqual(p2.path, '+31641044153')
915n/a # assert the behavior for urlparse
916n/a p1 = urllib.parse.urlparse('tel:+31-641044153')
917n/a self.assertEqual(p1.scheme, 'tel')
918n/a self.assertEqual(p1.path, '+31-641044153')
919n/a p2 = urllib.parse.urlparse('tel:+31641044153')
920n/a self.assertEqual(p2.scheme, 'tel')
921n/a self.assertEqual(p2.path, '+31641044153')
922n/a
923n/a def test_telurl_params(self):
924n/a p1 = urllib.parse.urlparse('tel:123-4;phone-context=+1-650-516')
925n/a self.assertEqual(p1.scheme, 'tel')
926n/a self.assertEqual(p1.path, '123-4')
927n/a self.assertEqual(p1.params, 'phone-context=+1-650-516')
928n/a
929n/a p1 = urllib.parse.urlparse('tel:+1-201-555-0123')
930n/a self.assertEqual(p1.scheme, 'tel')
931n/a self.assertEqual(p1.path, '+1-201-555-0123')
932n/a self.assertEqual(p1.params, '')
933n/a
934n/a p1 = urllib.parse.urlparse('tel:7042;phone-context=example.com')
935n/a self.assertEqual(p1.scheme, 'tel')
936n/a self.assertEqual(p1.path, '7042')
937n/a self.assertEqual(p1.params, 'phone-context=example.com')
938n/a
939n/a p1 = urllib.parse.urlparse('tel:863-1234;phone-context=+1-914-555')
940n/a self.assertEqual(p1.scheme, 'tel')
941n/a self.assertEqual(p1.path, '863-1234')
942n/a self.assertEqual(p1.params, 'phone-context=+1-914-555')
943n/a
944n/a def test_Quoter_repr(self):
945n/a quoter = urllib.parse.Quoter(urllib.parse._ALWAYS_SAFE)
946n/a self.assertIn('Quoter', repr(quoter))
947n/a
948n/a def test_all(self):
949n/a expected = []
950n/a undocumented = {
951n/a 'splitattr', 'splithost', 'splitnport', 'splitpasswd',
952n/a 'splitport', 'splitquery', 'splittag', 'splittype', 'splituser',
953n/a 'splitvalue',
954n/a 'Quoter', 'ResultBase', 'clear_cache', 'to_bytes', 'unwrap',
955n/a }
956n/a for name in dir(urllib.parse):
957n/a if name.startswith('_') or name in undocumented:
958n/a continue
959n/a object = getattr(urllib.parse, name)
960n/a if getattr(object, '__module__', None) == 'urllib.parse':
961n/a expected.append(name)
962n/a self.assertCountEqual(urllib.parse.__all__, expected)
963n/a
964n/a
965n/aclass Utility_Tests(unittest.TestCase):
966n/a """Testcase to test the various utility functions in the urllib."""
967n/a # In Python 2 this test class was in test_urllib.
968n/a
969n/a def test_splittype(self):
970n/a splittype = urllib.parse.splittype
971n/a self.assertEqual(splittype('type:opaquestring'), ('type', 'opaquestring'))
972n/a self.assertEqual(splittype('opaquestring'), (None, 'opaquestring'))
973n/a self.assertEqual(splittype(':opaquestring'), (None, ':opaquestring'))
974n/a self.assertEqual(splittype('type:'), ('type', ''))
975n/a self.assertEqual(splittype('type:opaque:string'), ('type', 'opaque:string'))
976n/a
977n/a def test_splithost(self):
978n/a splithost = urllib.parse.splithost
979n/a self.assertEqual(splithost('//www.example.org:80/foo/bar/baz.html'),
980n/a ('www.example.org:80', '/foo/bar/baz.html'))
981n/a self.assertEqual(splithost('//www.example.org:80'),
982n/a ('www.example.org:80', ''))
983n/a self.assertEqual(splithost('/foo/bar/baz.html'),
984n/a (None, '/foo/bar/baz.html'))
985n/a
986n/a def test_splituser(self):
987n/a splituser = urllib.parse.splituser
988n/a self.assertEqual(splituser('User:Pass@www.python.org:080'),
989n/a ('User:Pass', 'www.python.org:080'))
990n/a self.assertEqual(splituser('@www.python.org:080'),
991n/a ('', 'www.python.org:080'))
992n/a self.assertEqual(splituser('www.python.org:080'),
993n/a (None, 'www.python.org:080'))
994n/a self.assertEqual(splituser('User:Pass@'),
995n/a ('User:Pass', ''))
996n/a self.assertEqual(splituser('User@example.com:Pass@www.python.org:080'),
997n/a ('User@example.com:Pass', 'www.python.org:080'))
998n/a
999n/a def test_splitpasswd(self):
1000n/a # Some of the password examples are not sensible, but it is added to
1001n/a # confirming to RFC2617 and addressing issue4675.
1002n/a splitpasswd = urllib.parse.splitpasswd
1003n/a self.assertEqual(splitpasswd('user:ab'), ('user', 'ab'))
1004n/a self.assertEqual(splitpasswd('user:a\nb'), ('user', 'a\nb'))
1005n/a self.assertEqual(splitpasswd('user:a\tb'), ('user', 'a\tb'))
1006n/a self.assertEqual(splitpasswd('user:a\rb'), ('user', 'a\rb'))
1007n/a self.assertEqual(splitpasswd('user:a\fb'), ('user', 'a\fb'))
1008n/a self.assertEqual(splitpasswd('user:a\vb'), ('user', 'a\vb'))
1009n/a self.assertEqual(splitpasswd('user:a:b'), ('user', 'a:b'))
1010n/a self.assertEqual(splitpasswd('user:a b'), ('user', 'a b'))
1011n/a self.assertEqual(splitpasswd('user 2:ab'), ('user 2', 'ab'))
1012n/a self.assertEqual(splitpasswd('user+1:a+b'), ('user+1', 'a+b'))
1013n/a self.assertEqual(splitpasswd('user:'), ('user', ''))
1014n/a self.assertEqual(splitpasswd('user'), ('user', None))
1015n/a self.assertEqual(splitpasswd(':ab'), ('', 'ab'))
1016n/a
1017n/a def test_splitport(self):
1018n/a splitport = urllib.parse.splitport
1019n/a self.assertEqual(splitport('parrot:88'), ('parrot', '88'))
1020n/a self.assertEqual(splitport('parrot'), ('parrot', None))
1021n/a self.assertEqual(splitport('parrot:'), ('parrot', None))
1022n/a self.assertEqual(splitport('127.0.0.1'), ('127.0.0.1', None))
1023n/a self.assertEqual(splitport('parrot:cheese'), ('parrot:cheese', None))
1024n/a self.assertEqual(splitport('[::1]:88'), ('[::1]', '88'))
1025n/a self.assertEqual(splitport('[::1]'), ('[::1]', None))
1026n/a self.assertEqual(splitport(':88'), ('', '88'))
1027n/a
1028n/a def test_splitnport(self):
1029n/a splitnport = urllib.parse.splitnport
1030n/a self.assertEqual(splitnport('parrot:88'), ('parrot', 88))
1031n/a self.assertEqual(splitnport('parrot'), ('parrot', -1))
1032n/a self.assertEqual(splitnport('parrot', 55), ('parrot', 55))
1033n/a self.assertEqual(splitnport('parrot:'), ('parrot', -1))
1034n/a self.assertEqual(splitnport('parrot:', 55), ('parrot', 55))
1035n/a self.assertEqual(splitnport('127.0.0.1'), ('127.0.0.1', -1))
1036n/a self.assertEqual(splitnport('127.0.0.1', 55), ('127.0.0.1', 55))
1037n/a self.assertEqual(splitnport('parrot:cheese'), ('parrot', None))
1038n/a self.assertEqual(splitnport('parrot:cheese', 55), ('parrot', None))
1039n/a
1040n/a def test_splitquery(self):
1041n/a # Normal cases are exercised by other tests; ensure that we also
1042n/a # catch cases with no port specified (testcase ensuring coverage)
1043n/a splitquery = urllib.parse.splitquery
1044n/a self.assertEqual(splitquery('http://python.org/fake?foo=bar'),
1045n/a ('http://python.org/fake', 'foo=bar'))
1046n/a self.assertEqual(splitquery('http://python.org/fake?foo=bar?'),
1047n/a ('http://python.org/fake?foo=bar', ''))
1048n/a self.assertEqual(splitquery('http://python.org/fake'),
1049n/a ('http://python.org/fake', None))
1050n/a self.assertEqual(splitquery('?foo=bar'), ('', 'foo=bar'))
1051n/a
1052n/a def test_splittag(self):
1053n/a splittag = urllib.parse.splittag
1054n/a self.assertEqual(splittag('http://example.com?foo=bar#baz'),
1055n/a ('http://example.com?foo=bar', 'baz'))
1056n/a self.assertEqual(splittag('http://example.com?foo=bar#'),
1057n/a ('http://example.com?foo=bar', ''))
1058n/a self.assertEqual(splittag('#baz'), ('', 'baz'))
1059n/a self.assertEqual(splittag('http://example.com?foo=bar'),
1060n/a ('http://example.com?foo=bar', None))
1061n/a self.assertEqual(splittag('http://example.com?foo=bar#baz#boo'),
1062n/a ('http://example.com?foo=bar#baz', 'boo'))
1063n/a
1064n/a def test_splitattr(self):
1065n/a splitattr = urllib.parse.splitattr
1066n/a self.assertEqual(splitattr('/path;attr1=value1;attr2=value2'),
1067n/a ('/path', ['attr1=value1', 'attr2=value2']))
1068n/a self.assertEqual(splitattr('/path;'), ('/path', ['']))
1069n/a self.assertEqual(splitattr(';attr1=value1;attr2=value2'),
1070n/a ('', ['attr1=value1', 'attr2=value2']))
1071n/a self.assertEqual(splitattr('/path'), ('/path', []))
1072n/a
1073n/a def test_splitvalue(self):
1074n/a # Normal cases are exercised by other tests; test pathological cases
1075n/a # with no key/value pairs. (testcase ensuring coverage)
1076n/a splitvalue = urllib.parse.splitvalue
1077n/a self.assertEqual(splitvalue('foo=bar'), ('foo', 'bar'))
1078n/a self.assertEqual(splitvalue('foo='), ('foo', ''))
1079n/a self.assertEqual(splitvalue('=bar'), ('', 'bar'))
1080n/a self.assertEqual(splitvalue('foobar'), ('foobar', None))
1081n/a self.assertEqual(splitvalue('foo=bar=baz'), ('foo', 'bar=baz'))
1082n/a
1083n/a def test_to_bytes(self):
1084n/a result = urllib.parse.to_bytes('http://www.python.org')
1085n/a self.assertEqual(result, 'http://www.python.org')
1086n/a self.assertRaises(UnicodeError, urllib.parse.to_bytes,
1087n/a 'http://www.python.org/medi\u00e6val')
1088n/a
1089n/a def test_unwrap(self):
1090n/a url = urllib.parse.unwrap('<URL:type://host/path>')
1091n/a self.assertEqual(url, 'type://host/path')
1092n/a
1093n/a
1094n/aif __name__ == "__main__":
1095n/a unittest.main()