Code

Ticket #20099: 20099-test2.diff

File 20099-test2.diff, 1.2 KB (added by claudep, 13 months ago)

is_ignorable_404 test

Line 
1diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py
2index f2f7f4d..49c94cf 100644
3--- a/tests/middleware/tests.py
4+++ b/tests/middleware/tests.py
5@@ -320,6 +320,21 @@ class BrokenLinkEmailsMiddlewareTest(TestCase):
6         BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
7         self.assertEqual(len(mail.outbox), 0)
8 
9+    def test_custom_is_ignorable_404(self):
10+        class SubclassedMiddleware(BrokenLinkEmailsMiddleware):
11+            """Ignoring based on UA only"""
12+            ua_patterns = (re.compile(r'Spider.*'), re.compile(r'Robot.*'))
13+            def is_ignorable_404(self, uri, user_agent):
14+                return any(pattern.search(user_agent) for pattern in self.ua_patterns)
15+
16+        self.req.META['HTTP_REFERER'] = '/another/url/'
17+        self.req.META['HTTP_USER_AGENT'] = 'Spider machine 3.4'
18+        SubclassedMiddleware().process_response(self.req, self.resp)
19+        self.assertEqual(len(mail.outbox), 0)
20+        self.req.META['HTTP_USER_AGENT'] = 'My user agent'
21+        SubclassedMiddleware().process_response(self.req, self.resp)
22+        self.assertEqual(len(mail.outbox), 1)
23+
24 
25 class ConditionalGetMiddlewareTest(TestCase):
26     urls = 'middleware.cond_get_urls'
27