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_IS_URL(self):
rtn = IS_URL()("abc.com")
self.assertEqual(rtn, ("http://abc.com", None))
rtn = IS_URL(mode="generic")("abc.com")
self.assertEqual(rtn, ("abc.com", None))
rtn = IS_URL(allowed_schemes=["https"], prepend_scheme="https")(
"https://abc.com"
)
self.assertEqual(rtn, ("https://abc.com", None))
rtn = IS_URL(prepend_scheme="https")("abc.com")
self.assertEqual(rtn, ("https://abc.com", None))
rtn = IS_URL(
mode="generic", allowed_schemes=["ftps", "https"], prepend_scheme="https"
)("https://abc.com")
self.assertEqual(rtn, ("https://abc.com", None))
rtn = IS_URL(
mode="generic",
allowed_schemes=["ftps", "https", None],
prepend_scheme="https",
)("abc.com")
self.assertEqual(rtn, ("abc.com", None))
# regression test for issue 773
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("blargg://unreal"), ("blargg://unreal", "Enter a valid URL"))
# 'generic' mode with custom allowed_schemes that still includes
# 'http' (the default for prepend_scheme)
x = IS_URL(mode="generic", allowed_schemes=["http", "blargg"])
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("ftp://google.ca"), ("ftp://google.ca", "Enter a valid URL"))
self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL"))
self.assertEqual(x("google.ca:80"), ("google.ca:80", "Enter a valid URL"))
self.assertEqual(x("blargg://unreal"), ("blargg://unreal", None))
# 'generic' mode with overriden prepend_scheme
x = IS_URL(mode="generic", prepend_scheme="ftp")
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("ftp://google.ca"), ("ftp://google.ca", None))
self.assertEqual(x("google.ca"), ("google.ca", None))
self.assertEqual(x("google.ca:80"), ("ftp://google.ca:80", None))
self.assertEqual(x("blargg://unreal"), ("blargg://unreal", "Enter a valid URL"))
# 'generic' mode with overriden allowed_schemes and prepend_scheme
x = IS_URL(
mode="generic", allowed_schemes=[None, "ftp", "ftps"], prepend_scheme="ftp"
)
self.assertEqual(
x("http://google.ca"), ("http://google.ca", "Enter a valid URL")
)
self.assertEqual(x("google.ca"), ("google.ca", None))
self.assertEqual(x("ftp://google.ca"), ("ftp://google.ca", None))
self.fail("Wrong exception: " + str(e))
else:
self.fail("Got invalid prepend_scheme: 'mailto'")
# Does not prepend if None type is not specified in allowed_scheme, because a scheme is required
a = IS_HTTP_URL(allowed_schemes=["http"])
self.assertEqual(a("google.ca"), ("google.ca", "Enter a valid URL"))
self.assertEqual(a("google.ca:80"), ("google.ca:80", "Enter a valid URL"))
class TestUnicode(unittest.TestCase):
x = IS_URL()
y = IS_URL(allowed_schemes=["https"], prepend_scheme="https")
# excludes the option for abbreviated URLs with no scheme
z = IS_URL(prepend_scheme=None)
# disables prepending the scheme in the return value
def testUnicodeToAsciiUrl(self):
self.assertEqual(
unicode_to_ascii_authority(u"www.Alliancefran\xe7aise.nu"),
"www.xn--alliancefranaise-npb.nu",
)
self.assertEqual(unicode_to_ascii_authority(u"www.benn.ca"), "www.benn.ca")
self.assertRaises(
UnicodeError, unicode_to_ascii_authority, u"\u4e2d" * 1000
) # label is too long
def testValidUrls(self):
self.assertEqual(
self.x(u"www.Alliancefrancaise.nu"),
("http://www.Alliancefrancaise.nu", None),
def test_IS_URL(self):
rtn = IS_URL()("abc.com")
self.assertEqual(rtn, ("http://abc.com", None))
rtn = IS_URL(mode="generic")("abc.com")
self.assertEqual(rtn, ("abc.com", None))
rtn = IS_URL(allowed_schemes=["https"], prepend_scheme="https")(
"https://abc.com"
)
self.assertEqual(rtn, ("https://abc.com", None))
rtn = IS_URL(prepend_scheme="https")("abc.com")
self.assertEqual(rtn, ("https://abc.com", None))
rtn = IS_URL(
mode="generic", allowed_schemes=["ftps", "https"], prepend_scheme="https"
)("https://abc.com")
self.assertEqual(rtn, ("https://abc.com", None))
rtn = IS_URL(
mode="generic",
allowed_schemes=["ftps", "https", None],
prepend_scheme="https",
)("abc.com")
self.assertEqual(rtn, ("abc.com", None))
# regression test for issue 773
rtn = IS_URL()("domain.ninja")
self.assertEqual(rtn, ("http://domain.ninja", None))
# addition of allowed_tlds
rtn = IS_URL(allowed_tlds=["com", "net", "org"])("domain.ninja")
def testModeHttp(self):
# defaults to mode='http'
x = IS_URL()
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
self.assertEqual(x("google..ca"), ("google..ca", "Enter a valid URL"))
self.assertEqual(x("google.ca.."), ("google.ca..", "Enter a valid URL"))
# explicit use of 'http' mode
x = IS_URL(mode="http")
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# prepends 'https' instead of 'http'
x = IS_URL(mode="http", prepend_scheme="https")
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("https://google.ca", None))
self.assertEqual(x("google.ca:80"), ("https://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# prepending disabled
x = IS_URL(prepend_scheme=None)
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("https://google.ca", None))
self.assertEqual(x("google.ca:80"), ("https://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# prepending disabled
x = IS_URL(prepend_scheme=None)
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("google.ca", None))
self.assertEqual(x("google.ca:80"), ("google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# custom allowed_schemes
x = IS_URL(mode="http", allowed_schemes=[None, "http"])
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(
x("https://google.ca"), ("https://google.ca", "Enter a valid URL")
)
self.assertEqual(x("google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# custom allowed_schemes, excluding None
x = IS_URL(allowed_schemes=["http"])
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(
x("https://google.ca"), ("https://google.ca", "Enter a valid URL")
)
self.assertEqual(x("google.ca"), ("google.ca", "Enter a valid URL"))
try:
x = IS_URL(mode="generic", allowed_schemes=[None, "ftp", "ftps"])
except Exception as e:
if (
str(e)
!= "prepend_scheme='http' is not in allowed_schemes=[None, 'ftp', 'ftps']"
):
self.fail("Wrong exception: " + str(e))
else:
self.fail("Accepted invalid prepend_scheme: 'http'")
# prepend_scheme's value must be in allowed_schemes, which by default
# is all schemes that really exist
try:
x = IS_URL(mode="generic", prepend_scheme="blargg")
x("http://www.google.ca")
# we can only reasonably know about the error at calling time
except Exception as e:
if not str(e).startswith(
"prepend_scheme='blargg' is not in allowed_schemes="
):
self.fail("Wrong exception: " + str(e))
else:
self.fail("Accepted invalid prepend_scheme: 'blargg'")
# prepend_scheme's value must be in allowed_schemes
try:
x = IS_URL(
mode="generic", allowed_schemes=[None, "http"], prepend_scheme="blargg"
)
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
self.assertEqual(x("google..ca"), ("google..ca", "Enter a valid URL"))
self.assertEqual(x("google.ca.."), ("google.ca..", "Enter a valid URL"))
# explicit use of 'http' mode
x = IS_URL(mode="http")
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# prepends 'https' instead of 'http'
x = IS_URL(mode="http", prepend_scheme="https")
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("https://google.ca", None))
self.assertEqual(x("google.ca:80"), ("https://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# prepending disabled
x = IS_URL(prepend_scheme=None)
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("google.ca", None))
self.assertEqual(x("google.ca:80"), ("google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# custom allowed_schemes
x = IS_URL(mode="http", allowed_schemes=[None, "http"])
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# prepends 'https' instead of 'http'
x = IS_URL(mode="http", prepend_scheme="https")
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("https://google.ca", None))
self.assertEqual(x("google.ca:80"), ("https://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# prepending disabled
x = IS_URL(prepend_scheme=None)
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca"), ("google.ca", None))
self.assertEqual(x("google.ca:80"), ("google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))
# custom allowed_schemes
x = IS_URL(mode="http", allowed_schemes=[None, "http"])
self.assertEqual(x("http://google.ca"), ("http://google.ca", None))
self.assertEqual(
x("https://google.ca"), ("https://google.ca", "Enter a valid URL")
)
self.assertEqual(x("google.ca"), ("http://google.ca", None))
self.assertEqual(x("google.ca:80"), ("http://google.ca:80", None))
self.assertEqual(x("unreal.blargg"), ("unreal.blargg", "Enter a valid URL"))