Used test client headers in i18n tests.

This commit is contained in:
Adam Johnson 2022-10-07 08:46:40 +01:00 committed by Mariusz Felisiak
parent 15682cb003
commit 6402b064a9
1 changed files with 76 additions and 112 deletions

View File

@ -1754,55 +1754,32 @@ class MiscTests(SimpleTestCase):
) )
def test_parse_literal_http_header(self): def test_parse_literal_http_header(self):
""" tests = [
Now test that we parse a literal HTTP header correctly. ("pt-br", "pt-br"),
""" ("pt", "pt"),
g = get_language_from_request ("es,de", "es"),
r = self.rf.get("/") ("es-a,de", "es"),
r.COOKIES = {} # There isn't a Django translation to a US variation of the Spanish
r.META = {"HTTP_ACCEPT_LANGUAGE": "pt-br"} # language, a safe assumption. When the user sets it as the
self.assertEqual("pt-br", g(r)) # preferred language, the main 'es' translation should be selected
# instead.
r.META = {"HTTP_ACCEPT_LANGUAGE": "pt"} ("es-us", "es"),
self.assertEqual("pt", g(r)) # There isn't a main language (zh) translation of Django but there
# is a translation to variation (zh-hans) the user sets zh-hans as
r.META = {"HTTP_ACCEPT_LANGUAGE": "es,de"} # the preferred language, it should be selected without falling
self.assertEqual("es", g(r)) # back nor ignoring it.
("zh-hans,de", "zh-hans"),
r.META = {"HTTP_ACCEPT_LANGUAGE": "es-ar,de"} ("NL", "nl"),
self.assertEqual("es-ar", g(r)) ("fy", "fy"),
("ia", "ia"),
# This test assumes there won't be a Django translation to a US ("sr-latn", "sr-latn"),
# variation of the Spanish language, a safe assumption. When the ("zh-hans", "zh-hans"),
# user sets it as the preferred language, the main 'es' ("zh-hant", "zh-hant"),
# translation should be selected instead. ]
r.META = {"HTTP_ACCEPT_LANGUAGE": "es-us"} for header, expected in tests:
self.assertEqual(g(r), "es") with self.subTest(header=header):
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE=header)
# This tests the following scenario: there isn't a main language (zh) self.assertEqual(get_language_from_request(request), expected)
# translation of Django but there is a translation to variation (zh-hans)
# the user sets zh-hans as the preferred language, it should be selected
# by Django without falling back nor ignoring it.
r.META = {"HTTP_ACCEPT_LANGUAGE": "zh-hans,de"}
self.assertEqual(g(r), "zh-hans")
r.META = {"HTTP_ACCEPT_LANGUAGE": "NL"}
self.assertEqual("nl", g(r))
r.META = {"HTTP_ACCEPT_LANGUAGE": "fy"}
self.assertEqual("fy", g(r))
r.META = {"HTTP_ACCEPT_LANGUAGE": "ia"}
self.assertEqual("ia", g(r))
r.META = {"HTTP_ACCEPT_LANGUAGE": "sr-latn"}
self.assertEqual("sr-latn", g(r))
r.META = {"HTTP_ACCEPT_LANGUAGE": "zh-hans"}
self.assertEqual("zh-hans", g(r))
r.META = {"HTTP_ACCEPT_LANGUAGE": "zh-hant"}
self.assertEqual("zh-hant", g(r))
@override_settings( @override_settings(
LANGUAGES=[ LANGUAGES=[
@ -1822,23 +1799,19 @@ class MiscTests(SimpleTestCase):
refs #18419 -- this is explicitly for browser compatibility refs #18419 -- this is explicitly for browser compatibility
""" """
g = get_language_from_request g = get_language_from_request
r = self.rf.get("/") request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="zh-cn,en")
r.COOKIES = {} self.assertEqual(g(request), "zh-hans")
r.META = {"HTTP_ACCEPT_LANGUAGE": "zh-cn,en"}
self.assertEqual(g(r), "zh-hans")
r.META = {"HTTP_ACCEPT_LANGUAGE": "zh-tw,en"} request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="zh-tw,en")
self.assertEqual(g(r), "zh-hant") self.assertEqual(g(request), "zh-hant")
def test_special_fallback_language(self): def test_special_fallback_language(self):
""" """
Some languages may have special fallbacks that don't follow the simple Some languages may have special fallbacks that don't follow the simple
'fr-ca' -> 'fr' logic (notably Chinese codes). 'fr-ca' -> 'fr' logic (notably Chinese codes).
""" """
r = self.rf.get("/") request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="zh-my,en")
r.COOKIES = {} self.assertEqual(get_language_from_request(request), "zh-hans")
r.META = {"HTTP_ACCEPT_LANGUAGE": "zh-my,en"}
self.assertEqual(get_language_from_request(r), "zh-hans")
def test_subsequent_code_fallback_language(self): def test_subsequent_code_fallback_language(self):
""" """
@ -1853,46 +1826,37 @@ class MiscTests(SimpleTestCase):
("zh-hant-tw", "zh-hant"), ("zh-hant-tw", "zh-hant"),
("zh-hant-SG", "zh-hant"), ("zh-hant-SG", "zh-hant"),
] ]
r = self.rf.get("/")
r.COOKIES = {}
for value, expected in tests: for value, expected in tests:
with self.subTest(value=value): with self.subTest(value=value):
r.META = {"HTTP_ACCEPT_LANGUAGE": f"{value},en"} request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE=f"{value},en")
self.assertEqual(get_language_from_request(r), expected) self.assertEqual(get_language_from_request(request), expected)
def test_parse_language_cookie(self): def test_parse_language_cookie(self):
"""
Now test that we parse language preferences stored in a cookie correctly.
"""
g = get_language_from_request g = get_language_from_request
r = self.rf.get("/") request = self.rf.get("/")
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: "pt-br"} request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "pt-br"
r.META = {} self.assertEqual("pt-br", g(request))
self.assertEqual("pt-br", g(r))
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: "pt"} request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "pt"
r.META = {} self.assertEqual("pt", g(request))
self.assertEqual("pt", g(r))
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: "es"} request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="de")
r.META = {"HTTP_ACCEPT_LANGUAGE": "de"} request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "es"
self.assertEqual("es", g(r)) self.assertEqual("es", g(request))
# This test assumes there won't be a Django translation to a US # There isn't a Django translation to a US variation of the Spanish
# variation of the Spanish language, a safe assumption. When the # language, a safe assumption. When the user sets it as the preferred
# user sets it as the preferred language, the main 'es' # language, the main 'es' translation should be selected instead.
# translation should be selected instead. request = self.rf.get("/")
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: "es-us"} request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "es-us"
r.META = {} self.assertEqual(g(request), "es")
self.assertEqual(g(r), "es") # There isn't a main language (zh) translation of Django but there is a
# translation to variation (zh-hans) the user sets zh-hans as the
# This tests the following scenario: there isn't a main language (zh) # preferred language, it should be selected without falling back nor
# translation of Django but there is a translation to variation (zh-hans) # ignoring it.
# the user sets zh-hans as the preferred language, it should be selected request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="de")
# by Django without falling back nor ignoring it. request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "zh-hans"
r.COOKIES = {settings.LANGUAGE_COOKIE_NAME: "zh-hans"} self.assertEqual(g(request), "zh-hans")
r.META = {"HTTP_ACCEPT_LANGUAGE": "de"}
self.assertEqual(g(r), "zh-hans")
@override_settings( @override_settings(
USE_I18N=True, USE_I18N=True,
@ -1993,12 +1957,10 @@ class MiscTests(SimpleTestCase):
previously valid should not be used (#14170). previously valid should not be used (#14170).
""" """
g = get_language_from_request g = get_language_from_request
r = self.rf.get("/") request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="pt-br")
r.COOKIES = {} self.assertEqual("pt-br", g(request))
r.META = {"HTTP_ACCEPT_LANGUAGE": "pt-br"}
self.assertEqual("pt-br", g(r))
with self.settings(LANGUAGES=[("en", "English")]): with self.settings(LANGUAGES=[("en", "English")]):
self.assertNotEqual("pt-br", g(r)) self.assertNotEqual("pt-br", g(request))
def test_i18n_patterns_returns_list(self): def test_i18n_patterns_returns_list(self):
with override_settings(USE_I18N=False): with override_settings(USE_I18N=False):
@ -2238,15 +2200,16 @@ class CountrySpecificLanguageTests(SimpleTestCase):
def test_get_language_from_request(self): def test_get_language_from_request(self):
# issue 19919 # issue 19919
r = self.rf.get("/") request = self.rf.get(
r.COOKIES = {} "/", HTTP_ACCEPT_LANGUAGE="en-US,en;q=0.8,bg;q=0.6,ru;q=0.4"
r.META = {"HTTP_ACCEPT_LANGUAGE": "en-US,en;q=0.8,bg;q=0.6,ru;q=0.4"} )
lang = get_language_from_request(r) lang = get_language_from_request(request)
self.assertEqual("en-us", lang) self.assertEqual("en-us", lang)
r = self.rf.get("/")
r.COOKIES = {} request = self.rf.get(
r.META = {"HTTP_ACCEPT_LANGUAGE": "bg-bg,en-US;q=0.8,en;q=0.6,ru;q=0.4"} "/", HTTP_ACCEPT_LANGUAGE="bg-bg,en-US;q=0.8,en;q=0.6,ru;q=0.4"
lang = get_language_from_request(r) )
lang = get_language_from_request(request)
self.assertEqual("bg", lang) self.assertEqual("bg", lang)
def test_get_language_from_request_null(self): def test_get_language_from_request_null(self):
@ -2258,15 +2221,16 @@ class CountrySpecificLanguageTests(SimpleTestCase):
def test_specific_language_codes(self): def test_specific_language_codes(self):
# issue 11915 # issue 11915
r = self.rf.get("/") request = self.rf.get(
r.COOKIES = {} "/", HTTP_ACCEPT_LANGUAGE="pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"
r.META = {"HTTP_ACCEPT_LANGUAGE": "pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"} )
lang = get_language_from_request(r) lang = get_language_from_request(request)
self.assertEqual("pt-br", lang) self.assertEqual("pt-br", lang)
r = self.rf.get("/")
r.COOKIES = {} request = self.rf.get(
r.META = {"HTTP_ACCEPT_LANGUAGE": "pt-pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"} "/", HTTP_ACCEPT_LANGUAGE="pt-pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"
lang = get_language_from_request(r) )
lang = get_language_from_request(request)
self.assertEqual("pt-br", lang) self.assertEqual("pt-br", lang)