From fcc4d30df0e2f86f14fabac4f537f449fc60f83b Mon Sep 17 00:00:00 2001 From: Andrey Rakhmatullin Date: Sat, 1 Feb 2025 23:47:12 +0500 Subject: [PATCH] Small cleanup. --- tests/test_downloader_handlers.py | 15 ++++++++------- tests/test_downloader_handlers_http2.py | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/test_downloader_handlers.py b/tests/test_downloader_handlers.py index d816e3aed..81a0a62ee 100644 --- a/tests/test_downloader_handlers.py +++ b/tests/test_downloader_handlers.py @@ -16,7 +16,7 @@ from twisted.internet import defer, error, reactor from twisted.protocols.policies import WrappingFactory from twisted.trial import unittest from twisted.web import resource, server, static, util -from twisted.web._newclient import ResponseFailed +from twisted.web.client import ResponseFailed from twisted.web.http import _DataLoss from w3lib.url import path_to_file_uri @@ -183,10 +183,7 @@ class BrokenDownloadResource(resource.Resource): def closeConnection(request): # We have to force a disconnection for HTTP/1.1 clients. Otherwise # client keeps the connection open waiting for more data. - if hasattr(request.channel, "loseConnection"): # twisted >=16.3.0 - request.channel.loseConnection() - else: - request.channel.transport.loseConnection() + request.channel.loseConnection() request.finish() @@ -703,6 +700,7 @@ class Http11MockServerTestCase(unittest.TestCase): """HTTP 1.1 test case with MockServer""" settings_dict: dict | None = None + is_secure = False def setUp(self): self.mockserver = MockServer() @@ -718,7 +716,8 @@ class Http11MockServerTestCase(unittest.TestCase): # download it yield crawler.crawl( seed=Request( - url=self.mockserver.url("/partial"), meta={"download_maxsize": 1000} + url=self.mockserver.url("/partial", is_secure=self.is_secure), + meta={"download_maxsize": 1000}, ) ) failure = crawler.spider.meta["failure"] @@ -727,7 +726,9 @@ class Http11MockServerTestCase(unittest.TestCase): @defer.inlineCallbacks def test_download(self): crawler = get_crawler(SingleRequestSpider, self.settings_dict) - yield crawler.crawl(seed=Request(url=self.mockserver.url(""))) + yield crawler.crawl( + seed=Request(url=self.mockserver.url("", is_secure=self.is_secure)) + ) failure = crawler.spider.meta.get("failure") self.assertTrue(failure is None) reason = crawler.spider.meta["close_reason"] diff --git a/tests/test_downloader_handlers_http2.py b/tests/test_downloader_handlers_http2.py index cc5df7b60..d699263ac 100644 --- a/tests/test_downloader_handlers_http2.py +++ b/tests/test_downloader_handlers_http2.py @@ -196,8 +196,6 @@ class Https2InvalidDNSPattern(Https2TestCase): @skipIf(not H2_ENABLED, "HTTP/2 support in Twisted is not enabled") class Https2CustomCiphers(Https11CustomCiphers): - scheme = "https" - @property def download_handler_cls(self) -> type[DownloadHandlerProtocol]: from scrapy.core.downloader.handlers.http2 import H2DownloadHandler @@ -205,6 +203,7 @@ class Https2CustomCiphers(Https11CustomCiphers): return H2DownloadHandler +@skipIf(not H2_ENABLED, "HTTP/2 support in Twisted is not enabled") class Http2MockServerTestCase(Http11MockServerTestCase): """HTTP 2.0 test case with MockServer""" @@ -213,6 +212,7 @@ class Http2MockServerTestCase(Http11MockServerTestCase): "https": "scrapy.core.downloader.handlers.http2.H2DownloadHandler" } } + is_secure = True @skipIf(not H2_ENABLED, "HTTP/2 support in Twisted is not enabled")