-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joel Collins
committed
Apr 14, 2020
1 parent
c65aa71
commit 37b4703
Showing
1 changed file
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from labthings.server import view | ||
from werkzeug.http import parse_set_header | ||
from werkzeug.wrappers import Response as ResponseBase | ||
from flask import make_response | ||
|
||
import pytest | ||
|
||
|
||
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 | ||
assert c.delete("/").status_code == 405 | ||
meths = parse_set_header(c.open("/", method="OPTIONS").headers["Allow"]) | ||
assert sorted(meths) == ["GET", "HEAD", "OPTIONS", "POST"] | ||
|
||
|
||
def test_method_based_view(app): | ||
class Index(view.View): | ||
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(view.View): | ||
def get(self): | ||
1 // 0 | ||
|
||
def post(self): | ||
1 // 0 | ||
|
||
class Other(Index): | ||
def get(self): | ||
return "GET" | ||
|
||
def post(self): | ||
return "POST" | ||
|
||
view_obj = Index.as_view("index") | ||
view_obj.view_class = Other | ||
app.add_url_rule("/", view_func=view_obj) | ||
common_test(app) | ||
|
||
|
||
def test_accept_default_application_json(app, client): | ||
class Index(view.View): | ||
def get(self): | ||
return "GET" | ||
|
||
app.add_url_rule("/", view_func=Index.as_view("index")) | ||
|
||
with client: | ||
res = client.get("/", headers=[("Accept", "application/json")]) | ||
assert res.status_code == 200 | ||
assert res.content_type == "application/json" | ||
|
||
|
||
def test_return_response(app, client): | ||
class Index(view.View): | ||
def get(self): | ||
return make_response("GET", 200) | ||
|
||
app.add_url_rule("/", view_func=Index.as_view("index")) | ||
|
||
with client: | ||
res = client.get("/", headers=[("Accept", "application/json")]) | ||
assert res.status_code == 200 | ||
assert res.data == b"GET" | ||
|
||
|
||
def test_missing_method(app, client): | ||
class Index(view.View): | ||
def get(self): | ||
return "GET" | ||
|
||
app.add_url_rule("/", view_func=Index.as_view("index")) | ||
|
||
with client: | ||
res = client.post("/", headers=[("Accept", "application/json")]) | ||
assert res.status_code == 405 | ||
|
||
|
||
def test_missing_head_method(app, client): | ||
class Index(view.View): | ||
def get(self): | ||
return "GET" | ||
|
||
app.add_url_rule("/", view_func=Index.as_view("index")) | ||
|
||
with client: | ||
res = client.head("/") | ||
assert res.status_code == 200 | ||
|
||
|
||
def test_get_value_text(): | ||
class Index(view.View): | ||
def get(self): | ||
return "GET" | ||
|
||
assert Index().get_value() == "GET" | ||
|
||
|
||
def test_get_value_missing(): | ||
class Index(view.View): | ||
def post(self): | ||
return "POST" | ||
|
||
assert Index().get_value() is None | ||
|
||
|
||
def test_get_value_raise_if_not_callable(): | ||
class Index(view.View): | ||
def post(self): | ||
return "POST" | ||
|
||
Index.get = "GET" | ||
|
||
with pytest.raises(TypeError): | ||
Index().get_value() | ||
|
||
|
||
def test_get_value_response_text(app_ctx): | ||
class Index(view.View): | ||
def get(self): | ||
return make_response("GET", 200) | ||
|
||
with app_ctx.test_request_context(): | ||
assert isinstance(Index().get(), ResponseBase) | ||
assert Index().get().json is None | ||
assert Index().get_value() == "GET" | ||
|
||
|
||
def test_get_value_response_json(app_ctx): | ||
class Index(view.View): | ||
def get(self): | ||
return make_response({"json": "body"}, 200) | ||
|
||
with app_ctx.test_request_context(): | ||
assert isinstance(Index().get(), ResponseBase) | ||
assert Index().get().json is not None | ||
assert Index().get_value() == {"json": "body"} |