test_testing.py
This file
Path
tests/test_testing.py
297 lines · 7.6 KiB · Python
Last changed by
Merge branch 'master' into json-object-hookDavid Lordbefore adoptionea2e960
History of this fileTake it
The walk stopped after 200 commits: lines older than 7df543d point at it.
Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 1–13why, one step back1# -*- coding: utf-8 -*-2"""3 tests.testing4 ~~~~~~~~~~~~~56 Test client and more.78 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import pytest1213import flaskDefault environ (#2047)Michael Recachinasbefore adoptionbd5e297
line 14why, one step back14import werkzeugMerge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 15–18why, one step back1516from flask._compat import text_type1718Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 19why, one step back19def test_environ_defaults_from_config(app, client):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 20–21why, one step back20 app.config['SERVER_NAME'] = 'example.com:1234'21 app.config['APPLICATION_ROOT'] = '/foo'Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 22why, one step back22Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 23–29why, one step back23 @app.route('/')24 def index():25 return flask.request.url2627 ctx = app.test_request_context()28 assert ctx.request.url == 'http://example.com:1234/foo/'29Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 30–34why, one step back30 rv = client.get('/')31 assert rv.data == b'http://example.com:1234/foo/'323334def test_environ_defaults(app, client, app_ctx, req_ctx):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 35–40why, one step back35 @app.route('/')36 def index():37 return flask.request.url3839 ctx = app.test_request_context()40 assert ctx.request.url == 'http://localhost/'Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 41–42why, one step back41 with client:42 rv = client.get('/')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 43why, one step back43 assert rv.data == b'http://localhost/'Default environ (#2047)Michael Recachinasbefore adoptionbd5e297
line 44why, one step back44Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 45–46why, one step back4546def test_environ_base_default(app, client, app_ctx):Default environ (#2047)Michael Recachinasbefore adoptionbd5e297
lines 47–51why, one step back47 @app.route('/')48 def index():49 flask.g.user_agent = flask.request.headers["User-Agent"]50 return flask.request.remote_addr51Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 52–54why, one step back52 rv = client.get('/')53 assert rv.data == b'127.0.0.1'54 assert flask.g.user_agent == 'werkzeug/' + werkzeug.__version__Default environ (#2047)Michael Recachinasbefore adoptionbd5e297
line 55why, one step back55Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 56–57why, one step back5657def test_environ_base_modified(app, client, app_ctx):Default environ (#2047)Michael Recachinasbefore adoptionbd5e297
lines 58–62why, one step back58 @app.route('/')59 def index():60 flask.g.user_agent = flask.request.headers["User-Agent"]61 return flask.request.remote_addr62Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 63–67why, one step back63 client.environ_base['REMOTE_ADDR'] = '0.0.0.0'64 client.environ_base['HTTP_USER_AGENT'] = 'Foo'65 rv = client.get('/')66 assert rv.data == b'0.0.0.0'67 assert flask.g.user_agent == 'Foo'Default environ (#2047)Michael Recachinasbefore adoptionbd5e297
line 68why, one step back68Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 69–73why, one step back69 client.environ_base['REMOTE_ADDR'] = '0.0.0.1'70 client.environ_base['HTTP_USER_AGENT'] = 'Bar'71 rv = client.get('/')72 assert rv.data == b'0.0.0.1'73 assert flask.g.user_agent == 'Bar'Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 74why, one step back74Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 75why, one step back75Merge branch 'master' into json-object-hookDavid Lordbefore adoptionea2e960
lines 76–107why, one step back76def test_specify_url_scheme(app, client):77 @app.route('/')78 def index():79 return flask.request.url8081 ctx = app.test_request_context(url_scheme='https')82 assert ctx.request.url == 'https://localhost/'8384 rv = client.get('/', url_scheme='https')85 assert rv.data == b'https://localhost/'868788def test_blueprint_with_subdomain(app, client):89 app.config['SERVER_NAME'] = 'example.com:1234'90 app.config['APPLICATION_ROOT'] = '/foo'9192 bp = flask.Blueprint('company', __name__, subdomain='xxx')9394 @bp.route('/')95 def index():96 return flask.request.url9798 app.register_blueprint(bp)99100 ctx = app.test_request_context('/', subdomain='xxx')101 assert ctx.request.url == 'http://xxx.example.com:1234/foo/'102 assert ctx.request.blueprint == bp.name103104 rv = client.get('/', subdomain='xxx')105 assert rv.data == b'http://xxx.example.com:1234/foo/'106107Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 108why, one step back108def test_redirect_keep_session(app, client, app_ctx):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 109–121why, one step back109 app.secret_key = 'testing'110111 @app.route('/', methods=['GET', 'POST'])112 def index():113 if flask.request.method == 'POST':114 return flask.redirect('/getsession')115 flask.session['data'] = 'foo'116 return 'index'117118 @app.route('/getsession')119 def get_session():120 return flask.session.get('data', '<missing>')121Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 122–123why, one step back122 with client:123 rv = client.get('/getsession')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 124–125why, one step back124 assert rv.data == b'<missing>'125Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 126why, one step back126 rv = client.get('/')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 127–128why, one step back127 assert rv.data == b'index'128 assert flask.session.get('data') == 'foo'Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 129why, one step back129 rv = client.post('/', data={}, follow_redirects=True)Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 130–132why, one step back130 assert rv.data == b'foo'131132 # This support requires a new Werkzeug versionMerge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 133why, one step back133 if not hasattr(client, 'redirect_client'):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 134–135why, one step back134 assert flask.session.get('data') == 'foo'135Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 136why, one step back136 rv = client.get('/getsession')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 137–138why, one step back137 assert rv.data == b'foo'138Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 139–140why, one step back139140def test_session_transactions(app, client):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 141–146why, one step back141 app.secret_key = 'testing'142143 @app.route('/')144 def index():145 return text_type(flask.session['foo'])146Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 147–148why, one step back147 with client:148 with client.session_transaction() as sess:Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 149–151why, one step back149 assert len(sess) == 0150 sess['foo'] = [42]151 assert len(sess) == 1Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 152why, one step back152 rv = client.get('/')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 153why, one step back153 assert rv.data == b'[42]'Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 154why, one step back154 with client.session_transaction() as sess:Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 155–157why, one step back155 assert len(sess) == 1156 assert sess['foo'] == [42]157Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 158why, one step back158Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 159–163why, one step back159def test_session_transactions_no_null_sessions():160 app = flask.Flask(__name__)161 app.testing = True162163 with app.test_client() as c:Merge pull request #1743 from homoSapiensSapiens/use_pytest_raisesMarkus Unterwaditzerbefore adoptiond45445e
line 164why, one step back164 with pytest.raises(RuntimeError) as e:Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 165–166why, one step back165 with c.session_transaction() as sess:166 passMerge pull request #1743 from homoSapiensSapiens/use_pytest_raisesMarkus Unterwaditzerbefore adoptiond45445e
line 167why, one step back167 assert 'Session backend did not open a session' in str(e.value)Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 168why, one step back168Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 169–170why, one step back169170def test_session_transactions_keep_context(app, client, req_ctx):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 171–172why, one step back171 app.secret_key = 'testing'172Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 173–177why, one step back173 rv = client.get('/')174 req = flask.request._get_current_object()175 assert req is not None176 with client.session_transaction():177 assert req is flask.request._get_current_object()Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 178why, one step back178Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 179–180why, one step back179180def test_session_transaction_needs_cookies(app):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 181why, one step back181 c = app.test_client(use_cookies=False)Merge pull request #1743 from homoSapiensSapiens/use_pytest_raisesMarkus Unterwaditzerbefore adoptiond45445e
line 182why, one step back182 with pytest.raises(RuntimeError) as e:Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 183–184why, one step back183 with c.session_transaction() as s:184 passMerge pull request #1743 from homoSapiensSapiens/use_pytest_raisesMarkus Unterwaditzerbefore adoptiond45445e
line 185why, one step back185 assert 'cookies' in str(e.value)Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 186why, one step back186Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 187–188why, one step back187188def test_test_client_context_binding(app, client):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 189why, one step back189 app.config['LOGGER_HANDLER_POLICY'] = 'never'Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 190–191why, one step back190 app.testing = False191Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 192–200why, one step back192 @app.route('/')193 def index():194 flask.g.value = 42195 return 'Hello World!'196197 @app.route('/other')198 def other():199 1 // 0200Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 201–202why, one step back201 with client:202 resp = client.get('/')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 203–206why, one step back203 assert flask.g.value == 42204 assert resp.data == b'Hello World!'205 assert resp.status_code == 200206Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 207why, one step back207 resp = client.get('/other')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 208–220why, one step back208 assert not hasattr(flask.g, 'value')209 assert b'Internal Server Error' in resp.data210 assert resp.status_code == 500211 flask.g.value = 23212213 try:214 flask.g.value215 except (AttributeError, RuntimeError):216 pass217 else:218 raise AssertionError('some kind of exception expected')219220Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 221–223why, one step back221def test_reuse_client(client):222 c = client223Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 224why, one step back224 with c:Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 225why, one step back225 assert client.get('/').status_code == 404Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 226–227why, one step back226227 with c:Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 228why, one step back228 assert client.get('/').status_code == 404Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 229why, one step back229Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 230–231why, one step back230231def test_test_client_calls_teardown_handlers(app, client):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 232why, one step back232 called = []Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 233why, one step back233Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 234–237why, one step back234 @app.teardown_request235 def remember(error):236 called.append(error)237Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 238why, one step back238 with client:Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 239why, one step back239 assert called == []Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 240why, one step back240 client.get('/')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 241–244why, one step back241 assert called == []242 assert called == [None]243244 del called[:]Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 245why, one step back245 with client:Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 246why, one step back246 assert called == []Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 247why, one step back247 client.get('/')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 248why, one step back248 assert called == []Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 249why, one step back249 client.get('/')Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 250–253why, one step back250 assert called == [None]251 assert called == [None, None]252253Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 254why, one step back254def test_full_url_request(app, client):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 255–258why, one step back255 @app.route('/action', methods=['POST'])256 def action():257 return 'x'258Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 259–260why, one step back259 with client:260 rv = client.post('http://domain.com/action?vodka=42', data={'gin': 43})Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 261–264why, one step back261 assert rv.status_code == 200262 assert 'gin' in flask.request.form263 assert 'vodka' in flask.request.args264Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 265–266why, one step back265266def test_subdomain(app, client):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 267why, one step back267 app.config['SERVER_NAME'] = 'example.com'Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 268why, one step back268Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 269–275why, one step back269 @app.route('/', subdomain='<company_id>')270 def view(company_id):271 return company_id272273 with app.test_request_context():274 url = flask.url_for('view', company_id='xxx')275Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 276–277why, one step back276 with client:277 response = client.get(url)Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 278–281why, one step back278279 assert 200 == response.status_code280 assert b'xxx' == response.data281Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 282–283why, one step back282283def test_nosubdomain(app, client):Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
line 284why, one step back284 app.config['SERVER_NAME'] = 'example.com'Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 285why, one step back285Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 286–292why, one step back286 @app.route('/<company_id>')287 def view(company_id):288 return company_id289290 with app.test_request_context():291 url = flask.url_for('view', company_id='xxx')292Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 293–294why, one step back293 with client:294 response = client.get(url)Merge pull request #1703 from jeffwidman/masterDavid Lordbefore adoption7df543d
lines 295–297why, one step back295296 assert 200 == response.status_code297 assert b'xxx' == response.data