Improve test coverage for responsetypes.py (#7646)

* Improve test coverage for responsetypes.py

* Solve typing issues
This commit is contained in:
Adrian 2026-06-22 17:10:38 +02:00 committed by GitHub
parent b6596de317
commit 7499d17e28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 6 deletions

View File

@ -40,18 +40,13 @@ class ResponseTypes:
self.classes: dict[str, type[Response]] = {}
self.mimetypes: MimeTypes = MimeTypes()
mimedata = get_data("scrapy", "mime.types")
if not mimedata:
raise ValueError(
"The mime.types file is not found in the Scrapy installation"
)
assert mimedata is not None
self.mimetypes.readfp(StringIO(mimedata.decode("utf8")))
for mimetype, cls in self.CLASSES.items():
self.classes[mimetype] = load_object(cls)
def from_mimetype(self, mimetype: str) -> type[Response]:
"""Return the most appropriate Response class for the given mimetype"""
if mimetype is None:
return Response
if mimetype in self.classes:
return self.classes[mimetype]
basetype = f"{mimetype.split('/', maxsplit=1)[0]}/*"

View File

@ -40,6 +40,9 @@ class TestResponseTypes:
retcls = responsetypes.from_content_disposition(source)
assert retcls is cls, f"{source} ==> {retcls} != {cls}"
def test_from_content_disposition_no_filename(self):
assert responsetypes.from_content_disposition(b"attachment") is Response
def test_from_content_type(self):
mappings = [
("text/html; charset=UTF-8", HtmlResponse),