diff --git a/CHANGES.rst b/CHANGES.rst index c4ca57431..877254fb8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 --------- diff --git a/src/pyramid/response.py b/src/pyramid/response.py index 3090d7e75..6a3ca3a8e 100644 --- a/src/pyramid/response.py +++ b/src/pyramid/response.py @@ -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 @@ -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 @@ -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') diff --git a/tests/test_response.py b/tests/test_response.py index 63def1212..bec795e35 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -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): @@ -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):