1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-21 06:52:59 +00:00

Add a non-asyncio async def middleware test.

This commit is contained in:
Andrey Rakhmatullin 2019-09-12 20:25:29 +05:00
parent 21f50c795a
commit 3603644552

View File

@ -1,6 +1,7 @@
import asyncio
from unittest import mock
from twisted.internet import defer
from twisted.internet.defer import Deferred
from twisted.trial.unittest import TestCase
from twisted.python.failure import Failure
@ -215,6 +216,25 @@ class MiddlewareUsingCoro(ManagerTestCase):
def test_asyncdef(self):
resp = Response('http://example.com/index.html')
class CoroMiddleware:
async def process_request(self, request, spider):
await defer.succeed(42)
return resp
self.mwman._add_middleware(CoroMiddleware())
req = Request('http://example.com/index.html')
download_func = mock.MagicMock()
dfd = self.mwman.download(download_func, req, self.spider)
results = []
dfd.addBoth(results.append)
self._wait(dfd)
self.assertIs(results[0], resp)
self.assertFalse(download_func.called)
def test_asyncdef_asyncio(self):
resp = Response('http://example.com/index.html')
class CoroMiddleware:
async def process_request(self, request, spider):
await asyncio.sleep(0.1)