test_reqctx.py
This file
Path
tests/test_reqctx.py
195 lines · 5.1 KiB · Python
Last changed by
Merge pull request #1743 from homoSapiensSapiens/use_pytest_raisesMarkus Unterwaditzerbefore adoptiond45445e
History of this fileTake it
The walk stopped after 200 commits: lines older than 86e8267 point at it.
Merge pull request #1378 from cbron/masterKeyan Pishdadianbefore adoption86e8267
lines 1–142why, one step back1# -*- coding: utf-8 -*-2"""3 tests.reqctx4 ~~~~~~~~~~~~56 Tests the request context.78 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import pytest1314import flask1516try:17 from greenlet import greenlet18except ImportError:19 greenlet = None202122def test_teardown_on_pop():23 buffer = []24 app = flask.Flask(__name__)25 @app.teardown_request26 def end_of_request(exception):27 buffer.append(exception)2829 ctx = app.test_request_context()30 ctx.push()31 assert buffer == []32 ctx.pop()33 assert buffer == [None]3435def test_teardown_with_previous_exception():36 buffer = []37 app = flask.Flask(__name__)38 @app.teardown_request39 def end_of_request(exception):40 buffer.append(exception)4142 try:43 raise Exception('dummy')44 except Exception:45 pass4647 with app.test_request_context():48 assert buffer == []49 assert buffer == [None]5051def test_teardown_with_handled_exception():52 buffer = []53 app = flask.Flask(__name__)54 @app.teardown_request55 def end_of_request(exception):56 buffer.append(exception)5758 with app.test_request_context():59 assert buffer == []60 try:61 raise Exception('dummy')62 except Exception:63 pass64 assert buffer == [None]6566def test_proper_test_request_context():67 app = flask.Flask(__name__)68 app.config.update(69 SERVER_NAME='localhost.localdomain:5000'70 )7172 @app.route('/')73 def index():74 return None7576 @app.route('/', subdomain='foo')77 def sub():78 return None7980 with app.test_request_context('/'):81 assert flask.url_for('index', _external=True) == \82 'http://localhost.localdomain:5000/'8384 with app.test_request_context('/'):85 assert flask.url_for('sub', _external=True) == \86 'http://foo.localhost.localdomain:5000/'8788 try:89 with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):90 pass91 except ValueError as e:92 assert str(e) == (93 "the server name provided "94 "('localhost.localdomain:5000') does not match the "95 "server name from the WSGI environment ('localhost')"96 )9798 app.config.update(SERVER_NAME='localhost')99 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):100 pass101102 app.config.update(SERVER_NAME='localhost:80')103 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):104 pass105106def test_context_binding():107 app = flask.Flask(__name__)108 @app.route('/')109 def index():110 return 'Hello %s!' % flask.request.args['name']111 @app.route('/meh')112 def meh():113 return flask.request.url114115 with app.test_request_context('/?name=World'):116 assert index() == 'Hello World!'117 with app.test_request_context('/meh'):118 assert meh() == 'http://localhost/meh'119 assert flask._request_ctx_stack.top is None120121def test_context_test():122 app = flask.Flask(__name__)123 assert not flask.request124 assert not flask.has_request_context()125 ctx = app.test_request_context()126 ctx.push()127 try:128 assert flask.request129 assert flask.has_request_context()130 finally:131 ctx.pop()132133def test_manual_context_binding():134 app = flask.Flask(__name__)135 @app.route('/')136 def index():137 return 'Hello %s!' % flask.request.args['name']138139 ctx = app.test_request_context('/?name=World')140 ctx.push()141 assert index() == 'Hello World!'142 ctx.pop()Merge pull request #1743 from homoSapiensSapiens/use_pytest_raisesMarkus Unterwaditzerbefore adoptiond45445e
line 143why, one step back143 with pytest.raises(RuntimeError):Merge pull request #1378 from cbron/masterKeyan Pishdadianbefore adoption86e8267
lines 144–195why, one step back144 index()145146@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')147def test_greenlet_context_copying():148 app = flask.Flask(__name__)149 greenlets = []150151 @app.route('/')152 def index():153 reqctx = flask._request_ctx_stack.top.copy()154 def g():155 assert not flask.request156 assert not flask.current_app157 with reqctx:158 assert flask.request159 assert flask.current_app == app160 assert flask.request.path == '/'161 assert flask.request.args['foo'] == 'bar'162 assert not flask.request163 return 42164 greenlets.append(greenlet(g))165 return 'Hello World!'166167 rv = app.test_client().get('/?foo=bar')168 assert rv.data == b'Hello World!'169170 result = greenlets[0].run()171 assert result == 42172173@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')174def test_greenlet_context_copying_api():175 app = flask.Flask(__name__)176 greenlets = []177178 @app.route('/')179 def index():180 reqctx = flask._request_ctx_stack.top.copy()181 @flask.copy_current_request_context182 def g():183 assert flask.request184 assert flask.current_app == app185 assert flask.request.path == '/'186 assert flask.request.args['foo'] == 'bar'187 return 42188 greenlets.append(greenlet(g))189 return 'Hello World!'190191 rv = app.test_client().get('/?foo=bar')192 assert rv.data == b'Hello World!'193194 result = greenlets[0].run()195 assert result == 42