Skip to content

Commit

Permalink
Use wsgi.file_wrapper to serve static files, if available
Browse files Browse the repository at this point in the history
  • Loading branch information
smarzola committed Aug 12, 2013
1 parent 4157f63 commit 3b6911e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions tests/test_statics.py
@@ -1,5 +1,6 @@
from webtest import TestApp
from nose.tools import raises
from webob import Request
from tg.support.statics import StaticsMiddleware, FileServeApp
from webob.exc import HTTPBadRequest, HTTPForbidden
from datetime import datetime
Expand Down Expand Up @@ -61,3 +62,26 @@ def test_FileApp_non_existing_file(self):
fa = TestApp(FileServeApp('this_does_not_exists.unknown', 0))
r = fa.get('/', status=403)
assert '403' in r

def test_wsgi_file_wrapper(self):
class DummyWrapper(object):
def __init__(self, file, block_size):
self.file = file
self.block_size = block_size

environ = {
'wsgi.url_scheme': 'http',
'wsgi.version':(1,0),
'wsgi.file_wrapper': DummyWrapper,
'SERVER_NAME': 'somedomain.com',
'SERVER_PORT': '8080',
'PATH_INFO': '/index.html',
'SCRIPT_NAME': '',
'REQUEST_METHOD': 'GET',
}

app = FileServeApp('./tests/test.html', 3600)
app_iter = Request(environ).send(app).app_iter
assert isinstance(app_iter, DummyWrapper)
assert 'Welcome to TurboGears 2.0' in app_iter.file.read()
app_iter.file.close()
2 changes: 1 addition & 1 deletion tg/support/statics.py
Expand Up @@ -119,7 +119,7 @@ def __call__(self, environ, start_response):
('Last-Modified', self.make_date(self.last_modified))
))
start_response('200 OK', headers)
return _FileIter(file, _BLOCK_SIZE)
return environ.get('wsgi.file_wrapper', _FileIter)(file, _BLOCK_SIZE)

INVALID_PATH_PARTS = set(['..', '.']).intersection

Expand Down

0 comments on commit 3b6911e

Please sign in to comment.