Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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>
  • Loading branch information...
commit 88883aa6db3b27f132000a4cf2a99c1fbbe941c9 1 parent 5ecf01d
Pedro Algarvio authored committed
Showing with 26 additions and 0 deletions.
  1. +9 −0 flask/app.py
  2. +17 −0 tests/flask_tests.py
View
9 flask/app.py
@@ -863,6 +863,15 @@ def test_request_context(self, *args, **kwargs):
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):
View
17 tests/flask_tests.py
@@ -501,6 +501,23 @@ def test_request_locals(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.