test_reqctx.py
This file
Path
tests/test_reqctx.py
185 lines · 4.8 KiB · Python
Last changed by
Remove flask superclassMarkus Unterwaditzerbefore adoptionaf4cb0f
History of this fileTake it
Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 1–11why, one step back1# -*- coding: utf-8 -*-2"""3 tests.reqctx4 ~~~~~~~~~~~~~~~~~~~~~~56 Tests the request context.78 :copyright: (c) 2014 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00
lines 12–13why, one step back12import pytest13Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 14–19why, one step back14import flask15import unittest16try:17 from greenlet import greenlet18except ImportError:19 greenlet = NoneRemove flask superclassMarkus Unterwaditzerbefore adoptionaf4cb0f
line 20why, one step back20Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 21–22why, one step back2122Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 23–28why, one step back23def test_teardown_on_pop():24 buffer = []25 app = flask.Flask(__name__)26 @app.teardown_request27 def end_of_request(exception):28 buffer.append(exception)Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 29why, one step back29Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 30–34why, one step back30 ctx = app.test_request_context()31 ctx.push()32 assert buffer == []33 ctx.pop()34 assert buffer == [None]Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 35why, one step back35Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 36–41why, one step back36def test_teardown_with_previous_exception():37 buffer = []38 app = flask.Flask(__name__)39 @app.teardown_request40 def end_of_request(exception):41 buffer.append(exception)Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 42why, one step back42Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 43–46why, one step back43 try:44 raise Exception('dummy')45 except Exception:46 passMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 47why, one step back47Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 48–50why, one step back48 with app.test_request_context():49 assert buffer == []50 assert buffer == [None]Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 51why, one step back51Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 52–56why, one step back52def test_proper_test_request_context():53 app = flask.Flask(__name__)54 app.config.update(55 SERVER_NAME='localhost.localdomain:5000'56 )Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 57why, one step back57Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 58–60why, one step back58 @app.route('/')59 def index():60 return NoneMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 61why, one step back61Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 62–64why, one step back62 @app.route('/', subdomain='foo')63 def sub():64 return NoneMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 65why, one step back65Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 66–68why, one step back66 with app.test_request_context('/'):67 assert flask.url_for('index', _external=True) == \68 'http://localhost.localdomain:5000/'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 69why, one step back69Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 70–72why, one step back70 with app.test_request_context('/'):71 assert flask.url_for('sub', _external=True) == \72 'http://foo.localhost.localdomain:5000/'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 73why, one step back73Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 74–75why, one step back74 try:75 with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00
line 76why, one step back76 passRemove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 77–82why, one step back77 except ValueError as e:78 assert str(e) == (79 "the server name provided "80 "('localhost.localdomain:5000') does not match the "81 "server name from the WSGI environment ('localhost')"82 )Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 83why, one step back83Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 84–86why, one step back84 app.config.update(SERVER_NAME='localhost')85 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):86 passRewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00
line 87why, one step back87Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 88–90why, one step back88 app.config.update(SERVER_NAME='localhost:80')89 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):90 passMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 91why, one step back91Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 92–99why, one step back92def test_context_binding():93 app = flask.Flask(__name__)94 @app.route('/')95 def index():96 return 'Hello %s!' % flask.request.args['name']97 @app.route('/meh')98 def meh():99 return flask.request.urlMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 100why, one step back100Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
line 101why, one step back101 with app.test_request_context('/?name=World'):Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00
line 102why, one step back102 assert index() == 'Hello World!'Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 103–116why, one step back103 with app.test_request_context('/meh'):104 assert meh() == 'http://localhost/meh'105 assert flask._request_ctx_stack.top is None106107def test_context_test():108 app = flask.Flask(__name__)109 assert not flask.request110 assert not flask.has_request_context()111 ctx = app.test_request_context()112 ctx.push()113 try:114 assert flask.request115 assert flask.has_request_context()116 finally:Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 117–118why, one step back117 ctx.pop()118Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 119–123why, one step back119def test_manual_context_binding():120 app = flask.Flask(__name__)121 @app.route('/')122 def index():123 return 'Hello %s!' % flask.request.args['name']Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 124why, one step back124Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 125–134why, one step back125 ctx = app.test_request_context('/?name=World')126 ctx.push()127 assert index() == 'Hello World!'128 ctx.pop()129 try:130 index()131 except RuntimeError:132 pass133 else:134 assert 0, 'expected runtime error'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 135why, one step back135Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 136–139why, one step back136@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')137def test_greenlet_context_copying():138 app = flask.Flask(__name__)139 greenlets = []Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 140why, one step back140Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 141–147why, one step back141 @app.route('/')142 def index():143 reqctx = flask._request_ctx_stack.top.copy()144 def g():145 assert not flask.request146 assert not flask.current_app147 with reqctx:Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00
lines 148–151why, one step back148 assert flask.request149 assert flask.current_app == app150 assert flask.request.path == '/'151 assert flask.request.args['foo'] == 'bar'Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 152–155why, one step back152 assert not flask.request153 return 42154 greenlets.append(greenlet(g))155 return 'Hello World!'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 156why, one step back156Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 157–158why, one step back157 rv = app.test_client().get('/?foo=bar')158 assert rv.data == b'Hello World!'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 159why, one step back159Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 160–161why, one step back160 result = greenlets[0].run()161 assert result == 42Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 162why, one step back162Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 163–166why, one step back163@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')164def test_greenlet_context_copying_api():165 app = flask.Flask(__name__)166 greenlets = []Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 167why, one step back167Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 168–179why, one step back168 @app.route('/')169 def index():170 reqctx = flask._request_ctx_stack.top.copy()171 @flask.copy_current_request_context172 def g():173 assert flask.request174 assert flask.current_app == app175 assert flask.request.path == '/'176 assert flask.request.args['foo'] == 'bar'177 return 42178 greenlets.append(greenlet(g))179 return 'Hello World!'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 180why, one step back180Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe
lines 181–185why, one step back181 rv = app.test_client().get('/?foo=bar')182 assert rv.data == b'Hello World!'183184 result = greenlets[0].run()185 assert result == 42