Michael Merickel
2014-05-17 3c6443134449e392ff7e55c4365e968b7dad3327
Merge branch 'patch-3' of dobesv/pyramid into pull.1251
3 files added
2 files modified
55 ■■■■ changed files
pyramid/response.py 17 ●●●● patch | view | raw | blame | history
pyramid/tests/fixtures/minimal.jpg patch | view | raw | blame | history
pyramid/tests/fixtures/minimal.pdf patch | view | raw | blame | history
pyramid/tests/fixtures/minimal.xml 1 ●●●● patch | view | raw | blame | history
pyramid/tests/test_response.py 37 ●●●● patch | view | raw | blame | history
pyramid/response.py
@@ -52,15 +52,16 @@
    """
    def __init__(self, path, request=None, cache_max_age=None,
                 content_type=None, content_encoding=None):
        super(FileResponse, self).__init__(conditional_response=True)
        if content_type is None:
            content_type, content_encoding = mimetypes.guess_type(path, strict=False)
            if content_type is None:
                content_type = 'application/octet-stream'
        super(FileResponse, self).__init__(
            conditional_response=True,
            content_type=content_type,
            content_encoding=content_encoding
        )
        self.last_modified = getmtime(path)
        if content_type is None:
            content_type, content_encoding = mimetypes.guess_type(path,
                                                                  strict=False)
        if content_type is None:
            content_type = 'application/octet-stream'
        self.content_type = content_type
        self.content_encoding = content_encoding
        content_length = getsize(path)
        f = open(path, 'rb')
        app_iter = None
pyramid/tests/fixtures/minimal.jpg
pyramid/tests/fixtures/minimal.pdf
Binary files differ
pyramid/tests/fixtures/minimal.xml
New file
@@ -0,0 +1 @@
<hello/>
pyramid/tests/test_response.py
@@ -23,21 +23,42 @@
        from pyramid.response import FileResponse
        return FileResponse(file, **kw)
    def _getPath(self):
    def _getPath(self, suffix='txt'):
        here = os.path.dirname(__file__)
        return os.path.join(here, 'fixtures', 'minimal.txt')
        return os.path.join(here, 'fixtures', 'minimal.%s'%(suffix,))
    def test_with_content_type(self):
        path = self._getPath()
    def test_with_image_content_type(self):
        path = self._getPath('jpg')
        r = self._makeOne(path, content_type='image/jpeg')
        self.assertEqual(r.content_type, 'image/jpeg')
        self.assertEqual(r.headers['content-type'], 'image/jpeg')
        path = self._getPath()
    def test_with_xml_content_type(self):
        path = self._getPath('xml')
        r = self._makeOne(path, content_type='application/xml')
        self.assertEqual(r.content_type, 'application/xml')
        self.assertEqual(r.headers['content-type'], 'application/xml; charset=UTF-8')
        r.app_iter.close()
    def test_with_pdf_content_type(self):
        path = self._getPath('xml')
        r = self._makeOne(path, content_type='application/pdf')
        self.assertEqual(r.content_type, 'application/pdf')
        self.assertEqual(r.headers['content-type'], 'application/pdf')
        r.app_iter.close()
    def test_without_content_type(self):
        path = self._getPath()
        r = self._makeOne(path)
        self.assertEqual(r.content_type, 'text/plain')
        r.app_iter.close()
        for suffix, content_type in (
            ('txt', 'text/plain; charset=UTF-8'),
            ('xml', 'application/xml; charset=UTF-8'),
            ('pdf', 'application/pdf')
        ):
            path = self._getPath(suffix)
            r = self._makeOne(path)
            self.assertEqual(r.content_type, content_type.split(';')[0])
            self.assertEqual(r.headers['content-type'], content_type)
            r.app_iter.close()
class TestFileIter(unittest.TestCase):
    def _makeOne(self, file, block_size):