Skip to content
Myr

test_views.py

This file

Path

tests/test_views.py

162 lines · 4.1 KiB · Python

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f63cd72 point at it.

Merge pull request #1721 from pra85/patch-1David Lordbefore adoptionf63cd72

lines 1–162why, one step back
1# -*- coding: utf-8 -*-
2"""3 tests.views4 ~~~~~~~~~~~56 Pluggable views.78 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import pytest1314import flask15import flask.views1617from werkzeug.http import parse_set_header1819def common_test(app):20 c = app.test_client()2122 assert c.get('/').data == b'GET'23 assert c.post('/').data == b'POST'24 assert c.put('/').status_code == 40525 meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow'])26 assert sorted(meths) == ['GET', 'HEAD', 'OPTIONS', 'POST']2728def test_basic_view():29 app = flask.Flask(__name__)3031 class Index(flask.views.View):32 methods = ['GET', 'POST']33 def dispatch_request(self):34 return flask.request.method3536 app.add_url_rule('/', view_func=Index.as_view('index'))37 common_test(app)3839def test_method_based_view():40 app = flask.Flask(__name__)4142 class Index(flask.views.MethodView):43 def get(self):44 return 'GET'45 def post(self):46 return 'POST'4748 app.add_url_rule('/', view_func=Index.as_view('index'))4950 common_test(app)5152def test_view_patching():53 app = flask.Flask(__name__)5455 class Index(flask.views.MethodView):56 def get(self):57 1 // 058 def post(self):59 1 // 06061 class Other(Index):62 def get(self):63 return 'GET'64 def post(self):65 return 'POST'6667 view = Index.as_view('index')68 view.view_class = Other69 app.add_url_rule('/', view_func=view)70 common_test(app)7172def test_view_inheritance():73 app = flask.Flask(__name__)7475 class Index(flask.views.MethodView):76 def get(self):77 return 'GET'78 def post(self):79 return 'POST'8081 class BetterIndex(Index):82 def delete(self):83 return 'DELETE'8485 app.add_url_rule('/', view_func=BetterIndex.as_view('index'))86 c = app.test_client()8788 meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow'])89 assert sorted(meths) == ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST']9091def test_view_decorators():92 app = flask.Flask(__name__)9394 def add_x_parachute(f):95 def new_function(*args, **kwargs):96 resp = flask.make_response(f(*args, **kwargs))97 resp.headers['X-Parachute'] = 'awesome'98 return resp99 return new_function100101 class Index(flask.views.View):102 decorators = [add_x_parachute]103 def dispatch_request(self):104 return 'Awesome'105106 app.add_url_rule('/', view_func=Index.as_view('index'))107 c = app.test_client()108 rv = c.get('/')109 assert rv.headers['X-Parachute'] == 'awesome'110 assert rv.data == b'Awesome'111112def test_implicit_head():113 app = flask.Flask(__name__)114115 class Index(flask.views.MethodView):116 def get(self):117 return flask.Response('Blub', headers={118 'X-Method': flask.request.method119 })120121 app.add_url_rule('/', view_func=Index.as_view('index'))122 c = app.test_client()123 rv = c.get('/')124 assert rv.data == b'Blub'125 assert rv.headers['X-Method'] == 'GET'126 rv = c.head('/')127 assert rv.data == b''128 assert rv.headers['X-Method'] == 'HEAD'129130def test_explicit_head():131 app = flask.Flask(__name__)132133 class Index(flask.views.MethodView):134 def get(self):135 return 'GET'136 def head(self):137 return flask.Response('', headers={'X-Method': 'HEAD'})138139 app.add_url_rule('/', view_func=Index.as_view('index'))140 c = app.test_client()141 rv = c.get('/')142 assert rv.data == b'GET'143 rv = c.head('/')144 assert rv.data == b''145 assert rv.headers['X-Method'] == 'HEAD'146147def test_endpoint_override():148 app = flask.Flask(__name__)149 app.debug = True150151 class Index(flask.views.View):152 methods = ['GET', 'POST']153 def dispatch_request(self):154 return flask.request.method155156 app.add_url_rule('/', view_func=Index.as_view('index'))157158 with pytest.raises(AssertionError):159 app.add_url_rule('/', view_func=Index.as_view('index'))160161 # But these tests should still pass. We just log a warning.162 common_test(app)