Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_encoding(self):
self.assertEqual(URL('http://www.xn--alliancefranaise-npb.nu/').hostname, 'www.alliancefran\xe7aise.nu')
self.assertEqual(str(URL('http://localhost/').with_hostinfo('www.alliancefran\xe7aise.nu')),
'http://www.xn--alliancefranaise-npb.nu/')
url = URL('http://%75%73%65%72:%70%61%73%73%77%64@httpbin.org/basic-auth/user/passwd')
self.assertEqual(url.username, 'user')
self.assertEqual(url.password, 'passwd')
username = 'foo@example.com'
password = 'pa$$word'
url = URL('http://example.com').with_userinfo(username, password)
self.assertEqual(url.username, username)
self.assertEqual(url.password, password)
self.assertEqual(str(url), 'http://foo%40example.com:pa%24%24word@example.com')
self.assertEqual(str(URL('http://example.com/日本語の/パス')),
'http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE/%E3%83%91%E3%82%B9')
original = 'http://example.com/\u3081\u3061\u3083\u304f\u3061\u3083\u306a/\u30d1\u30b9/%2F%23%3F'
url = URL(original)
self.assertEqual(str(url), 'http://example.com/%E3%82%81%E3%81%A1%E3%82%83%E3%81%8F%E3%81%A1%E3%82%83%E3%81%AA/'
url = URL(original)
self.assertEqual(str(url), 'http://example.com/%E3%82%81%E3%81%A1%E3%82%83%E3%81%8F%E3%81%A1%E3%82%83%E3%81%AA/'
'%E3%83%91%E3%82%B9/%2F%23%3F')
self.assertEqual(url.path, '/%E3%82%81%E3%81%A1%E3%82%83%E3%81%8F%E3%81%A1%E3%82%83%E3%81%AA/'
'%E3%83%91%E3%82%B9/%2F%23%3F')
self.assertEqual(url.name, '/#?')
self.assertTupleEqual(url.parts, ('http://example.com/', '\u3081\u3061\u3083\u304f\u3061\u3083\u306a',
'\u30d1\u30b9', '/#?'))
self.assertEqual(str(URL('http://example.com/name').with_name('\u65e5\u672c\u8a9e/\u540d\u524d')),
'http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E%2F%E5%90%8D%E5%89%8D')
self.assertEqual(str(URL('http://example.com/name') / '\u65e5\u672c\u8a9e/\u540d\u524d'),
'http://example.com/name/%E6%97%A5%E6%9C%AC%E8%AA%9E/%E5%90%8D%E5%89%8D')
self.assertEqual(str(URL('http://example.com/file').with_suffix('.///')), 'http://example.com/file.%2F%2F%2F')
def test_idempotent(self):
url = URL('http://\u65e5\u672c\u8a9e\u306e.\u30c9\u30e1\u30a4\u30f3.jp/'
'path/to/\u30d5\u30a1\u30a4\u30eb.ext?\u30af\u30a8\u30ea')
self.assertEqual(url, URL(str(url)))
self.assertEqual(url, URL('http://xn--u9ju32nb2abz6g.xn--eckwd4c7c.jp/'
'path/to/\u30d5\u30a1\u30a4\u30eb.ext?\u30af\u30a8\u30ea'))
self.assertEqual(str(url), 'http://foo%40example.com:pa%24%24word@example.com')
self.assertEqual(str(URL('http://example.com/日本語の/パス')),
'http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE/%E3%83%91%E3%82%B9')
original = 'http://example.com/\u3081\u3061\u3083\u304f\u3061\u3083\u306a/\u30d1\u30b9/%2F%23%3F'
url = URL(original)
self.assertEqual(str(url), 'http://example.com/%E3%82%81%E3%81%A1%E3%82%83%E3%81%8F%E3%81%A1%E3%82%83%E3%81%AA/'
'%E3%83%91%E3%82%B9/%2F%23%3F')
self.assertEqual(url.path, '/%E3%82%81%E3%81%A1%E3%82%83%E3%81%8F%E3%81%A1%E3%82%83%E3%81%AA/'
'%E3%83%91%E3%82%B9/%2F%23%3F')
self.assertEqual(url.name, '/#?')
self.assertTupleEqual(url.parts, ('http://example.com/', '\u3081\u3061\u3083\u304f\u3061\u3083\u306a',
'\u30d1\u30b9', '/#?'))
self.assertEqual(str(URL('http://example.com/name').with_name('\u65e5\u672c\u8a9e/\u540d\u524d')),
'http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E%2F%E5%90%8D%E5%89%8D')
self.assertEqual(str(URL('http://example.com/name') / '\u65e5\u672c\u8a9e/\u540d\u524d'),
'http://example.com/name/%E6%97%A5%E6%9C%AC%E8%AA%9E/%E5%90%8D%E5%89%8D')
self.assertEqual(str(URL('http://example.com/file').with_suffix('.///')), 'http://example.com/file.%2F%2F%2F')
def test_join(self):
url = URL('http://www.example.com/path/to/file.ext?query#fragment')
self.assertEqual(str(url / 'https://secure.example.com/path'), 'https://secure.example.com/path')
self.assertEqual(str(url / '/changed/path'), 'http://www.example.com/changed/path')
self.assertEqual(str(url.with_name('other_file')), 'http://www.example.com/path/to/other_file')
url = URL('http://%75%73%65%72:%70%61%73%73%77%64@httpbin.org/basic-auth/user/passwd')
self.assertEqual(url.username, 'user')
self.assertEqual(url.password, 'passwd')
username = 'foo@example.com'
password = 'pa$$word'
url = URL('http://example.com').with_userinfo(username, password)
self.assertEqual(url.username, username)
self.assertEqual(url.password, password)
self.assertEqual(str(url), 'http://foo%40example.com:pa%24%24word@example.com')
self.assertEqual(str(URL('http://example.com/日本語の/パス')),
'http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE/%E3%83%91%E3%82%B9')
original = 'http://example.com/\u3081\u3061\u3083\u304f\u3061\u3083\u306a/\u30d1\u30b9/%2F%23%3F'
url = URL(original)
self.assertEqual(str(url), 'http://example.com/%E3%82%81%E3%81%A1%E3%82%83%E3%81%8F%E3%81%A1%E3%82%83%E3%81%AA/'
'%E3%83%91%E3%82%B9/%2F%23%3F')
self.assertEqual(url.path, '/%E3%82%81%E3%81%A1%E3%82%83%E3%81%8F%E3%81%A1%E3%82%83%E3%81%AA/'
'%E3%83%91%E3%82%B9/%2F%23%3F')
self.assertEqual(url.name, '/#?')
self.assertTupleEqual(url.parts, ('http://example.com/', '\u3081\u3061\u3083\u304f\u3061\u3083\u306a',
'\u30d1\u30b9', '/#?'))
self.assertEqual(str(URL('http://example.com/name').with_name('\u65e5\u672c\u8a9e/\u540d\u524d')),
'http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E%2F%E5%90%8D%E5%89%8D')
self.assertEqual(str(URL('http://example.com/name') / '\u65e5\u672c\u8a9e/\u540d\u524d'),
'http://example.com/name/%E6%97%A5%E6%9C%AC%E8%AA%9E/%E5%90%8D%E5%89%8D')
self.assertEqual(str(URL('http://example.com/file').with_suffix('.///')), 'http://example.com/file.%2F%2F%2F')
def test_init_with_empty_string(self):
url = URL('')
self.assertEqual(str(url), '')
def test_with(self):
url = URL('http://www.example.com/path/to/file.exe?query?fragment')
self.assertEqual(str(url.with_scheme('https')), 'https://www.example.com/path/to/file.exe?query?fragment')
self.assertEqual(str(url.with_netloc('localhost')), 'http://localhost/path/to/file.exe?query?fragment')
self.assertEqual(str(url.with_userinfo('username', 'password')),
'http://username:password@www.example.com/path/to/file.exe?query?fragment')
self.assertEqual(str(url.with_userinfo(None, None)), 'http://www.example.com/path/to/file.exe?query?fragment')
self.assertEqual(str(url.with_hostinfo('localhost', 8080)),
'http://localhost:8080/path/to/file.exe?query?fragment')
self.assertEqual(str(URL('http://example.com/base/') / 'path/to/file'), 'http://example.com/base/path/to/file')
self.assertEqual(str(URL('http://example.com/path/?q') / URL('http://localhost/app/?q') / URL('to/content')),
'http://localhost/app/to/content')
def __new__(cls, *args, root=None):
if root is not None:
root = URL(root)
elif cls._chroot is not None:
root = cls._chroot
elif webob and len(args) >= 1 and isinstance(args[0], webob.Request):
root = URL(args[0].application_url)
else:
root = URL(*args)
assert root.scheme and root.netloc and not root.query and not root.fragment, 'malformed root: %s' % (root,)
if not root.path:
root = root / '/'
return type(cls.__name__, (cls,), {'_chroot': root})._from_parts(args)
def __new__(cls, *args, root=None):
if root is not None:
root = URL(root)
elif cls._chroot is not None:
root = cls._chroot
elif webob and len(args) >= 1 and isinstance(args[0], webob.Request):
root = URL(args[0].application_url)
else:
root = URL(*args)
assert root.scheme and root.netloc and not root.query and not root.fragment, 'malformed root: %s' % (root,)
if not root.path:
root = root / '/'
return type(cls.__name__, (cls,), {'_chroot': root})._from_parts(args)