Skip to content
Open
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
6 changes: 4 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ unreleased
Features
--------

- Add `Configurator.resolve_asset` and `Request.resolve_asset` methods. See
https://github.com/Pylons/pyramid/pull/3815.
- Add ``Configurator.resolve_asset`` and ``Request.resolve_asset`` methods.
See https://github.com/Pylons/pyramid/pull/3815.
- Add asset specification support to ``pyramid.response.FileResponse``. See
https://github.com/Pylons/pyramid/pull/3836.

Bug Fixes
---------
Expand Down
6 changes: 5 additions & 1 deletion src/pyramid/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from zope.interface import implementer

from pyramid.interfaces import IResponse, IResponseFactory
from pyramid.threadlocal import get_current_request

_BLOCK_SIZE = 4096 * 64 # 256K

Expand All @@ -19,7 +20,7 @@ class FileResponse(Response):
A Response object that can be used to serve a static file from disk
simply.

``path`` is a file path on disk.
``path`` is a path on the filesystem or an :term:`asset specification`.

``request`` must be a Pyramid :term:`request` object. Note
that a request *must* be passed if the response is meant to attempt to
Expand Down Expand Up @@ -52,6 +53,9 @@ def __init__(
content_type=content_type,
content_encoding=content_encoding,
)
if request is None:
request = get_current_request()
path = request.resolve_asset(path).abspath()
self.last_modified = getmtime(path)
content_length = getsize(path)
f = open(path, 'rb')
Expand Down
90 changes: 56 additions & 34 deletions tests/test_response.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from datetime import datetime, timezone
import io
import mimetypes
import os
from os.path import getmtime, getsize
import unittest
from unittest.mock import Mock

from pyramid import testing
from pyramid.request import Request


class TestResponse(unittest.TestCase):
Expand All @@ -26,72 +30,90 @@ def test_provides_IResponse(self):


class TestFileResponse(unittest.TestCase):
def setUp(self):
self.request = Request.blank('/')
self.config = testing.setUp(request=self.request)

def tearDown(self):
testing.tearDown()

def _makeOne(self, file, **kw):
from pyramid.response import FileResponse

return FileResponse(file, **kw)

def _getPath(self, suffix='txt'):
def _get_path(self, suffix='txt'):
here = os.path.dirname(__file__)
return os.path.join(here, 'fixtures', f'minimal.{suffix}')

def _validate_content(self, r, suffix='txt'):
path = self._get_path(suffix)
with open(path, 'rb') as fh:
expected = fh.read()
self.assertEqual(r.body, expected, 'File contents do not match.')
expected_dt = datetime.fromtimestamp(getmtime(path), timezone.utc)
expected_dt = expected_dt.replace(microsecond=0)
self.assertEqual(r.last_modified, expected_dt)
self.assertEqual(r.content_length, getsize(path))

def test_with_image_content_type(self):
path = self._getPath('jpg')
path = self._get_path('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()
r.app_iter.close()
self._validate_content(r, 'jpg')

def test_with_xml_content_type(self):
path = self._getPath('xml')
path = self._get_path('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()
self._validate_content(r, 'xml')

def test_with_pdf_content_type(self):
path = self._getPath('xml')
path = self._get_path('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()
self._validate_content(r, 'xml')

def test_without_content_type(self):
for suffix in ('txt', 'xml', 'pdf'):
path = self._getPath(suffix)
path = self._get_path(suffix)
r = self._makeOne(path)
self.assertEqual(
r.headers['content-type'].split(';')[0],
mimetypes.guess_type(path, strict=False)[0],
)
r.app_iter.close()

def test_python_277_bug_15207(self):
# python 2.7.7 on windows has a bug where its mimetypes.guess_type
# function returns Unicode for the content_type, unlike any previous
# version of Python. See https://github.com/Pylons/pyramid/issues/1360
# for more information.
import mimetypes as old_mimetypes

from pyramid import response
from pyramid.util import text_

class FakeMimetypesModule:
def guess_type(self, *arg, **kw):
return text_('foo/bar'), None

fake_mimetypes = FakeMimetypesModule()
try:
response.mimetypes = fake_mimetypes
path = self._getPath('xml')
r = self._makeOne(path)
self.assertEqual(r.content_type, 'foo/bar')
self.assertEqual(type(r.content_type), str)
finally:
response.mimetypes = old_mimetypes
self._validate_content(r, suffix)

def test_wsgi_file_wrapper(self):
from pyramid.response import FileIter

wrapper = Mock(side_effect=FileIter)
request = Request.blank(
'/',
environ={
'wsgi.file_wrapper': wrapper,
},
)

path = self._get_path()
r = self._makeOne(path, request=request)
self._validate_content(r)

wrapper.assert_called_once()

def test_asset_spec(self):
r = self._makeOne('tests:fixtures/minimal.txt')
self._validate_content(r)

def test_asset_spec_explicit_request(self):
request = Request.blank('/')
r = self._makeOne('tests:fixtures/minimal.txt', request=request)
self._validate_content(r)


class TestFileIter(unittest.TestCase):
Expand Down
Loading