mirror of https://github.com/scrapy/scrapy.git
Improve test coverage for responsetypes.py (#7646)
* Improve test coverage for responsetypes.py * Solve typing issues
This commit is contained in:
parent
b6596de317
commit
7499d17e28
|
|
@ -40,18 +40,13 @@ class ResponseTypes:
|
||||||
self.classes: dict[str, type[Response]] = {}
|
self.classes: dict[str, type[Response]] = {}
|
||||||
self.mimetypes: MimeTypes = MimeTypes()
|
self.mimetypes: MimeTypes = MimeTypes()
|
||||||
mimedata = get_data("scrapy", "mime.types")
|
mimedata = get_data("scrapy", "mime.types")
|
||||||
if not mimedata:
|
assert mimedata is not None
|
||||||
raise ValueError(
|
|
||||||
"The mime.types file is not found in the Scrapy installation"
|
|
||||||
)
|
|
||||||
self.mimetypes.readfp(StringIO(mimedata.decode("utf8")))
|
self.mimetypes.readfp(StringIO(mimedata.decode("utf8")))
|
||||||
for mimetype, cls in self.CLASSES.items():
|
for mimetype, cls in self.CLASSES.items():
|
||||||
self.classes[mimetype] = load_object(cls)
|
self.classes[mimetype] = load_object(cls)
|
||||||
|
|
||||||
def from_mimetype(self, mimetype: str) -> type[Response]:
|
def from_mimetype(self, mimetype: str) -> type[Response]:
|
||||||
"""Return the most appropriate Response class for the given mimetype"""
|
"""Return the most appropriate Response class for the given mimetype"""
|
||||||
if mimetype is None:
|
|
||||||
return Response
|
|
||||||
if mimetype in self.classes:
|
if mimetype in self.classes:
|
||||||
return self.classes[mimetype]
|
return self.classes[mimetype]
|
||||||
basetype = f"{mimetype.split('/', maxsplit=1)[0]}/*"
|
basetype = f"{mimetype.split('/', maxsplit=1)[0]}/*"
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,9 @@ class TestResponseTypes:
|
||||||
retcls = responsetypes.from_content_disposition(source)
|
retcls = responsetypes.from_content_disposition(source)
|
||||||
assert retcls is cls, f"{source} ==> {retcls} != {cls}"
|
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):
|
def test_from_content_type(self):
|
||||||
mappings = [
|
mappings = [
|
||||||
("text/html; charset=UTF-8", HtmlResponse),
|
("text/html; charset=UTF-8", HtmlResponse),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue