Scrapy, a fast high-level web crawling & scraping framework for Python.
Test HttpCompressionMiddleware subclasses with custom, parameterless __init__
Adrián Chavesa3e53027ec35
diff --git a/scrapy/downloadermiddlewares/httpcompression.py b/scrapy/downloadermiddlewares/httpcompression.pyindex 87f744956..1808154d2 100644--- a/scrapy/downloadermiddlewares/httpcompression.py+++ b/scrapy/downloadermiddlewares/httpcompression.py@@ -1,9 +1,11 @@+import warningsimport zlib-from scrapy.utils.gz import gunzip+from scrapy.exceptions import NotConfiguredfrom scrapy.http import Response, TextResponsefrom scrapy.responsetypes import responsetypes-from scrapy.exceptions import NotConfigured+from scrapy.utils.deprecate import ScrapyDeprecationWarning+from scrapy.utils.gz import gunzipACCEPTED_ENCODINGS = [b'gzip', b'deflate']@@ -28,6 +30,12 @@ class HttpCompressionMiddleware:try:return cls(stats=crawler.stats)except TypeError:+ warnings.warn(+ "HttpCompressionMiddleware subclasses must either modify "+ "their '__init__' method to support a 'stats' parameter or "+ "reimplement the 'from_crawler' method.",+ ScrapyDeprecationWarning,+ )result = cls()result.stats = crawler.statsreturn resultdiff --git a/tests/test_downloadermiddleware_httpcompression.py b/tests/test_downloadermiddleware_httpcompression.pyindex c2bcbd63c..2ee87aa67 100644--- a/tests/test_downloadermiddleware_httpcompression.py+++ b/tests/test_downloadermiddleware_httpcompression.py@@ -1,12 +1,13 @@+from gzip import GzipFilefrom io import BytesIO-from unittest import TestCase, SkipTestfrom os.path import join-from gzip import GzipFile+from unittest import TestCase, SkipTest+from warnings import catch_warningsfrom scrapy.spiders import Spiderfrom scrapy.http import Response, Request, HtmlResponsefrom scrapy.downloadermiddlewares.httpcompression import HttpCompressionMiddleware, ACCEPTED_ENCODINGS-from scrapy.exceptions import NotConfigured+from scrapy.exceptions import NotConfigured, ScrapyDeprecationWarningfrom scrapy.responsetypes import responsetypesfrom scrapy.utils.gz import gunzipfrom scrapy.utils.test import get_crawler@@ -321,3 +322,30 @@ class HttpCompressionTest(TestCase):self.assertEqual(response.body, b'')self.assertStatsEqual('httpcompression/response_count', None)self.assertStatsEqual('httpcompression/response_bytes', None)+++class HttpCompressionSubclassTest(TestCase):++ def test_init_missing_stats(self):+ class HttpCompressionMiddlewareSubclass(HttpCompressionMiddleware):++ def __init__(self):+ super().__init__()++ crawler = get_crawler(Spider)+ with catch_warnings(record=True) as caught_warnings:+ instance = HttpCompressionMiddlewareSubclass.from_crawler(crawler)+ messages = tuple(+ str(warning.message) for warning in caught_warnings+ if warning.category is ScrapyDeprecationWarning+ )+ self.assertEqual(+ messages,+ (+ (+ "HttpCompressionMiddleware subclasses must either modify "+ "their '__init__' method to support a 'stats' parameter "+ "or reimplement the 'from_crawler' method."+ ),+ )+ )