Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't incorrectly default charset on FileResponse #1251

Merged
merged 3 commits into from
May 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pyramid/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ class FileResponse(Response):
"""
def __init__(self, path, request=None, cache_max_age=None,
content_type=None, content_encoding=None):
super(FileResponse, self).__init__(conditional_response=True)
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_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)
content_length = getsize(path)
f = open(path, 'rb')
app_iter = None
Expand Down
Binary file added pyramid/tests/fixtures/minimal.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pyramid/tests/fixtures/minimal.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions pyramid/tests/fixtures/minimal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<hello/>
37 changes: 29 additions & 8 deletions pyramid/tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,43 @@ def _makeOne(self, file, **kw):
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_without_content_type(self):
path = self._getPath()
r = self._makeOne(path)
self.assertEqual(r.content_type, 'text/plain')
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):
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):
from pyramid.response import FileIter
Expand Down