Skip to content

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.py
index 87f744956..1808154d2 100644
--- a/scrapy/downloadermiddlewares/httpcompression.py
+++ b/scrapy/downloadermiddlewares/httpcompression.py
@@ -1,9 +1,11 @@
+import warnings
import zlib
-from scrapy.utils.gz import gunzip
+from scrapy.exceptions import NotConfigured
from scrapy.http import Response, TextResponse
from scrapy.responsetypes import responsetypes
-from scrapy.exceptions import NotConfigured
+from scrapy.utils.deprecate import ScrapyDeprecationWarning
+from scrapy.utils.gz import gunzip
ACCEPTED_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.stats
return result
diff --git a/tests/test_downloadermiddleware_httpcompression.py b/tests/test_downloadermiddleware_httpcompression.py
index c2bcbd63c..2ee87aa67 100644
--- a/tests/test_downloadermiddleware_httpcompression.py
+++ b/tests/test_downloadermiddleware_httpcompression.py
@@ -1,12 +1,13 @@
+from gzip import GzipFile
from io import BytesIO
-from unittest import TestCase, SkipTest
from os.path import join
-from gzip import GzipFile
+from unittest import TestCase, SkipTest
+from warnings import catch_warnings
from scrapy.spiders import Spider
from scrapy.http import Response, Request, HtmlResponse
from scrapy.downloadermiddlewares.httpcompression import HttpCompressionMiddleware, ACCEPTED_ENCODINGS
-from scrapy.exceptions import NotConfigured
+from scrapy.exceptions import NotConfigured, ScrapyDeprecationWarning
from scrapy.responsetypes import responsetypes
from scrapy.utils.gz import gunzip
from 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."
+ ),
+ )
+ )