Ticket #20099: 20099-test2.diff

File 20099-test2.diff, 1.2 KB (added by claudep, 2 years ago)

is_ignorable_404 test

  • tests/middleware/tests.py

    diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py
    index f2f7f4d..49c94cf 100644
    a b class BrokenLinkEmailsMiddlewareTest(TestCase): 
    320320        BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
    321321        self.assertEqual(len(mail.outbox), 0)
    322322
     323    def test_custom_is_ignorable_404(self):
     324        class SubclassedMiddleware(BrokenLinkEmailsMiddleware):
     325            """Ignoring based on UA only"""
     326            ua_patterns = (re.compile(r'Spider.*'), re.compile(r'Robot.*'))
     327            def is_ignorable_404(self, uri, user_agent):
     328                return any(pattern.search(user_agent) for pattern in self.ua_patterns)
     329
     330        self.req.META['HTTP_REFERER'] = '/another/url/'
     331        self.req.META['HTTP_USER_AGENT'] = 'Spider machine 3.4'
     332        SubclassedMiddleware().process_response(self.req, self.resp)
     333        self.assertEqual(len(mail.outbox), 0)
     334        self.req.META['HTTP_USER_AGENT'] = 'My user agent'
     335        SubclassedMiddleware().process_response(self.req, self.resp)
     336        self.assertEqual(len(mail.outbox), 1)
     337
    323338
    324339class ConditionalGetMiddlewareTest(TestCase):
    325340    urls = 'middleware.cond_get_urls'
Back to Top