Skip to content

Commit 0636715

Browse files
author
Joel Collins
committed
Fixed response encoding in tests
1 parent 9e013e5 commit 0636715

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

labthings/server/default_views/extensions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def get(self):
1717
Returns a list of Extension representations, including basic documentation.
1818
Describes server methods, web views, and other relevant Lab Things metadata.
1919
"""
20-
return registered_extensions().values()
20+
return registered_extensions().values() or []

labthings/server/view/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask.views import MethodView
2-
from flask import request
2+
from flask import request, make_response
33
from werkzeug.wrappers import Response as ResponseBase
44
from werkzeug.exceptions import MethodNotAllowed
55

@@ -58,9 +58,7 @@ def dispatch_request(self, *args, **kwargs):
5858
representations = self.representations or OrderedDict()
5959

6060
# noinspection PyUnresolvedReferences
61-
mediatype = request.accept_mimetypes.best_match(
62-
representations, default="application/json"
63-
)
61+
mediatype = request.accept_mimetypes.best_match(representations, default=None)
6462
if mediatype in representations:
6563
data, code, headers = unpack(resp)
6664
resp = representations[mediatype](data, code, headers)

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
from labthings.server.labthing import LabThing
77
from labthings.server.view import View
88

9+
from flask.testing import FlaskClient
10+
11+
12+
class JsonClient(FlaskClient):
13+
def open(self, *args, **kwargs):
14+
kwargs.setdefault(
15+
"headers",
16+
{"Content-Type": "application/json", "Accept": "application/json"},
17+
)
18+
kwargs.setdefault("content_type", "application/json")
19+
return super().open(*args, **kwargs)
20+
921

1022
@pytest.fixture
1123
def view_cls():
@@ -111,11 +123,13 @@ def req_ctx(app):
111123

112124
@pytest.fixture
113125
def client(app):
126+
app.test_client_class = JsonClient
114127
return app.test_client()
115128

116129

117130
@pytest.fixture
118131
def thing_client(thing):
132+
thing.app.test_client_class = JsonClient
119133
return thing.app.test_client()
120134

121135

tests/test_server_decorators.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ def put(self, args):
180180
app.add_url_rule("/", view_func=WrappedCls.as_view("index"))
181181

182182
with client as c:
183-
assert c.get("/").data == b'{"integer":1}\n'
184-
assert c.post("/", json={"integer": 5}).data == b'{"integer":5}\n'
185-
assert c.put("/", json={"integer": 5}).data == b'{"integer":5}\n'
183+
assert c.get("/").json == {"integer": 1}
184+
assert c.post("/", json={"integer": 5}).json == {"integer": 5}
185+
assert c.put("/", json={"integer": 5}).json == {"integer": 5}
186186

187187

188188
def test_property_schema_empty_class(empty_cls):
@@ -205,7 +205,7 @@ def post(self, data):
205205
app.add_url_rule("/", view_func=Index.as_view("index"))
206206

207207
with client as c:
208-
assert c.post("/", data=b"5\n").data == b"5"
208+
assert c.post("/", data=b"5\n").data == b'"5"\n'
209209

210210

211211
def test_use_body_required_no_data(app, client):
@@ -254,7 +254,7 @@ def post(self, data):
254254
app.add_url_rule("/", view_func=Index.as_view("index"))
255255

256256
with client as c:
257-
assert c.post("/").data == b"5"
257+
assert c.post("/").data == b'"5"\n'
258258

259259

260260
def test_use_body_malformed(app, client):
@@ -276,7 +276,7 @@ def post(self, data):
276276
def test_use_args(app, client):
277277
class Index(View):
278278
def post(self, data):
279-
return str(data)
279+
return data
280280

281281
schema = _Schema.from_dict({"integer": fields.Int()})()
282282
Index.post = decorators.use_args(schema)(Index.post)
@@ -286,7 +286,7 @@ def post(self, data):
286286
app.add_url_rule("/", view_func=Index.as_view("index"))
287287

288288
with client as c:
289-
assert c.post("/", json={"integer": 5}).data == b"{'integer': 5}"
289+
assert c.post("/", json={"integer": 5}).json == {"integer": 5}
290290

291291

292292
def test_use_args_field(app, client):
@@ -302,7 +302,7 @@ def post(self, data):
302302
app.add_url_rule("/", view_func=Index.as_view("index"))
303303

304304
with client as c:
305-
assert c.post("/").data == b"5"
305+
assert c.post("/").data == b'"5"\n'
306306

307307

308308
def test_doc(empty_cls):

tests/test_server_view_builder.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ def test_property_of(app, client):
1010
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))
1111

1212
with client as c:
13-
assert c.get("/").data == b"propertyValue"
14-
assert c.post("/", data=b"newPropertyValue").data == b"newPropertyValue"
15-
assert c.get("/").data == b"newPropertyValue"
13+
assert c.get("/").data == b'"propertyValue"\n'
14+
assert c.post("/", data=b"newPropertyValue").data == b'"newPropertyValue"\n'
15+
assert c.get("/").data == b'"newPropertyValue"\n'
1616

1717

1818
def test_property_of_dict(app, client):
@@ -31,18 +31,17 @@ def test_property_of_dict(app, client):
3131
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))
3232

3333
with client as c:
34-
assert (
35-
c.get("/").data
36-
== b'{"property_name":"propertyValue","property_name_2":"propertyValue2"}\n'
37-
)
38-
assert (
39-
c.put("/", json={"property_name": "newPropertyValue"}).data
40-
== b'{"property_name":"newPropertyValue","property_name_2":"propertyValue2"}\n'
41-
)
42-
assert (
43-
c.post("/", json={"property_name": "newPropertyValue"}).data
44-
== b'{"property_name":"newPropertyValue"}\n'
45-
)
34+
assert c.get("/").json == {
35+
"property_name": "propertyValue",
36+
"property_name_2": "propertyValue2",
37+
}
38+
assert c.put("/", json={"property_name": "newPropertyValue"}).json == {
39+
"property_name": "newPropertyValue",
40+
"property_name_2": "propertyValue2",
41+
}
42+
assert c.post("/", json={"property_name": "newPropertyValue"}).json == {
43+
"property_name": "newPropertyValue"
44+
}
4645

4746

4847
def test_property_of_readonly():
@@ -72,7 +71,7 @@ def f(arg: int, kwarg: str = "default"):
7271
app.add_url_rule("/", view_func=GeneratedClass.as_view("index"))
7372

7473
with client as c:
75-
assert c.post("/", json={"arg": 5}).data == b'{"arg":5,"kwarg":"default"}\n'
74+
assert c.post("/", json={"arg": 5}).json == {"arg": 5, "kwarg": "default"}
7675

7776

7877
def test_action_from_task(app, client):

0 commit comments

Comments
 (0)