Skip to content
Myr

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 file

Take it

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 1–11why, one step back
1# -*- 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"""11

Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00

lines 12–13why, one step back
12import pytest
13

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 14–19why, one step back
14import flask
15import unittest16try:17 from greenlet import greenlet18except ImportError:19 greenlet = None

Remove flask superclassMarkus Unterwaditzerbefore adoptionaf4cb0f

line 20why, one step back
20

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 21–22why, one step back
21
22

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 23–28why, one step back
23def 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 back
29

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 30–34why, one step back
30 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 back
35

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 36–41why, one step back
36def 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 back
42

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 43–46why, one step back
43 try:
44 raise Exception('dummy')45 except Exception:46 pass

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 47why, one step back
47

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 48–50why, one step back
48 with app.test_request_context():
49 assert buffer == []50 assert buffer == [None]

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 51why, one step back
51

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 52–56why, one step back
52def 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 back
57

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 58–60why, one step back
58 @app.route('/')
59 def index():60 return None

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 61why, one step back
61

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 62–64why, one step back
62 @app.route('/', subdomain='foo')
63 def sub():64 return None

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 65why, one step back
65

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 66–68why, one step back
66 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 back
69

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 70–72why, one step back
70 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 back
73

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 74–75why, one step back
74 try:
75 with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):

Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00

line 76why, one step back
76 pass

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 77–82why, one step back
77 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 back
83

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 84–86why, one step back
84 app.config.update(SERVER_NAME='localhost')
85 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost'}):86 pass

Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00

line 87why, one step back
87

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 88–90why, one step back
88 app.config.update(SERVER_NAME='localhost:80')
89 with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):90 pass

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 91why, one step back
91

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 92–99why, one step back
92def 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.url

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 100why, one step back
100

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

line 101why, one step back
101 with app.test_request_context('/?name=World'):

Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00

line 102why, one step back
102 assert index() == 'Hello World!'

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 103–116why, one step back
103 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 back
117 ctx.pop()
118

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 119–123why, one step back
119def 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 back
124

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 125–134why, one step back
125 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 back
135

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 136–139why, one step back
136@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 back
140

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 141–147why, one step back
141 @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 back
148 assert flask.request
149 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 back
152 assert not flask.request
153 return 42154 greenlets.append(greenlet(g))155 return 'Hello World!'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 156why, one step back
156

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 157–158why, one step back
157 rv = app.test_client().get('/?foo=bar')
158 assert rv.data == b'Hello World!'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 159why, one step back
159

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 160–161why, one step back
160 result = greenlets[0].run()
161 assert result == 42

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 162why, one step back
162

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 163–166why, one step back
163@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 back
167

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 168–179why, one step back
168 @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 back
180

Remove useless classesMarkus Unterwaditzerbefore adoptionaf41dbe

lines 181–185why, one step back
181 rv = app.test_client().get('/?foo=bar')
182 assert rv.data == b'Hello World!'183184 result = greenlets[0].run()185 assert result == 42