mirror of https://github.com/scrapy/scrapy.git
triggering a warning when user puts URL in allowed_domains now covered by test
This commit is contained in:
parent
91ff194d1e
commit
8ec3b476b0
|
|
@ -6,6 +6,7 @@ See documentation in docs/topics/spider-middleware.rst
|
|||
|
||||
import re
|
||||
import logging
|
||||
import warnings
|
||||
|
||||
from scrapy import signals
|
||||
from scrapy.http import Request
|
||||
|
|
@ -55,7 +56,7 @@ class OffsiteMiddleware(object):
|
|||
for domain in allowed_domains:
|
||||
url_pattern = re.compile("^https?://.*$")
|
||||
if url_pattern.match(domain):
|
||||
logger.warn("allowed_domains accepts only domains, not URLs. Ignoring URL entry %s in allowed_domains." % domain)
|
||||
warnings.warn("allowed_domains accepts only domains, not URLs. Ignoring URL entry %s in allowed_domains." % domain, Warning)
|
||||
regex = r'^(.*\.)?(%s)$' % '|'.join(re.escape(d) for d in allowed_domains if d is not None)
|
||||
return re.compile(regex)
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from scrapy.http import Response, Request
|
|||
from scrapy.spiders import Spider
|
||||
from scrapy.spidermiddlewares.offsite import OffsiteMiddleware
|
||||
from scrapy.utils.test import get_crawler
|
||||
import warnings
|
||||
|
||||
class TestOffsiteMiddleware(TestCase):
|
||||
|
||||
|
|
@ -68,3 +69,13 @@ class TestOffsiteMiddleware4(TestOffsiteMiddleware3):
|
|||
reqs = [Request('http://scrapytest.org/1')]
|
||||
out = list(self.mw.process_spider_output(res, reqs, self.spider))
|
||||
self.assertEqual(out, reqs)
|
||||
|
||||
|
||||
class TestOffsiteMiddleware5(TestOffsiteMiddleware4):
|
||||
|
||||
def test_get_host_regex(self):
|
||||
self.spider.allowed_domains = ['http://scrapytest.org', 'scrapy.org', 'scrapy.test.org']
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
self.mw.get_host_regex(self.spider)
|
||||
assert "allowed_domains accepts only domains, not URLs." in str(w[-1].message)
|
||||
|
|
|
|||
Loading…
Reference in New Issue