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 test_webob_jail(self):
request = webob.Request.blank('/path/to/filename.ext', {'SCRIPT_NAME': '/app/root'})
self.assertEqual(request.application_url, 'http://localhost/app/root')
self.assertEqual(request.url, 'http://localhost/app/root/path/to/filename.ext')
url = JailedURL(request)
self.assertEqual(str(url.chroot), 'http://localhost/app/root')
self.assertEqual(str(url), 'http://localhost/app/root/path/to/filename.ext')
scheme, netloc, path, query, fragment = urllib.parse.urlsplit(part)
# trick to escape '/' in query and fragment and trailing
if not re.match(re.escape(sep) + '+$', path):
path = re.sub('%s+$' % (re.escape(sep),), lambda m: '\\x00' * len(m.group(0)), path)
path = urllib.parse.urlunsplit(('', '', path, query.replace('/', '\\x00'), fragment.replace('/', '\\x00')))
drive = urllib.parse.urlunsplit((scheme, netloc, '', '', ''))
root, path = re.match('^(%s*)(.*)$' % (re.escape(sep),), path).groups()
return drive, root, path
class URL(urllib.parse._NetlocResultMixinStr, PurePath):
_flavour = _URLFlavour()
_parse_qsl_args = {}
_urlencode_args = {'doseq': True}
@classmethod
def _parse_args(cls, args):
return super()._parse_args((cls._canonicalize_arg(a) for a in args))
@classmethod
def _canonicalize_arg(cls, a):
if isinstance(a, urllib.parse.SplitResult):
return urllib.parse.urlunsplit(a)
if isinstance(a, urllib.parse.ParseResult):
return urllib.parse.urlunparse(a)
if webob and isinstance(a, webob.Request):