Pre n post xtractmime tests

This commit is contained in:
Akshay Sharma 2021-07-25 19:42:27 +05:30
parent 763ec07b7b
commit 1512ed22b2
1 changed files with 22 additions and 10 deletions

View File

@ -72,23 +72,35 @@ class ResponseTypesTest(unittest.TestCase):
retcls = responsetypes.from_headers(source)
assert retcls is cls, f"{source} ==> {retcls} != {cls}"
def test_from_args(self):
def test_from_args_pre_xtractmime(self):
mappings = [
({'url': 'http://www.example.com/data.csv'}, TextResponse),
({'headers': Headers({'Content-Type': ['text/plain; charset=utf-8']}),
'url': 'http://www.example.com/item/'}, TextResponse),
({'headers': Headers({'Content-Type': ['text/html; charset=utf-8']}),
'url': 'http://www.example.com/item/'}, HtmlResponse), # Failing with xtractmime, returning TextResponse expected HtmlResponse
({'headers': Headers({'Content-Disposition': ['attachment; filename="data.xml.gz"']}),
'url': 'http://www.example.com/page/'}, Response), # Failing with xtractmime, returning TextResponse expected Response
]
for source, cls in mappings:
retcls = responsetypes.from_args(**source)
assert retcls is cls, f"{source} ==> {retcls} != {cls}"
def test_from_args_post_xtractmime(self):
mappings = [
({'body': b'Some plain text data with tabs and null bytes',
'url': 'http://www.example.com/item/',
'headers': Headers({'Content-Type': ['text/html; charset=utf-8'], })},
TextResponse),
({'body': b'\x03\x02\xdf\xdd\x23', 'url': '://www.example.com/item/',
'headers': Headers({'Content-Encoding': 'UTF-8'})}, Response),
({'body': b'Some plain text data with tabs and null bytes'}, TextResponse),
({'body': b'\x03\x02\xdf\xdd\x23', 'headers': Headers({'Content-Encoding': 'UTF-8'})},
Response),
# different behaviour with http and non-http urls
({'body': b'\x00\xfe\xff', 'url': 'http://www.example.com/item/',
'headers': Headers({'Content-Type': b'text/plain'})}, Response),
({'body': b'\x00\xfe\xff', 'url': '://www.example.com/item/',
'headers': Headers({'Content-Type': b'text/plain'})}, TextResponse),
({'url': 'http://www.example.com/item/file.html'}, HtmlResponse), # Failing with xtractmime, return TextResponse expected HtmlResponse
({'body': b'Some plain text\ndata with tabs\t and null bytes\0'}, Response), # earlier expected to be binary response, refer "test_from_body()"
({'body': b'<html><head><title>Hello</title></head>'}, HtmlResponse),
({'body': b'<?xml version="1.0" encoding="utf-8"'}, XmlResponse),
({'headers': Headers({'Content-Disposition': ['attachment; filename="data.xml.gz"']}),
'url': 'http://www.example.com/page/'}, Response),
({'filename': 'file.txt'}, TextResponse),
({'filename': 'file.html'}, HtmlResponse), # Failing with xtractmime, returning TextResponse expected HtmlResponse
]
for source, cls in mappings:
retcls = responsetypes.from_args(**source)