Permalink
Browse files

Added more Flask tests

  • Loading branch information...
1 parent 8009bd9 commit 08f1f0dc32004fae01cf96d1eed84e6461e698e5 @mitsuhiko mitsuhiko committed Apr 14, 2010
Showing with 197 additions and 82 deletions.
  1. +11 −0 Makefile
  2. +0 −82 flask_tests.py
  3. +179 −0 tests/flask_tests.py
  4. +1 −0 tests/templates/context_template.html
  5. +6 −0 tests/templates/escaping_template.html
View
11 Makefile
@@ -0,0 +1,11 @@
+.PHONY: clean-pyc test
+
+all: clean-pyc test
+
+test:
+ python tests/flask_tests.py
+
+clean-pyc:
+ find . -name '*.pyc' -exec rm -f {} +
+ find . -name '*.pyo' -exec rm -f {} +
+ find . -name '*~' -exec rm -f {} +
View
82 flask_tests.py
@@ -1,82 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
- Flask Tests
- ~~~~~~~~~~~
-
- Tests Flask itself. The majority of Flask is already tested
- as part of Werkzeug.
-
- :copyright: (c) 2010 by Armin Ronacher.
- :license: BSD, see LICENSE for more details.
-"""
-from __future__ import with_statement
-import flask
-import unittest
-import tempfile
-
-
-class ContextTestCase(unittest.TestCase):
-
- def test_context_binding(self):
- app = flask.Flask(__name__)
- @app.route('/')
- def index():
- return 'Hello %s!' % flask.request.args['name']
- @app.route('/meh')
- def meh():
- return flask.request.url
-
- with app.test_request_context('/?name=World'):
- assert index() == 'Hello World!'
- with app.test_request_context('/meh'):
- assert meh() == 'http://localhost/meh'
-
- def test_request_dispatching(self):
- app = flask.Flask(__name__)
- @app.route('/')
- def index():
- return flask.request.method
- @app.route('/more', methods=['GET', 'POST'])
- def more():
- return flask.request.method
-
- c = app.test_client()
- assert c.get('/').data == 'GET'
- rv = c.post('/')
- assert rv.status_code == 405
- assert sorted(rv.allow) == ['GET', 'HEAD']
- rv = c.head('/')
- assert rv.status_code == 200
- assert not rv.data # head truncates
- assert c.post('/more').data == 'POST'
- assert c.get('/more').data == 'GET'
- rv = c.delete('/more')
- assert rv.status_code == 405
- assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']
-
- def test_session(self):
- app = flask.Flask(__name__)
- app.secret_key = 'testkey'
- @app.route('/set', methods=['POST'])
- def set():
- flask.session['value'] = flask.request.form['value']
- return 'value set'
- @app.route('/get')
- def get():
- return flask.session['value']
-
- c = app.test_client()
- assert c.post('/set', data={'value': '42'}).data == 'value set'
- assert c.get('/get').data == '42'
-
- def test_url_generation(self):
- app = flask.Flask(__name__)
- @app.route('/hello/<name>', methods=['POST'])
- def hello():
- pass
- with app.test_request_context():
- assert flask.url_for('hello', name='test x') == '/hello/test%20x'
-
-
-if __name__ == '__main__':
- unittest.main()
View
179 tests/flask_tests.py
@@ -0,0 +1,179 @@
+# -*- coding: utf-8 -*-
+"""
+ Flask Tests
+ ~~~~~~~~~~~
+
+ Tests Flask itself. The majority of Flask is already tested
+ as part of Werkzeug.
+
+ :copyright: (c) 2010 by Armin Ronacher.
+ :license: BSD, see LICENSE for more details.
+"""
+from __future__ import with_statement
+import flask
+import unittest
+import tempfile
+
+
+class ContextTestCase(unittest.TestCase):
+
+ def test_context_binding(self):
+ app = flask.Flask(__name__)
+ @app.route('/')
+ def index():
+ return 'Hello %s!' % flask.request.args['name']
+ @app.route('/meh')
+ def meh():
+ return flask.request.url
+
+ with app.test_request_context('/?name=World'):
+ assert index() == 'Hello World!'
+ with app.test_request_context('/meh'):
+ assert meh() == 'http://localhost/meh'
+
+
+class BasicFunctionality(unittest.TestCase):
+
+ def test_request_dispatching(self):
+ app = flask.Flask(__name__)
+ @app.route('/')
+ def index():
+ return flask.request.method
+ @app.route('/more', methods=['GET', 'POST'])
+ def more():
+ return flask.request.method
+
+ c = app.test_client()
+ assert c.get('/').data == 'GET'
+ rv = c.post('/')
+ assert rv.status_code == 405
+ assert sorted(rv.allow) == ['GET', 'HEAD']
+ rv = c.head('/')
+ assert rv.status_code == 200
+ assert not rv.data # head truncates
+ assert c.post('/more').data == 'POST'
+ assert c.get('/more').data == 'GET'
+ rv = c.delete('/more')
+ assert rv.status_code == 405
+ assert sorted(rv.allow) == ['GET', 'HEAD', 'POST']
+
+ def test_session(self):
+ app = flask.Flask(__name__)
+ app.secret_key = 'testkey'
+ @app.route('/set', methods=['POST'])
+ def set():
+ flask.session['value'] = flask.request.form['value']
+ return 'value set'
+ @app.route('/get')
+ def get():
+ return flask.session['value']
+
+ c = app.test_client()
+ assert c.post('/set', data={'value': '42'}).data == 'value set'
+ assert c.get('/get').data == '42'
+
+ def test_request_processing(self):
+ app = flask.Flask(__name__)
+ evts = []
+ @app.request_init
+ def before_request():
+ evts.append('before')
+ @app.request_shutdown
+ def after_request(response):
+ response.data += '|after'
+ evts.append('after')
+ return response
+ @app.route('/')
+ def index():
+ assert 'before' in evts
+ assert 'after' not in evts
+ return 'request'
+ assert 'after' not in evts
+ rv = app.test_client().get('/').data
+ assert 'after' in evts
+ assert rv == 'request|after'
+
+ def test_error_handling(self):
+ app = flask.Flask(__name__)
+ @app.errorhandler(404)
+ def not_found(e):
+ return 'not found', 404
+ @app.errorhandler(500)
+ def internal_server_error(e):
+ return 'internal server error', 500
+ @app.route('/')
+ def index():
+ flask.abort(404)
+ @app.route('/error')
+ def error():
+ 1/0
+ c = app.test_client()
+ rv = c.get('/')
+ assert rv.status_code == 404
+ assert rv.data == 'not found'
+ rv = c.get('/error')
+ assert rv.status_code == 500
+ assert 'internal server error' in rv.data
+
+ def test_response_creation(self):
+ app = flask.Flask(__name__)
+ @app.route('/unicode')
+ def from_unicode():
+ return u'Hällo Wörld'
+ @app.route('/string')
+ def from_string():
+ return u'Hällo Wörld'.encode('utf-8')
+ @app.route('/args')
+ def from_tuple():
+ return 'Meh', 400, {'X-Foo': 'Testing'}, 'text/plain'
+ c = app.test_client()
+ assert c.get('/unicode').data == u'Hällo Wörld'.encode('utf-8')
+ assert c.get('/string').data == u'Hällo Wörld'.encode('utf-8')
+ rv = c.get('/args')
+ assert rv.data == 'Meh'
+ assert rv.headers['X-Foo'] == 'Testing'
+ assert rv.status_code == 400
+ assert rv.mimetype == 'text/plain'
+
+ def test_url_generation(self):
+ app = flask.Flask(__name__)
+ @app.route('/hello/<name>', methods=['POST'])
+ def hello():
+ pass
+ with app.test_request_context():
+ assert flask.url_for('hello', name='test x') == '/hello/test%20x'
+
+
+class Templating(unittest.TestCase):
+
+ def test_context_processing(self):
+ app = flask.Flask(__name__)
+ @app.context_processor
+ def context_processor():
+ return {'injected_value': 42}
+ @app.route('/')
+ def index():
+ return flask.render_template('context_template.html', value=23)
+ rv = app.test_client().get('/')
+ assert rv.data == '<p>23|42'
+
+ def test_escaping(self):
+ text = '<p>Hello World!'
+ app = flask.Flask(__name__)
+ @app.route('/')
+ def index():
+ return flask.render_template('escaping_template.html', text=text,
+ html=flask.Markup(text))
+ lines = app.test_client().get('/').data.splitlines()
+ assert lines == [
+ '&lt;p&gt;Hello World!',
+ '<p>Hello World!',
+ '<p>Hello World!',
+ '<p>Hello World!',
+ '&lt;p&gt;Hello World!',
+ '<p>Hello World!'
+ ]
+
+
+if __name__ == '__main__':
+ unittest.main()
View
1 tests/templates/context_template.html
@@ -0,0 +1 @@
+<p>{{ value }}|{{ injected_value }}
View
6 tests/templates/escaping_template.html
@@ -0,0 +1,6 @@
+{{ text }}
+{{ html }}
+{% autoescape false %}{{ text }}
+{{ html }}{% endautoescape %}
+{% autoescape true %}{{ text }}
+{{ html }}{% endautoescape %}

0 comments on commit 08f1f0d

Please sign in to comment.