diff --git a/neutron/tests/unit/test_api_v2_resource.py b/neutron/tests/unit/test_api_v2_resource.py index 91ac57117a..4444c75760 100644 --- a/neutron/tests/unit/test_api_v2_resource.py +++ b/neutron/tests/unit/test_api_v2_resource.py @@ -100,8 +100,7 @@ class RequestTestCase(base.BaseTestCase): self.assertTrue(self.req.context.is_admin) def test_best_match_language(self): - # Here we test that we are actually invoking language negotiation - # by webop and also that the default locale always available is en-US + # Test that we are actually invoking language negotiation by webop request = wsgi.Request.blank('/') gettextutils.get_available_languages = mock.MagicMock() gettextutils.get_available_languages.return_value = ['known-language', @@ -109,9 +108,18 @@ class RequestTestCase(base.BaseTestCase): request.headers['Accept-Language'] = 'known-language' language = request.best_match_language() self.assertEqual(language, 'known-language') + + # If the Accept-Leader is an unknown language, missing or empty, + # the best match locale should be None request.headers['Accept-Language'] = 'unknown-language' language = request.best_match_language() - self.assertEqual(language, 'en_US') + self.assertEqual(language, None) + request.headers['Accept-Language'] = '' + language = request.best_match_language() + self.assertEqual(language, None) + request.headers.pop('Accept-Language') + language = request.best_match_language() + self.assertEqual(language, None) class ResourceTestCase(base.BaseTestCase): diff --git a/neutron/wsgi.py b/neutron/wsgi.py index 35c9746560..6bac0f447e 100644 --- a/neutron/wsgi.py +++ b/neutron/wsgi.py @@ -301,10 +301,15 @@ class Request(webob.Request): return None def best_match_language(self): - """Determine language for returned response.""" + """Determines best available locale from the Accept-Language header. + + :returns: the best language match or None if the 'Accept-Language' + header was not available in the request. + """ + if not self.accept_language: + return None all_languages = gettextutils.get_available_languages('neutron') - return self.accept_language.best_match(all_languages, - default_match='en_US') + return self.accept_language.best_match(all_languages) @property def context(self):