Scrapy, a fast high-level web crawling & scraping framework for Python.
added more cases to ResponseTypes, and tests for ResponseTypes
Pablo Hoffmane25bfa5d88be
diff --git a/scrapy/trunk/scrapy/core/downloader/responsetypes.py b/scrapy/trunk/scrapy/core/downloader/responsetypes.pyindex d15dddc3b..0e2868aec 100644--- a/scrapy/trunk/scrapy/core/downloader/responsetypes.py+++ b/scrapy/trunk/scrapy/core/downloader/responsetypes.py@@ -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 Responsedef from_url(self, url):"""Return the most appropiate Response class from a URL"""diff --git a/scrapy/trunk/scrapy/tests/test_responsetypes.py b/scrapy/trunk/scrapy/tests/test_responsetypes.pynew file mode 100644index 000000000..b956320b9--- /dev/null+++ b/scrapy/trunk/scrapy/tests/test_responsetypes.py@@ -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()