class TestLocaleURLMiddleware(TestCase): def setUp(self): self.rf = RequestFactory() self.middleware = LocaleURLMiddleware() @override_settings(DEV_LANGUAGES=("de", "fr")) def test_redirects_to_correct_language(self): """Should redirect to lang prefixed url.""" path = "/the/dude/" req = self.rf.get(path, HTTP_ACCEPT_LANGUAGE="de") resp = self.middleware.process_request(req) self.assertEqual(resp["Location"], "/de" + path) @override_settings(DEV_LANGUAGES=("es", "fr"), LANGUAGE_CODE="en-US") def test_redirects_to_default_language(self): """Should redirect to default lang if not in settings.""" path = "/the/dude/" req = self.rf.get(path, HTTP_ACCEPT_LANGUAGE="de") resp = self.middleware.process_request(req) self.assertEqual(resp["Location"], "/en-US" + path) @override_settings(DEV_LANGUAGES=("de", "fr")) def test_redirects_to_correct_language_despite_unicode_errors(self): """Should redirect to lang prefixed url, stripping invalid chars.""" path = "/the/dude/" corrupt_querystring = "?" + urlencode( {b"a\xa4\x91b\xa4\x91i\xc0de": "s"}) corrected_querystring = "?abide=s" req = self.rf.get(path + corrupt_querystring, HTTP_ACCEPT_LANGUAGE="de") resp = self.middleware.process_request(req) self.assertEqual(resp["Location"], "/de" + path + corrected_querystring)
class TestLocaleURLMiddleware(TestCase): def setUp(self): self.rf = RequestFactory() self.middleware = LocaleURLMiddleware() @override_settings(DEV_LANGUAGES=('de', 'fr')) def test_redirects_to_correct_language(self): """Should redirect to lang prefixed url.""" path = '/the/dude/' req = self.rf.get(path, HTTP_ACCEPT_LANGUAGE='de') resp = self.middleware.process_request(req) self.assertEqual(resp['Location'], '/de' + path) @override_settings(DEV_LANGUAGES=('es', 'fr'), LANGUAGE_CODE='en-US') def test_redirects_to_default_language(self): """Should redirect to default lang if not in settings.""" path = '/the/dude/' req = self.rf.get(path, HTTP_ACCEPT_LANGUAGE='de') resp = self.middleware.process_request(req) self.assertEqual(resp['Location'], '/en-US' + path) @override_settings(DEV_LANGUAGES=('de', 'fr')) def test_redirects_to_correct_language_despite_unicode_errors(self): """Should redirect to lang prefixed url, stripping invalid chars.""" path = '/the/dude/' corrupt_querystring = '?' + urlencode( {b'a\xa4\x91b\xa4\x91i\xc0de': 's'}) corrected_querystring = '?abide=s' req = self.rf.get(path + corrupt_querystring, HTTP_ACCEPT_LANGUAGE='de') resp = self.middleware.process_request(req) self.assertEqual(resp['Location'], '/de' + path + corrected_querystring)
class TestLocaleURLMiddleware(TestCase): def setUp(self): self.rf = RequestFactory() self.middleware = LocaleURLMiddleware() @override_settings(DEV_LANGUAGES=("de", "fr")) def test_matching_locale(self): locale = "fr" path = "/the/dude/" full_path = f"/{locale}{path}" req = self.rf.get(full_path) self.middleware.process_request(req) self.assertEqual(req.path_info, path) self.assertEqual(req.locale, "fr") @override_settings(DEV_LANGUAGES=("de", "fr")) def test_non_matching_locale(self): locale = "zh" path = "/the/dude/" full_path = f"/{locale}{path}" req = self.rf.get(full_path) self.middleware.process_request(req) self.assertEqual(req.path_info, full_path) self.assertEqual(req.locale, "") @override_settings(DEV_LANGUAGES=("zh-CN", "zh-TW")) def test_matching_main_language_to_sub_language(self): locale = "zh" path = "/the/dude/" full_path = f"/{locale}{path}" req = self.rf.get(full_path) self.middleware.process_request(req) self.assertEqual(req.path_info, path) self.assertEqual(req.locale, "zh-CN") @override_settings(DEV_LANGUAGES=("es-ES", "fr")) def test_matching_canonical(self): locale = "es" path = "/the/dude/" full_path = f"/{locale}{path}" req = self.rf.get(full_path) self.middleware.process_request(req) self.assertEqual(req.path_info, path) self.assertEqual(req.locale, "es-ES")