From e25bfa5d88be33063ff0d2e0c0aac3380cef3570 Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Tue, 27 Jan 2009 11:27:09 +0000 Subject: [PATCH] added more cases to ResponseTypes, and tests for ResponseTypes --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40781 --- .../scrapy/core/downloader/responsetypes.py | 4 +- .../trunk/scrapy/tests/test_responsetypes.py | 61 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 scrapy/trunk/scrapy/tests/test_responsetypes.py diff --git a/scrapy/trunk/scrapy/core/downloader/responsetypes.py b/scrapy/trunk/scrapy/core/downloader/responsetypes.py index 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 Response def 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.py new file mode 100644 index 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), + ('Hello', HtmlResponse), + (' %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()