Browse Source

Fix for Flask's ticket 126. A proper environment is now built to use

with `test_request_context()`.

Signed-off-by: Armin Ronacher <armin.ronacher@active-4.com>
pull/132/head
Pedro Algarvio 14 years ago committed by Armin Ronacher
parent
commit
88883aa6db
  1. 9
      flask/app.py
  2. 17
      tests/flask_tests.py

9
flask/app.py

@ -863,6 +863,15 @@ class Flask(_PackageBoundObject):
function accepts the same arguments).
"""
from werkzeug import create_environ
environ_overrides = kwargs.setdefault('environ_overrides', {})
if self.config.get('SERVER_NAME'):
server_name = self.config.get('SERVER_NAME')
if ':' not in server_name:
server_name += ':80'
http_host, http_port = server_name.split(':')
environ_overrides.setdefault('SERVER_NAME', server_name)
environ_overrides.setdefault('HTTP_HOST', server_name)
environ_overrides.setdefault('SERVER_PORT', http_port)
return self.request_context(create_environ(*args, **kwargs))
def wsgi_app(self, environ, start_response):

17
tests/flask_tests.py

@ -501,6 +501,23 @@ class BasicFunctionalityTestCase(unittest.TestCase):
self.assertEqual(repr(flask.g), '<LocalProxy unbound>')
self.assertFalse(flask.g)
def test_proper_test_request_context(self):
app = flask.Flask(__name__)
app.config.update(
SERVER_NAME='localhost.localdomain:5000'
)
@app.route('/')
def index():
return None
with app.test_request_context('/'):
assert flask.url_for('index', _external=True) == 'http://localhost.localdomain:5000/'
def testit():
with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):
pass
self.assertRaises(ValueError, testit)
class JSONTestCase(unittest.TestCase):

Loading…
Cancel
Save