Added more tests for the new stack behavior.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 307d1bc · Browse files · 3 files·+33 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+33 added−2 removed
- other:+33 added−2 removed
Provenance
- Commit
f8f2e2d- Tree
d14d959- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
307d1bc
3 files changed+33 added−2 removed
modified:flask/testsuite/appctx.py+23 added-1 removed
16 unmodified lines17 18 19 class AppContextTestCase(FlaskTestCase):20 21- def test_basic_support(self):21+ def test_basic_url_generation(self):22 app = flask.Flask(__name__)23 app.config['SERVER_NAME'] = 'localhost'24 app.config['PREFERRED_URL_SCHEME'] = 'https'25 4 unmodified lines30 with app.app_context():31 rv = flask.url_for('index')32 self.assert_equal(rv, 'https://localhost/')33 34+ def test_url_generation_requires_server_name(self):35+ app = flask.Flask(__name__)36+ with app.app_context():37+ with self.assert_raises(RuntimeError):38+ flask.url_for('index')39 40+ def test_url_generation_without_context_fails(self):41+ with self.assert_raises(RuntimeError):42+ flask.url_for('index')43+44+ def test_request_context_means_app_context(self):45+ app = flask.Flask(__name__)46+ with app.test_request_context():47+ self.assert_equal(flask.current_app._get_current_object(), app)48+ self.assert_equal(flask._app_ctx_stack.top, None)49+50+ def test_app_context_provides_current_app(self):51+ app = flask.Flask(__name__)52+ with app.app_context():53+ self.assert_equal(flask.current_app._get_current_object(), app)54+ self.assert_equal(flask._app_ctx_stack.top, None)55+56+57 def suite():58 suite = unittest.TestSuite()59 suite.addTest(unittest.makeSuite(AppContextTestCase))60 return suitemodified:flask/__init__.py+2 added-1 removed
21 unmodified lines22 from .config import Config23 from .helpers import url_for, jsonify, json_available, flash, \24 send_file, send_from_directory, get_flashed_messages, \25 get_template_attribute, make_response, safe_join26-from .globals import current_app, g, request, session, _request_ctx_stack26+from .globals import current_app, g, request, session, _request_ctx_stack, \27+ _app_ctx_stack28 from .ctx import has_request_context29 from .module import Module30 from .blueprints import Blueprint31 from .templating import render_template, render_template_string11 unmodified linesmodified:flask/helpers.py+8 added
198 unmodified lines199 :param _method: if provided this explicitly specifies an HTTP method.200 """201 appctx = _app_ctx_stack.top202 reqctx = _request_ctx_stack.top203+ if appctx is None:204+ raise RuntimeError('Attempted to generate a URL with the application '205+ 'context being pushed. This has to be executed ')206 207 # If request specific information is available we have some extra208 # features that support "relative" urls.209 if reqctx is not None:17 unmodified lines227 # Otherwise go with the url adapter from the appctx and make228 # the urls external by default.229 else:230 url_adapter = appctx.url_adapter231+ if url_adapter is None:232+ raise RuntimeError('Application was not able to create a URL '233+ 'adapter for request independent URL generation. '234+ 'You might be able to fix this by setting '235+ 'the SERVER_NAME config variable.')236 external = values.pop('_external', True)237 238 anchor = values.pop('_anchor', None)239 method = values.pop('_method', None)471 unmodified lines