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

Fixed #32768 -- Added Vary header when redirecting to prefixed i18n pattern.

get_language_from_request() uses Accept-Language and/or Cookie to determine the
correct redirect. Upstream caches need the matching Vary header to cache the
result.
This commit is contained in:
Alex Hayward 2021-05-19 12:19:23 +01:00 committed by Carlton Gibson
parent 84c7c4a477
commit eeed488a34
2 changed files with 11 additions and 2 deletions

View File

@ -53,7 +53,12 @@ class LocaleMiddleware(MiddlewareMixin):
'%s%s/' % (script_prefix, language),
1
)
return self.response_redirect_class(language_url)
# Redirect to the language-specific URL as detected by
# get_language_from_request(). HTTP caches may cache this
# redirect, so add the Vary header.
redirect = self.response_redirect_class(language_url)
patch_vary_headers(redirect, ('Accept-Language', 'Cookie'))
return redirect
if not (i18n_patterns_used and language_from_path):
patch_vary_headers(response, ('Accept-Language',))

View File

@ -254,9 +254,13 @@ class URLVaryAcceptLanguageTests(URLTestCaseBase):
self.assertEqual(response.get('Vary'), 'Accept-Language')
def test_en_redirect(self):
"""
The redirect to a prefixed URL depends on 'Accept-Language' and
'Cookie', but once prefixed no header is set.
"""
response = self.client.get('/account/register/', HTTP_ACCEPT_LANGUAGE='en')
self.assertRedirects(response, '/en/account/register/')
self.assertFalse(response.get('Vary'))
self.assertEqual(response.get('Vary'), 'Accept-Language, Cookie')
response = self.client.get(response.headers['location'])
self.assertEqual(response.status_code, 200)