Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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

authored by Pedro Algarvio · committed by Armin Ronacher · parent 1f6321a · Browse files · 3 files+132 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Armin Ronacher <armin.ronacher@active-4.com>

Diff stats

3 files+132 added−7 removed

  • tests:+103 added−2 removed
  • other:+29 added−5 removed

3 files+132 added−7 removed

modified:flask/app.py+5 added-3 removed
702 unmodified lines
703 if req.routing_exception is not None:704 raise req.routing_exception705 rule = req.url_rule706 # if we provide automatic options for this URL and the707 # request came with the OPTIONS method, reply automatically 707 # request came with the OPTIONS method, reply automatically708 if rule.provide_automatic_options and req.method == 'OPTIONS':709 return self.make_default_options_response()710 # otherwise dispatch to the handler for that endpoint711 return self.view_functions[rule.endpoint](**req.view_args)
154 unmodified lines
866 environ_overrides = kwargs.setdefault('environ_overrides', {})867 if self.config.get('SERVER_NAME'):868 server_name = self.config.get('SERVER_NAME')869 if ':' not in server_name:870 server_name += ':80'871 http_host, http_port = server_name.split(':')870 http_host, http_port = server_name, '80'871 else:872 http_host, http_port = server_name.split(':', 1)873874 environ_overrides.setdefault('SERVER_NAME', server_name)875 environ_overrides.setdefault('HTTP_HOST', server_name)876 environ_overrides.setdefault('SERVER_PORT', http_port)877 return self.request_context(create_environ(*args, **kwargs))
45 unmodified lines
modified:flask/testing.py+24 added-2 removed
8 unmodified lines
9 :copyright: (c) 2010 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""1213from werkzeug import Client13from werkzeug import Client, EnvironBuilder14from flask import _request_ctx_stack151617class FlaskClient(Client):
10 unmodified lines
28 _request_ctx_stack.pop()29 self.context_preserved = False30 kwargs.setdefault('environ_overrides', {}) \31 ['flask._preserve_context'] = self.preserve_context3233 as_tuple = kwargs.pop('as_tuple', False)34 buffered = kwargs.pop('buffered', False)35 follow_redirects = kwargs.pop('follow_redirects', False)3637 builder = EnvironBuilder(*args, **kwargs)3839 if self.application.config.get('SERVER_NAME'):40 server_name = self.application.config.get('SERVER_NAME')41 if ':' not in server_name:42 http_host, http_port = server_name, None43 else:44 http_host, http_port = server_name.split(':', 1)45 if builder.base_url == 'http://localhost/':46 # Default Generated Base URL47 if http_port != None:48 builder.host = http_host + ':' + http_port49 else:50 builder.host = http_host51 old = _request_ctx_stack.top52 try:34 return Client.open(self, *args, **kwargs)53 return Client.open(self, builder,54 as_tuple=as_tuple,55 buffered=buffered,56 follow_redirects=follow_redirects)57 finally:58 self.context_preserved = _request_ctx_stack.top is not old5960 def __enter__(self):
7 unmodified lines
modified:tests/flask_tests.py+103 added-2 removed
508 unmodified lines
509 @app.route('/')510 def index():511 return None512513 @app.route('/', subdomain='foo')514 def sub():515 return None516517 with app.test_request_context('/'):518 assert flask.url_for('index', _external=True) == 'http://localhost.localdomain:5000/'519516 def testit():520 with app.test_request_context('/'):521 assert flask.url_for('sub', _external=True) == 'http://foo.localhost.localdomain:5000/'522523 try:524 with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):525 pass519 self.assertRaises(ValueError, testit)526 except Exception, e:527 assert isinstance(e, ValueError)528 assert str(e) == "the server name provided " + \529 "('localhost.localdomain:5000') does not match the " + \530 "server name from the WSGI environment ('localhost')", str(e)531532 try:533 app.config.update(SERVER_NAME='localhost')534 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):535 pass536 except ValueError, e:537 raise ValueError(538 "No ValueError exception should have been raised \"%s\"" % e539 )540541 try:542 app.config.update(SERVER_NAME='localhost:80')543 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):544 pass545 except ValueError, e:546 raise ValueError(547 "No ValueError exception should have been raised \"%s\"" % e548 )549550551 def test_test_app_proper_environ(self):552 app = flask.Flask(__name__)553 app.config.update(554 SERVER_NAME='localhost.localdomain:5000'555 )556 @app.route('/')557 def index():558 return 'Foo'559560 @app.route('/', subdomain='foo')561 def subdomain():562 return 'Foo SubDomain'563564 try:565 rv = app.test_client().get('/')566 assert rv.data == 'Foo'567 except ValueError, e:568 raise ValueError(569 "No ValueError exception should have been raised \"%s\"" % e570 )571572 try:573 rv = app.test_client().get('/', 'http://localhost.localdomain:5000')574 assert rv.data == 'Foo'575 except ValueError, e:576 raise ValueError(577 "No ValueError exception should have been raised \"%s\"" % e578 )579580 try:581 rv = app.test_client().get('/', 'https://localhost.localdomain:5000')582 assert rv.data == 'Foo'583 except ValueError, e:584 raise ValueError(585 "No ValueError exception should have been raised \"%s\"" % e586 )587588 try:589 app.config.update(SERVER_NAME='localhost.localdomain')590 rv = app.test_client().get('/', 'https://localhost.localdomain')591 assert rv.data == 'Foo'592 except ValueError, e:593 raise ValueError(594 "No ValueError exception should have been raised \"%s\"" % e595 )596597 try:598 app.config.update(SERVER_NAME='localhost.localdomain:443')599 rv = app.test_client().get('/', 'https://localhost.localdomain')600 assert rv.data == 'Foo'601 except ValueError, e:602 assert str(e) == "the server name provided " + \603 "('localhost.localdomain:443') does not match the " + \604 "server name from the WSGI environment ('localhost.localdomain')", str(e)605606 try:607 app.config.update(SERVER_NAME='localhost.localdomain')608 app.test_client().get('/', 'http://foo.localhost')609 except ValueError, e:610 assert str(e) == "the server name provided " + \611 "('localhost.localdomain') does not match the " + \612 "server name from the WSGI environment ('foo.localhost')", str(e)613614 try:615 rv = app.test_client().get('/', 'http://foo.localhost.localdomain')616 assert rv.data == 'Foo SubDomain'617 except ValueError, e:618 raise ValueError(619 "No ValueError exception should have been raised \"%s\"" % e620 )621622623class JSONTestCase(unittest.TestCase):624625 def test_jsonify(self):626 d = dict(a=23, b=42, c=[1, 2, 3])
744 unmodified lines