Static files are active in the WSGI app now, not just the server.

This commit is contained in:
Armin Ronacher 2010-04-14 14:11:00 +02:00
parent 08f1f0dc32
commit ca520fb7e4
3 changed files with 15 additions and 5 deletions

View file

@ -16,7 +16,8 @@ from threading import local
from contextlib import contextmanager from contextlib import contextmanager
from jinja2 import Environment, PackageLoader from jinja2 import Environment, PackageLoader
from werkzeug import Request as RequestBase, Response as ResponseBase, \ 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.routing import Map, Rule
from werkzeug.exceptions import HTTPException, InternalServerError from werkzeug.exceptions import HTTPException, InternalServerError
from werkzeug.contrib.securecookie import SecureCookie from werkzeug.contrib.securecookie import SecureCookie
@ -241,6 +242,9 @@ class Flask(object):
if self.static_path is not None: if self.static_path is not None:
self.url_map.add(Rule(self.static_path + '/<filename>', self.url_map.add(Rule(self.static_path + '/<filename>',
build_only=True, endpoint='static')) 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 #: the Jinja2 environment. It is created from the
#: :attr:`jinja_options` and the loader that is returned #: :attr:`jinja_options` and the loader that is returned
@ -286,10 +290,6 @@ class Flask(object):
from werkzeug import run_simple from werkzeug import run_simple
if 'debug' in options: if 'debug' in options:
self.debug = options.pop('debug') 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_reloader', self.debug)
options.setdefault('use_debugger', self.debug) options.setdefault('use_debugger', self.debug)
return run_simple(host, port, self, **options) return run_simple(host, port, self, **options)

View file

@ -143,6 +143,15 @@ class BasicFunctionality(unittest.TestCase):
with app.test_request_context(): with app.test_request_context():
assert flask.url_for('hello', name='test x') == '/hello/test%20x' 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() == '<h1>Hello World!</h1>'
with app.test_request_context():
assert flask.url_for('static', filename='index.html') \
== '/static/index.html'
class Templating(unittest.TestCase): class Templating(unittest.TestCase):

1
tests/static/index.html Normal file
View file

@ -0,0 +1 @@
<h1>Hello World!</h1>