From ca520fb7e4c29afccc637e61c11429a0f3e8d5ad Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 14 Apr 2010 14:11:00 +0200 Subject: [PATCH] Static files are active in the WSGI app now, not just the server. --- flask.py | 10 +++++----- tests/flask_tests.py | 9 +++++++++ tests/static/index.html | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 tests/static/index.html diff --git a/flask.py b/flask.py index b45c49a4..68e25ade 100644 --- a/flask.py +++ b/flask.py @@ -16,7 +16,8 @@ from threading import local from contextlib import contextmanager from jinja2 import Environment, PackageLoader from werkzeug import Request as RequestBase, Response as ResponseBase, \ - LocalStack, LocalProxy, create_environ, cached_property + LocalStack, LocalProxy, create_environ, cached_property, \ + SharedDataMiddleware from werkzeug.routing import Map, Rule from werkzeug.exceptions import HTTPException, InternalServerError from werkzeug.contrib.securecookie import SecureCookie @@ -241,6 +242,9 @@ class Flask(object): if self.static_path is not None: self.url_map.add(Rule(self.static_path + '/', build_only=True, endpoint='static')) + self.wsgi_app = SharedDataMiddleware(self.wsgi_app, { + self.static_path: (self.package_name, 'static') + }) #: the Jinja2 environment. It is created from the #: :attr:`jinja_options` and the loader that is returned @@ -286,10 +290,6 @@ class Flask(object): from werkzeug import run_simple if 'debug' in options: self.debug = options.pop('debug') - if self.static_path is not None: - options['static_files'] = { - self.static_path: (self.package_name, 'static') - } options.setdefault('use_reloader', self.debug) options.setdefault('use_debugger', self.debug) return run_simple(host, port, self, **options) diff --git a/tests/flask_tests.py b/tests/flask_tests.py index 515a25c3..dec05bea 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -143,6 +143,15 @@ class BasicFunctionality(unittest.TestCase): with app.test_request_context(): assert flask.url_for('hello', name='test x') == '/hello/test%20x' + def test_static_files(self): + app = flask.Flask(__name__) + rv = app.test_client().get('/static/index.html') + assert rv.status_code == 200 + assert rv.data.strip() == '

Hello World!

' + with app.test_request_context(): + assert flask.url_for('static', filename='index.html') \ + == '/static/index.html' + class Templating(unittest.TestCase): diff --git a/tests/static/index.html b/tests/static/index.html new file mode 100644 index 00000000..de8b69b6 --- /dev/null +++ b/tests/static/index.html @@ -0,0 +1 @@ +

Hello World!