Skip to content

Commit

Permalink
Test exception in _handle_download
Browse files Browse the repository at this point in the history
  • Loading branch information
elacuesta committed Sep 8, 2023
1 parent 039ab4f commit 9843cbb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions scrapy_playwright/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,13 @@ async def _download_request_with_page(
server_addr = await response.server_addr()
server_ip_address = ip_address(server_addr["ipAddress"])

if download.get("exception"):
raise download["exception"]

if not request.meta.get("playwright_include_page"):
await page.close()
self.stats.inc_value("playwright/page_count/closed")

if download.get("exception"):
raise download["exception"]
if download:
request.meta["playwright_suggested_filename"] = download.get("suggested_filename")
respcls = responsetypes.from_args(url=download["url"], body=download["bytes"])
Expand Down Expand Up @@ -420,7 +421,7 @@ async def _get_response_and_download(
self, request: Request, page: Page
) -> Tuple[Optional[PlaywrightResponse], dict]:
response: Optional[PlaywrightResponse] = None
download: dict = {} # updated in-place in _download_event_handler
download: dict = {} # updated in-place in _handle_download
download_ready = asyncio.Event()

async def _handle_download(dwnld: Download) -> None:
Expand Down
16 changes: 16 additions & 0 deletions tests/tests_asyncio/test_playwright_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ async def test_download_file_delay_error(self):
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
) in self._caplog.record_tuples

@patch("scrapy_playwright.handler.NamedTemporaryFile")
@pytest.mark.asyncio
async def test_download_file_exception(self, NamedTemporaryFile):
NamedTemporaryFile.side_effect = ZeroDivisionError("asdf")
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
with MockServer() as server:
request = Request(url=server.urljoin("/mancha.pdf"), meta={"playwright": True})
with pytest.raises(ZeroDivisionError) as excinfo:
await handler._download_request(request, Spider("foo"))
assert (
"scrapy-playwright",
logging.WARNING,
f"Closing page due to failed request: {request}"
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
) in self._caplog.record_tuples


class TestCaseChromium(IsolatedAsyncioTestCase, MixinTestCase):
browser_type = "chromium"
Expand Down

0 comments on commit 9843cbb

Please sign in to comment.