Better handling for test_request_context don't just append the port.

Also implemented a proper initial environment to use with
`Flask.test_app()` based on the application's configuration.

Signed-off-by: Armin Ronacher <armin.ronacher@active-4.com>
This commit is contained in:
Pedro Algarvio 2010-10-22 16:13:35 +01:00 committed by Armin Ronacher
parent 1f6321a1cb
commit a327452540
3 changed files with 132 additions and 7 deletions

View file

@ -510,13 +510,114 @@ class BasicFunctionalityTestCase(unittest.TestCase):
def index():
return None
@app.route('/', subdomain='foo')
def sub():
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('/'):
assert flask.url_for('sub', _external=True) == 'http://foo.localhost.localdomain:5000/'
try:
with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):
pass
self.assertRaises(ValueError, testit)
except Exception, e:
assert isinstance(e, ValueError)
assert str(e) == "the server name provided " + \
"('localhost.localdomain:5000') does not match the " + \
"server name from the WSGI environment ('localhost')", str(e)
try:
app.config.update(SERVER_NAME='localhost')
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):
pass
except ValueError, e:
raise ValueError(
"No ValueError exception should have been raised \"%s\"" % e
)
try:
app.config.update(SERVER_NAME='localhost:80')
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):
pass
except ValueError, e:
raise ValueError(
"No ValueError exception should have been raised \"%s\"" % e
)
def test_test_app_proper_environ(self):
app = flask.Flask(__name__)
app.config.update(
SERVER_NAME='localhost.localdomain:5000'
)
@app.route('/')
def index():
return 'Foo'
@app.route('/', subdomain='foo')
def subdomain():
return 'Foo SubDomain'
try:
rv = app.test_client().get('/')
assert rv.data == 'Foo'
except ValueError, e:
raise ValueError(
"No ValueError exception should have been raised \"%s\"" % e
)
try:
rv = app.test_client().get('/', 'http://localhost.localdomain:5000')
assert rv.data == 'Foo'
except ValueError, e:
raise ValueError(
"No ValueError exception should have been raised \"%s\"" % e
)
try:
rv = app.test_client().get('/', 'https://localhost.localdomain:5000')
assert rv.data == 'Foo'
except ValueError, e:
raise ValueError(
"No ValueError exception should have been raised \"%s\"" % e
)
try:
app.config.update(SERVER_NAME='localhost.localdomain')
rv = app.test_client().get('/', 'https://localhost.localdomain')
assert rv.data == 'Foo'
except ValueError, e:
raise ValueError(
"No ValueError exception should have been raised \"%s\"" % e
)
try:
app.config.update(SERVER_NAME='localhost.localdomain:443')
rv = app.test_client().get('/', 'https://localhost.localdomain')
assert rv.data == 'Foo'
except ValueError, e:
assert str(e) == "the server name provided " + \
"('localhost.localdomain:443') does not match the " + \
"server name from the WSGI environment ('localhost.localdomain')", str(e)
try:
app.config.update(SERVER_NAME='localhost.localdomain')
app.test_client().get('/', 'http://foo.localhost')
except ValueError, e:
assert str(e) == "the server name provided " + \
"('localhost.localdomain') does not match the " + \
"server name from the WSGI environment ('foo.localhost')", str(e)
try:
rv = app.test_client().get('/', 'http://foo.localhost.localdomain')
assert rv.data == 'Foo SubDomain'
except ValueError, e:
raise ValueError(
"No ValueError exception should have been raised \"%s\"" % e
)
class JSONTestCase(unittest.TestCase):