mirror of https://github.com/scrapy/scrapy.git
added more cases to ResponseTypes, and tests for ResponseTypes
--HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40781
This commit is contained in:
parent
b6246cbc37
commit
e25bfa5d88
|
|
@ -39,6 +39,7 @@ class ResponseTypes(object):
|
|||
def from_content_disposition(self, content_disposition):
|
||||
try:
|
||||
filename = content_disposition.split(';')[1].split('=')[1]
|
||||
filename = filename.strip('"\'')
|
||||
return self.from_filename(filename)
|
||||
except IndexError:
|
||||
return Response
|
||||
|
|
@ -55,7 +56,8 @@ class ResponseTypes(object):
|
|||
|
||||
def from_filename(self, filename):
|
||||
"""Return the most appropiate Response class from a file name"""
|
||||
return self.from_mimetype(mimetypes.guess_type(filename)[0])
|
||||
mimetype, encoding = mimetypes.guess_type(filename)
|
||||
return self.from_mimetype(mimetype) if encoding is None else Response
|
||||
|
||||
def from_url(self, url):
|
||||
"""Return the most appropiate Response class from a URL"""
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
import unittest
|
||||
from scrapy.core.downloader.responsetypes import responsetypes
|
||||
|
||||
from scrapy.http import Response, TextResponse, XmlResponse, HtmlResponse, Headers
|
||||
|
||||
class ResponseTypesTest(unittest.TestCase):
|
||||
|
||||
def test_from_filename(self):
|
||||
mappings = [
|
||||
('data.bin', Response),
|
||||
('file.txt', TextResponse),
|
||||
('file.xml.gz', Response),
|
||||
('file.xml', XmlResponse),
|
||||
('file.html', HtmlResponse),
|
||||
]
|
||||
for source, cls in mappings:
|
||||
retcls = responsetypes.from_filename(source)
|
||||
assert retcls is cls, "%s ==> %s != %s" % (source, retcls, cls)
|
||||
|
||||
def test_from_content_disposition(self):
|
||||
mappings = [
|
||||
('attachment; filename="data.xml"', XmlResponse),
|
||||
('attachment; filename=data.xml', XmlResponse),
|
||||
]
|
||||
for source, cls in mappings:
|
||||
retcls = responsetypes.from_content_disposition(source)
|
||||
assert retcls is cls, "%s ==> %s != %s" % (source, retcls, cls)
|
||||
|
||||
def test_from_content_type(self):
|
||||
mappings = [
|
||||
('text/html; charset=UTF-8', HtmlResponse),
|
||||
('text/xml; charset=UTF-8', XmlResponse),
|
||||
('application/xml; charset=UTF-8', XmlResponse),
|
||||
('application/octet-stream', Response),
|
||||
]
|
||||
for source, cls in mappings:
|
||||
retcls = responsetypes.from_content_type(source)
|
||||
assert retcls is cls, "%s ==> %s != %s" % (source, retcls, cls)
|
||||
|
||||
def test_from_body(self):
|
||||
mappings = [
|
||||
('\x03\x02\xdf\xdd\x23', Response),
|
||||
('Some plain text\ndata with tabs\t and null bytes\0', TextResponse),
|
||||
('<html><head><title>Hello</title></head>', HtmlResponse),
|
||||
('<?xml version="1.0" encoding="utf-8"', XmlResponse),
|
||||
]
|
||||
for source, cls in mappings:
|
||||
retcls = responsetypes.from_body(source)
|
||||
assert retcls is cls, "%s ==> %s != %s" % (source, retcls, cls)
|
||||
|
||||
def test_from_headers(self):
|
||||
mappings = [
|
||||
({'Content-Type': ['text/html; charset=utf-8']}, HtmlResponse),
|
||||
({'Content-Type': ['application/octet-stream'], 'Content-Disposition': ['attachment; filename=data.csv']}, TextResponse),
|
||||
]
|
||||
for source, cls in mappings:
|
||||
source = Headers(source)
|
||||
retcls = responsetypes.from_headers(source)
|
||||
assert retcls is cls, "%s ==> %s != %s" % (source, retcls, cls)
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Reference in New Issue