mirror of
				https://github.com/django/django.git
				synced 2025-10-28 08:06:09 +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:
		
				
					committed by
					
						 Carlton Gibson
						Carlton Gibson
					
				
			
			
				
	
			
			
			
						parent
						
							84c7c4a477
						
					
				
				
					commit
					eeed488a34
				
			| @@ -53,7 +53,12 @@ class LocaleMiddleware(MiddlewareMixin): | |||||||
|                     '%s%s/' % (script_prefix, language), |                     '%s%s/' % (script_prefix, language), | ||||||
|                     1 |                     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): |         if not (i18n_patterns_used and language_from_path): | ||||||
|             patch_vary_headers(response, ('Accept-Language',)) |             patch_vary_headers(response, ('Accept-Language',)) | ||||||
|   | |||||||
| @@ -254,9 +254,13 @@ class URLVaryAcceptLanguageTests(URLTestCaseBase): | |||||||
|         self.assertEqual(response.get('Vary'), 'Accept-Language') |         self.assertEqual(response.get('Vary'), 'Accept-Language') | ||||||
|  |  | ||||||
|     def test_en_redirect(self): |     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') |         response = self.client.get('/account/register/', HTTP_ACCEPT_LANGUAGE='en') | ||||||
|         self.assertRedirects(response, '/en/account/register/') |         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']) |         response = self.client.get(response.headers['location']) | ||||||
|         self.assertEqual(response.status_code, 200) |         self.assertEqual(response.status_code, 200) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user