1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
import pytest
from werkzeug.http import parse_set_header
import flask.views
def common_test(app):
c = app.test_client()
assert c.get("/").data == b"GET"
assert c.post("/").data == b"POST"
assert c.put("/").status_code == 405
meths = parse_set_header(c.open("/", method="OPTIONS").headers["Allow"])
assert sorted(meths) == ["GET", "HEAD", "OPTIONS", "POST"]
def test_basic_view(app):
class Index(flask.views.View):
methods = ["GET", "POST"]
def dispatch_request(self):
return flask.request.method
app.add_url_rule("/", view_func=Index.as_view("index"))
common_test(app)
def test_method_based_view(app):
class Index(flask.views.MethodView):
def get(self):
return "GET"
def post(self):
return "POST"
app.add_url_rule("/", view_func=Index.as_view("index"))
common_test(app)
def test_view_patching(app):
class Index(flask.views.MethodView):
def get(self):
raise ZeroDivisionError
def post(self):
raise ZeroDivisionError
class Other(Index):
def get(self):
return "GET"
def post(self):
return "POST"
view = Index.as_view("index")
view.view_class = Other
app.add_url_rule("/", view_func=view)
common_test(app)
def test_view_inheritance(app, client):
class Index(flask.views.MethodView):
def get(self):
return "GET"
def post(self):
return "POST"
class BetterIndex(Index):
def delete(self):
return "DELETE"
app.add_url_rule("/", view_func=BetterIndex.as_view("index"))
meths = parse_set_header(client.open("/", method="OPTIONS").headers["Allow"])
assert sorted(meths) == ["DELETE", "GET", "HEAD", "OPTIONS", "POST"]
def test_view_decorators(app, client):
def add_x_parachute(f):
def new_function(*args, **kwargs):
resp = flask.make_response(f(*args, **kwargs))
resp.headers["X-Parachute"] = "awesome"
return resp
return new_function
class Index(flask.views.View):
decorators = [add_x_parachute]
def dispatch_request(self):
return "Awesome"
app.add_url_rule("/", view_func=Index.as_view("index"))
rv = client.get("/")
assert rv.headers["X-Parachute"] == "awesome"
assert rv.data == b"Awesome"
def test_view_provide_automatic_options_attr():
app = flask.Flask(__name__)
class Index1(flask.views.View):
provide_automatic_options = False
def dispatch_request(self):
return "Hello World!"
app.add_url_rule("/", view_func=Index1.as_view("index"))
c = app.test_client()
rv = c.open("/", method="OPTIONS")
assert rv.status_code == 405
app = flask.Flask(__name__)
class Index2(flask.views.View):
methods = ["OPTIONS"]
provide_automatic_options = True
def dispatch_request(self):
return "Hello World!"
app.add_url_rule("/", view_func=Index2.as_view("index"))
c = app.test_client()
rv = c.open("/", method="OPTIONS")
assert sorted(rv.allow) == ["OPTIONS"]
app = flask.Flask(__name__)
class Index3(flask.views.View):
def dispatch_request(self):
return "Hello World!"
app.add_url_rule("/", view_func=Index3.as_view("index"))
c = app.test_client()
rv = c.open("/", method="OPTIONS")
assert "OPTIONS" in rv.allow
def test_implicit_head(app, client):
class Index(flask.views.MethodView):
def get(self):
return flask.Response("Blub", headers={"X-Method": flask.request.method})
app.add_url_rule("/", view_func=Index.as_view("index"))
rv = client.get("/")
assert rv.data == b"Blub"
assert rv.headers["X-Method"] == "GET"
rv = client.head("/")
assert rv.data == b""
assert rv.headers["X-Method"] == "HEAD"
def test_explicit_head(app, client):
class Index(flask.views.MethodView):
def get(self):
return "GET"
def head(self):
return flask.Response("", headers={"X-Method": "HEAD"})
app.add_url_rule("/", view_func=Index.as_view("index"))
rv = client.get("/")
assert rv.data == b"GET"
rv = client.head("/")
assert rv.data == b""
assert rv.headers["X-Method"] == "HEAD"
def test_endpoint_override(app):
app.debug = True
class Index(flask.views.View):
methods = ["GET", "POST"]
def dispatch_request(self):
return flask.request.method
app.add_url_rule("/", view_func=Index.as_view("index"))
with pytest.raises(AssertionError):
app.add_url_rule("/", view_func=Index.as_view("index"))
# But these tests should still pass. We just log a warning.
common_test(app)
def test_methods_var_inheritance(app, client):
class BaseView(flask.views.MethodView):
methods = ["GET", "PROPFIND"]
class ChildView(BaseView):
def get(self):
return "GET"
def propfind(self):
return "PROPFIND"
app.add_url_rule("/", view_func=ChildView.as_view("index"))
assert client.get("/").data == b"GET"
assert client.open("/", method="PROPFIND").data == b"PROPFIND"
assert ChildView.methods == {"PROPFIND", "GET"}
def test_multiple_inheritance(app, client):
class GetView(flask.views.MethodView):
def get(self):
return "GET"
class DeleteView(flask.views.MethodView):
def delete(self):
return "DELETE"
class GetDeleteView(GetView, DeleteView):
pass
app.add_url_rule("/", view_func=GetDeleteView.as_view("index"))
assert client.get("/").data == b"GET"
assert client.delete("/").data == b"DELETE"
assert sorted(GetDeleteView.methods) == ["DELETE", "GET"]
def test_remove_method_from_parent(app, client):
class GetView(flask.views.MethodView):
def get(self):
return "GET"
class OtherView(flask.views.MethodView):
def post(self):
return "POST"
class View(GetView, OtherView):
methods = ["GET"]
app.add_url_rule("/", view_func=View.as_view("index"))
assert client.get("/").data == b"GET"
assert client.post("/").status_code == 405
assert sorted(View.methods) == ["GET"]
def test_init_once(app, client):
n = 0
class CountInit(flask.views.View):
init_every_request = False
def __init__(self):
nonlocal n
n += 1
def dispatch_request(self):
return str(n)
app.add_url_rule("/", view_func=CountInit.as_view("index"))
assert client.get("/").data == b"1"
assert client.get("/").data == b"1"
|