Added more Flask tests
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 8009bd9 · Browse files · 5 files·+197 added−82 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+197 added−82 removed
- tests:+186 added−0 removed
- other:+11 added−82 removed
Provenance
- Commit
08f1f0d- Tree
9bffd28- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
8009bd9
5 files changed+197 added−82 removed
added:tests/templates/context_template.html+1 added
1<p>{{ value }}|{{ injected_value }}added:tests/templates/escaping_template.html+6 added
123456{{ text }}{{ html }}{% autoescape false %}{{ text }}{{ html }}{% endautoescape %}{% autoescape true %}{{ text }}{{ html }}{% endautoescape %}added:tests/flask_tests.py+179 added
1+# -*- coding: utf-8 -*-2+"""3+ Flask Tests4+ ~~~~~~~~~~~5+6+ Tests Flask itself. The majority of Flask is already tested7+ as part of Werkzeug.8+9+ :copyright: (c) 2010 by Armin Ronacher.10+ :license: BSD, see LICENSE for more details.11+"""12+from __future__ import with_statement13+import flask14+import unittest15+import tempfile16+17+18+class ContextTestCase(unittest.TestCase):19+20+ def test_context_binding(self):21+ app = flask.Flask(__name__)22+ @app.route('/')23+ def index():24+ return 'Hello %s!' % flask.request.args['name']25+ @app.route('/meh')26+ def meh():27+ return flask.request.url28+29+ with app.test_request_context('/?name=World'):30+ assert index() == 'Hello World!'31+ with app.test_request_context('/meh'):32+ assert meh() == 'http://localhost/meh'33+34+35+class BasicFunctionality(unittest.TestCase):36+37+ def test_request_dispatching(self):38+ app = flask.Flask(__name__)39+ @app.route('/')40+ def index():41+ return flask.request.method42+ @app.route('/more', methods=['GET', 'POST'])43+ def more():44+ return flask.request.method45+46+ c = app.test_client()47+ assert c.get('/').data == 'GET'48+ rv = c.post('/')49+ assert rv.status_code == 40550+ assert sorted(rv.allow) == ['GET', 'HEAD']51+ rv = c.head('/')52+ assert rv.status_code == 20053+ assert not rv.data # head truncates54+ assert c.post('/more').data == 'POST'55+ assert c.get('/more').data == 'GET'56+ rv = c.delete('/more')57+ assert rv.status_code == 40558+ assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']59+60+ def test_session(self):61+ app = flask.Flask(__name__)62+ app.secret_key = 'testkey'63+ @app.route('/set', methods=['POST'])64+ def set():65+ flask.session['value'] = flask.request.form['value']66+ return 'value set'67+ @app.route('/get')68+ def get():69+ return flask.session['value']70+71+ c = app.test_client()72+ assert c.post('/set', data={'value': '42'}).data == 'value set'73+ assert c.get('/get').data == '42'74+75+ def test_request_processing(self):76+ app = flask.Flask(__name__)77+ evts = []78+ @app.request_init79+ def before_request():80+ evts.append('before')81+ @app.request_shutdown82+ def after_request(response):83+ response.data += '|after'84+ evts.append('after')85+ return response86+ @app.route('/')87+ def index():88+ assert 'before' in evts89+ assert 'after' not in evts90+ return 'request'91+ assert 'after' not in evts92+ rv = app.test_client().get('/').data93+ assert 'after' in evts94+ assert rv == 'request|after'95+96+ def test_error_handling(self):97+ app = flask.Flask(__name__)98+ @app.errorhandler(404)99+ def not_found(e):100+ return 'not found', 404101+ @app.errorhandler(500)102+ def internal_server_error(e):103+ return 'internal server error', 500104+ @app.route('/')105+ def index():106+ flask.abort(404)107+ @app.route('/error')108+ def error():109+ 1/0110+ c = app.test_client()111+ rv = c.get('/')112+ assert rv.status_code == 404113+ assert rv.data == 'not found'114+ rv = c.get('/error')115+ assert rv.status_code == 500116+ assert 'internal server error' in rv.data117+118+ def test_response_creation(self):119+ app = flask.Flask(__name__)120+ @app.route('/unicode')121+ def from_unicode():122+ return u'Hällo Wörld'123+ @app.route('/string')124+ def from_string():125+ return u'Hällo Wörld'.encode('utf-8')126+ @app.route('/args')127+ def from_tuple():128+ return 'Meh', 400, {'X-Foo': 'Testing'}, 'text/plain'129+ c = app.test_client()130+ assert c.get('/unicode').data == u'Hällo Wörld'.encode('utf-8')131+ assert c.get('/string').data == u'Hällo Wörld'.encode('utf-8')132+ rv = c.get('/args')133+ assert rv.data == 'Meh'134+ assert rv.headers['X-Foo'] == 'Testing'135+ assert rv.status_code == 400136+ assert rv.mimetype == 'text/plain'137+138+ def test_url_generation(self):139+ app = flask.Flask(__name__)140+ @app.route('/hello/<name>', methods=['POST'])141+ def hello():142+ pass143+ with app.test_request_context():144+ assert flask.url_for('hello', name='test x') == '/hello/test%20x'145+146+147+class Templating(unittest.TestCase):148+149+ def test_context_processing(self):150+ app = flask.Flask(__name__)151+ @app.context_processor152+ def context_processor():153+ return {'injected_value': 42}154+ @app.route('/')155+ def index():156+ return flask.render_template('context_template.html', value=23)157+ rv = app.test_client().get('/')158+ assert rv.data == '<p>23|42'159+160+ def test_escaping(self):161+ text = '<p>Hello World!'162+ app = flask.Flask(__name__)163+ @app.route('/')164+ def index():165+ return flask.render_template('escaping_template.html', text=text,166+ html=flask.Markup(text))167+ lines = app.test_client().get('/').data.splitlines()168+ assert lines == [169+ '<p>Hello World!',170+ '<p>Hello World!',171+ '<p>Hello World!',172+ '<p>Hello World!',173+ '<p>Hello World!',174+ '<p>Hello World!'175+ ]176+177+178+if __name__ == '__main__':179+ unittest.main()deleted:flask_tests.py-82 removed
1-# -*- coding: utf-8 -*-2-"""3- Flask Tests4- ~~~~~~~~~~~5-6- Tests Flask itself. The majority of Flask is already tested7- as part of Werkzeug.8-9- :copyright: (c) 2010 by Armin Ronacher.10- :license: BSD, see LICENSE for more details.11-"""12-from __future__ import with_statement13-import flask14-import unittest15-import tempfile16-17-18-class ContextTestCase(unittest.TestCase):19-20- def test_context_binding(self):21- app = flask.Flask(__name__)22- @app.route('/')23- def index():24- return 'Hello %s!' % flask.request.args['name']25- @app.route('/meh')26- def meh():27- return flask.request.url28-29- with app.test_request_context('/?name=World'):30- assert index() == 'Hello World!'31- with app.test_request_context('/meh'):32- assert meh() == 'http://localhost/meh'33-34- def test_request_dispatching(self):35- app = flask.Flask(__name__)36- @app.route('/')37- def index():38- return flask.request.method39- @app.route('/more', methods=['GET', 'POST'])40- def more():41- return flask.request.method42-43- c = app.test_client()44- assert c.get('/').data == 'GET'45- rv = c.post('/')46- assert rv.status_code == 40547- assert sorted(rv.allow) == ['GET', 'HEAD']48- rv = c.head('/')49- assert rv.status_code == 20050- assert not rv.data # head truncates51- assert c.post('/more').data == 'POST'52- assert c.get('/more').data == 'GET'53- rv = c.delete('/more')54- assert rv.status_code == 40555- assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']56-57- def test_session(self):58- app = flask.Flask(__name__)59- app.secret_key = 'testkey'60- @app.route('/set', methods=['POST'])61- def set():62- flask.session['value'] = flask.request.form['value']63- return 'value set'64- @app.route('/get')65- def get():66- return flask.session['value']67-68- c = app.test_client()69- assert c.post('/set', data={'value': '42'}).data == 'value set'70- assert c.get('/get').data == '42'71-72- def test_url_generation(self):73- app = flask.Flask(__name__)74- @app.route('/hello/<name>', methods=['POST'])75- def hello():76- pass77- with app.test_request_context():78- assert flask.url_for('hello', name='test x') == '/hello/test%20x'79-80-81-if __name__ == '__main__':82- unittest.main()added:Makefile+11 added
1+.PHONY: clean-pyc test2+3+all: clean-pyc test4+5+test:6+ python tests/flask_tests.py7+8+clean-pyc:9+ find . -name '*.pyc' -exec rm -f {} +10+ find . -name '*.pyo' -exec rm -f {} +11+ find . -name '*~' -exec rm -f {} +