Exemple #1
0
 def test_urls_dont_call_reverse(self, reverse_mock):
     """
     Should not call reverse() for an obvious URL.
     """
     resolver = get_resolver([
         redirect(r'^iam/the/walrus/$', '/coo/coo/cachoo/'),
         redirect(r'^iam/the/ape-man/$', 'https://example.com/egg-man/'),
     ])
     middleware = RedirectsMiddleware(resolver)
     resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))
     self.assertEqual(resp.status_code, 301)
     self.assertEqual(resp['Location'], '/coo/coo/cachoo/')
     resp = middleware.process_request(self.rf.get('/iam/the/ape-man/'))
     self.assertEqual(resp.status_code, 301)
     self.assertEqual(resp['Location'], 'https://example.com/egg-man/')
     reverse_mock.assert_not_called()
Exemple #2
0
 def test_will_not_return_protocol_relative_redirect(self):
     """Allowing a protocol relative URL can allow an unintended domain redirect."""
     resolver = get_resolver([
         redirect(r'^(.+)/$', '/{}/', locale_prefix=False),
     ])
     middleware = RedirectsMiddleware(resolver)
     resp = middleware.process_request(self.rf.get('/%2fexample.com/'))
     self.assertEqual(resp['Location'], '/example.com/')
Exemple #3
0
    def test_no_redirect(self):
        """Should be able to skip redirects."""
        resolver = get_resolver([
            no_redirect(r'^iam/the/walrus/$'),
            redirect(r'^iam/the/.*/$', '/coo/coo/cachoo/'),
        ])
        middleware = RedirectsMiddleware(resolver)
        resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))
        self.assertIsNone(resp)

        # including locale
        middleware = RedirectsMiddleware(resolver)
        resp = middleware.process_request(self.rf.get('/pt-BR/iam/the/walrus/'))
        self.assertIsNone(resp)

        resp = middleware.process_request(self.rf.get('/iam/the/marmot/'))
        self.assertEqual(resp.status_code, 301)
        self.assertEqual(resp['Location'], '/coo/coo/cachoo/')
Exemple #4
0
 def test_value_capture_and_substitution(self):
     """
     Should be able to capture info from URL and use in redirection.
     """
     resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$', '/donnie/the/{name}/')])
     middleware = RedirectsMiddleware(resolver)
     resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))
     self.assertEqual(resp.status_code, 301)
     self.assertEqual(resp['Location'], '/donnie/the/walrus/')
Exemple #5
0
 def test_empty_unnamed_captures(self):
     """
     Should be able to define an optional unnamed capture.
     """
     resolver = get_resolver([redirect(r'^iam/the(/.+)?/$', '/donnie/the{}/',
                                       locale_prefix=False)])
     middleware = RedirectsMiddleware(resolver)
     resp = middleware.process_request(self.rf.get('/iam/the/'))
     self.assertEqual(resp.status_code, 301)
     self.assertEqual(resp['Location'], '/donnie/the/')
Exemple #6
0
 def test_locale_value_capture_ignore_locale(self):
     """
     Should be able to ignore the original locale.
     """
     resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',
                                       '/donnie/the/{name}/', prepend_locale=False)])
     middleware = RedirectsMiddleware(resolver)
     resp = middleware.process_request(self.rf.get('/zh-TW/iam/the/walrus/'))
     self.assertEqual(resp.status_code, 301)
     self.assertEqual(resp['Location'], '/donnie/the/walrus/')
Exemple #7
0
 def test_locale_value_capture_no_locale(self):
     """
     Should get locale value in kwargs and not break if no locale in URL.
     """
     resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',
                                       '/donnie/the/{name}/')])
     middleware = RedirectsMiddleware(resolver)
     resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))
     self.assertEqual(resp.status_code, 301)
     self.assertEqual(resp['Location'], '/donnie/the/walrus/')
Exemple #8
0
 def test_locale_value_capture(self):
     """
     Should prepend locale value automatically.
     """
     resolver = get_resolver([redirect(r'^iam/the/(?P<name>.+)/$',
                                       '/donnie/the/{name}/')])
     middleware = RedirectsMiddleware(resolver)
     resp = middleware.process_request(self.rf.get('/pt-BR/iam/the/walrus/'))
     self.assertEqual(resp.status_code, 301)
     self.assertEqual(resp['Location'], '/pt-BR/donnie/the/walrus/')
Exemple #9
0
    def test_match_flags(self):
        """
        Should be able to set regex flags for redirect URL.
        """
        resolver = get_resolver([
            redirect(r'^iam/the/walrus/$', '/coo/coo/cachoo/'),
            no_redirect(r'^iam/the/walrus/$', re_flags='i'),
        ])
        middleware = RedirectsMiddleware(resolver)
        resp = middleware.process_request(self.rf.get('/IAm/The/Walrus/'))
        self.assertIsNone(resp)

        # also with locale
        resp = middleware.process_request(self.rf.get('/es-ES/Iam/The/Walrus/'))
        self.assertIsNone(resp)

        # sanity check
        resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))
        self.assertEqual(resp.status_code, 301)
        self.assertEqual(resp['Location'], '/coo/coo/cachoo/')
Exemple #10
0
    def test_no_locale_prefix(self):
        """
        Should be able to define a redirect that ignores locale prefix.

        Also when not using any named captures (like implied locale) unnamed
        captures should work. For some reason Django only allows unnamed captures
        to pass through if there are no named ones.
        """
        resolver = get_resolver([redirect(r'^iam/the/(.+)/$', '/donnie/the/{}/',
                                          locale_prefix=False)])
        middleware = RedirectsMiddleware(resolver)
        resp = middleware.process_request(self.rf.get('/iam/the/walrus/'))
        self.assertEqual(resp.status_code, 301)
        self.assertEqual(resp['Location'], '/donnie/the/walrus/')
Exemple #11
0
    def test_non_ascii_strip_tags(self):
        """
        Should deal with non-ascii characters when there's a substitution as well as strip tags.

        This is from errors that happened on www.m.o. The following URL caused a 500:

        https://www.mozilla.org/editor/midasdemo/securityprefs.html%3C/span%3E%3C/a%3E%C2%A0
        """
        resolver = get_resolver([redirect(r'^editor/(?P<page>.*)$',
                                          'http://www-archive.mozilla.org/editor/{page}')])
        middleware = RedirectsMiddleware(resolver)
        resp = middleware.process_request(self.rf.get('/editor/midasdemo/securityprefs.html'
                                                      '%3C/span%3E%3C/a%3E%C2%A0'))
        self.assertEqual(resp.status_code, 301)
        self.assertEqual(resp['Location'], 'http://www-archive.mozilla.org/editor/midasdemo/'
                                           'securityprefs.html%C2%A0')