mirror of https://github.com/scrapy/scrapy.git
Add type check in get_media_requests to enforce image / file urls as a list (#6949)
This commit is contained in:
parent
e225d0dea4
commit
9e83a58643
|
|
@ -704,6 +704,10 @@ class FilesPipeline(MediaPipeline):
|
|||
self, item: Any, info: MediaPipeline.SpiderInfo
|
||||
) -> list[Request]:
|
||||
urls = ItemAdapter(item).get(self.files_urls_field, [])
|
||||
if not isinstance(urls, list):
|
||||
raise TypeError(
|
||||
f"{self.files_urls_field} must be a list of URLs, got {type(urls).__name__}. "
|
||||
)
|
||||
return [Request(u, callback=NO_CALLBACK) for u in urls]
|
||||
|
||||
def file_downloaded(
|
||||
|
|
|
|||
|
|
@ -241,6 +241,10 @@ class ImagesPipeline(FilesPipeline):
|
|||
self, item: Any, info: MediaPipeline.SpiderInfo
|
||||
) -> list[Request]:
|
||||
urls = ItemAdapter(item).get(self.images_urls_field, [])
|
||||
if not isinstance(urls, list):
|
||||
raise TypeError(
|
||||
f"{self.images_urls_field} must be a list of URLs, got {type(urls).__name__}. "
|
||||
)
|
||||
return [Request(u, callback=NO_CALLBACK) for u in urls]
|
||||
|
||||
def item_completed(
|
||||
|
|
|
|||
|
|
@ -261,6 +261,26 @@ class TestFilesPipeline:
|
|||
request = Request("http://example.com")
|
||||
assert file_path(request, item=item) == "full/path-to-store-file"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"bad_type",
|
||||
[
|
||||
"http://example.com/file.pdf",
|
||||
("http://example.com/file.pdf",),
|
||||
{"url": "http://example.com/file.pdf"},
|
||||
123,
|
||||
None,
|
||||
],
|
||||
)
|
||||
def test_rejects_non_list_file_urls(self, tmp_path, bad_type):
|
||||
pipeline = FilesPipeline.from_crawler(
|
||||
get_crawler(None, {"FILES_STORE": str(tmp_path)})
|
||||
)
|
||||
item = ItemWithFiles()
|
||||
item["file_urls"] = bad_type
|
||||
|
||||
with pytest.raises(TypeError, match="file_urls must be a list of URLs"):
|
||||
list(pipeline.get_media_requests(item, None))
|
||||
|
||||
|
||||
class TestFilesPipelineFieldsMixin(ABC):
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -209,6 +209,26 @@ class TestImagesPipeline:
|
|||
assert converted.mode == "RGB"
|
||||
assert converted.getcolors() == [(10000, (205, 230, 255))]
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"bad_type",
|
||||
[
|
||||
"http://example.com/file.jpg",
|
||||
("http://example.com/file.jpg",),
|
||||
{"url": "http://example.com/file.jpg"},
|
||||
123,
|
||||
None,
|
||||
],
|
||||
)
|
||||
def test_rejects_non_list_image_urls(self, tmp_path, bad_type):
|
||||
pipeline = ImagesPipeline.from_crawler(
|
||||
get_crawler(None, {"IMAGES_STORE": str(tmp_path)})
|
||||
)
|
||||
item = ImagesPipelineTestItem()
|
||||
item["image_urls"] = bad_type
|
||||
|
||||
with pytest.raises(TypeError, match="image_urls must be a list of URLs"):
|
||||
list(pipeline.get_media_requests(item, None))
|
||||
|
||||
|
||||
class TestImagesPipelineFieldsMixin(ABC):
|
||||
@property
|
||||
|
|
|
|||
Loading…
Reference in New Issue