Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
 def test_redirects_to_correct_language_despite_unicode_errors(self):
     """Should redirect to lang prefixed url, stripping invalid chars."""
     path = '/the/dude/'
     corrupt_querystring = '?a\xa4\x91b\xa4\x91i\xc0de=s'
     corrected_querystring = '?abide=s'
     req = self.rf.get(path + corrupt_querystring,
                       HTTP_ACCEPT_LANGUAGE='de')
     resp = LocaleURLMiddleware().process_request(req)
     self.assertEqual(resp['Location'],
                      '/de' + path + corrected_querystring)
Exemplo n.º 4
0
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")
Exemplo n.º 5
0
 def setUp(self):
     self.rf = RequestFactory()
     self.middleware = LocaleURLMiddleware()
Exemplo n.º 6
0
 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 = LocaleURLMiddleware().process_request(req)
     self.assertEqual(resp['Location'], '/en-US' + path)
Exemplo n.º 7
0
 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 = LocaleURLMiddleware().process_request(req)
     self.assertEqual(resp['Location'], '/de' + path)
Exemplo n.º 8
0
 def test_no_redirect_lang_param(self):
     """Middleware should not redirect when exempt."""
     path = '/fr/the/dude/'
     req = self.rf.get(path, {'lang': 'de'})
     resp = LocaleURLMiddleware().process_request(req)
     self.assertIs(resp, None)  # no redirect
Exemplo n.º 9
0
 def test_redirects_lang_param(self):
     """Middleware should remove the lang param on redirect."""
     path = '/fr/the/dude/'
     req = self.rf.get(path, {'lang': 'de'})
     resp = LocaleURLMiddleware().process_request(req)
     self.assertEqual(resp['Location'], '/de/the/dude/')