Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Treat HTTP 201 as a successful download in FilesPipeline (#8349)

Adrian09b84e3691b0
diff --git a/scrapy/pipelines/files.py b/scrapy/pipelines/files.py
index e580045a8..67dad7651 100644
--- a/scrapy/pipelines/files.py
+++ b/scrapy/pipelines/files.py
@@ -781,7 +781,7 @@ class FilesPipeline(MediaPipeline):
) -> FileInfo:
referer = referer_str(request)
- if response.status != 200:
+ if response.status not in {200, 201}:
logger.warning(
"File (code: %(status)s): Error downloading file from "
"%(request)s referred in <%(referer)s>",
diff --git a/tests/test_pipeline_files.py b/tests/test_pipeline_files.py
index 3aface043..b943e6e9e 100644
--- a/tests/test_pipeline_files.py
+++ b/tests/test_pipeline_files.py
@@ -446,6 +446,20 @@ class TestFilesPipeline:
assert path.exists()
assert path.read_bytes() == b"data"
+ @coroutine_test
+ async def test_file_created(self) -> None:
+ item_url = "http://example.com/created.pdf"
+ item = _create_item_with_files(item_url)
+ with mock.patch.object(
+ FilesPipeline,
+ "get_media_requests",
+ return_value=[_prepare_request_object(item_url, status=201)],
+ ):
+ result = await self.pipeline.process_item(item)
+ assert result["files"][0]["status"] == "downloaded"
+ path = Path(self.tempdir) / result["files"][0]["path"]
+ assert path.read_bytes() == b"data"
+
@coroutine_test
async def test_checksum_algorithm(self) -> None:
pipeline = self._create_pipeline(
@@ -1596,12 +1610,13 @@ def _prepare_request_object(
item_url: str,
flags: list[str] | None = None,
headers: dict[str, str] | None = None,
+ status: int = 200,
) -> Request:
return Request(
item_url,
meta={
"response": Response(
- item_url, status=200, body=b"data", flags=flags, headers=headers
+ item_url, status=status, body=b"data", flags=flags, headers=headers
)
},
)