forked from orbit-oss/flask
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>
This commit is contained in:
parent
5ecf01dd4e
commit
88883aa6db
2 changed files with 26 additions and 0 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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…
Add table
Add a link
Reference in a new issue