1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Add tests for punycode encoding

This commit is contained in:
Julien BERNARD 2022-11-14 21:10:30 -05:00
parent 8f9165bfed
commit 988e3f9460

View File

@ -14,6 +14,7 @@ from django.utils.encoding import (
force_str,
get_system_encoding,
iri_to_uri,
punycode,
repercent_broken_unicode,
smart_bytes,
smart_str,
@ -22,6 +23,12 @@ from django.utils.encoding import (
from django.utils.functional import SimpleLazyObject
from django.utils.translation import gettext_lazy
try:
# try importing idna package for IDNA 2008 compliance
import idna
except ImportError:
idna = None
class TestEncodingUtils(SimpleTestCase):
def test_force_str_exception(self):
@ -218,3 +225,32 @@ class TestRFC3987IEncodingUtils(unittest.TestCase):
for uri, expected in cases:
with self.subTest(uri):
self.assertEqual(escape_uri_path(uri), expected)
class TestPunycodeEncoding(unittest.TestCase):
def test_valid_punycode(self):
self.assertIsNone(punycode(None))
self.assertEqual(punycode(""), "")
cases = [
("xn----f38am99bqvcd5liy1cxsg.test", "xn----f38am99bqvcd5liy1cxsg.test"),
("test.xn--rhqv96g", "test.xn--rhqv96g"),
("test.شبك", "test.xn--ngbx0c"),
("普遍接受-测试.top", "xn----f38am99bqvcd5liy1cxsg.top"),
("मेल.डाटामेल.भारत", "xn--r2bi6d.xn--c2bd4bq1db8d.xn--h2brj9c"),
("fußball.de", "xn--fuball-cta.de" if idna else "fussball.de"),
]
for ulabel, alabel in cases:
with self.subTest(ulabel):
self.assertEqual(punycode(ulabel), alabel)
def test_invalid_punycode(self):
cases = [".test.top"]
if idna:
cases += ["in--valid", "\\u0557w.test"]
for label in cases:
with self.subTest(label):
with self.assertRaises(UnicodeError):
punycode(label)