checking for subclass of URLWarning instead of checking error message text when URL in allowed_domains

This commit is contained in:
Jana Cavojska 2017-11-26 20:07:04 +01:00
parent 8ec3b476b0
commit 454d5e5733
2 changed files with 8 additions and 2 deletions

View File

@ -56,10 +56,15 @@ class OffsiteMiddleware(object):
for domain in allowed_domains:
url_pattern = re.compile("^https?://.*$")
if url_pattern.match(domain):
warnings.warn("allowed_domains accepts only domains, not URLs. Ignoring URL entry %s in allowed_domains." % domain, Warning)
warnings.warn("allowed_domains accepts only domains, not URLs. Ignoring URL entry %s in allowed_domains." % domain, URLWarning)
regex = r'^(.*\.)?(%s)$' % '|'.join(re.escape(d) for d in allowed_domains if d is not None)
return re.compile(regex)
def spider_opened(self, spider):
self.host_regex = self.get_host_regex(spider)
self.domains_seen = set()
class URLWarning(Warning):
pass

View File

@ -5,6 +5,7 @@ from six.moves.urllib.parse import urlparse
from scrapy.http import Response, Request
from scrapy.spiders import Spider
from scrapy.spidermiddlewares.offsite import OffsiteMiddleware
from scrapy.spidermiddlewares.offsite import URLWarning
from scrapy.utils.test import get_crawler
import warnings
@ -78,4 +79,4 @@ class TestOffsiteMiddleware5(TestOffsiteMiddleware4):
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)
assert issubclass(w[-1].category, URLWarning)