Skip to content

Commit efcc038

Browse files
committed
Added CBOR representation tests
1 parent c6cccdd commit efcc038

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

tests/test_server_representations.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from labthings.server import representations
22
from flask import Response
3+
import cbor2
34
import pytest
45

56

@@ -60,3 +61,29 @@ def test_pretty_output_json(app_ctx_debug):
6061
response.data
6162
== b'{\n "a": "String",\n "b": 5,\n "c": [\n 10,\n 20,\n 30,\n 40,\n 50\n ],\n "d": {\n "a": "String",\n "b": 5,\n "c": [\n 10,\n 20,\n 30,\n 40,\n 50\n ]\n }\n}\n'
6263
)
64+
65+
66+
def test_encode_cbor():
67+
data = {
68+
"a": "String",
69+
"b": 5,
70+
"c": [10, 20, 30, 40, 50],
71+
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
72+
}
73+
assert cbor2.loads(representations.encode_cbor(data)) == data
74+
75+
76+
def test_output_cbor(app_ctx):
77+
data = {
78+
"a": "String",
79+
"b": 5,
80+
"c": [10, 20, 30, 40, 50],
81+
"d": {"a": "String", "b": 5, "c": [10, 20, 30, 40, 50]},
82+
}
83+
84+
with app_ctx.test_request_context():
85+
response = representations.output_cbor(data, 200)
86+
assert isinstance(response, Response)
87+
assert response.status_code == 200
88+
assert response.headers.get("Content-Type") == "application/cbor"
89+
assert cbor2.loads(response.data) == data

tests/test_server_view.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from werkzeug.wrappers import Response as ResponseBase
44
from flask import make_response
55

6+
import json
7+
import cbor2
8+
69
import pytest
710

811

@@ -53,14 +56,29 @@ def post(self):
5356
def test_accept_default_application_json(app, client):
5457
class Index(view.View):
5558
def get(self):
56-
return "GET"
59+
return {"key": "value"}
5760

5861
app.add_url_rule("/", view_func=Index.as_view("index"))
5962

6063
with client:
61-
res = client.get("/", headers=[("Accept", "application/json")])
64+
res = client.get("/")
6265
assert res.status_code == 200
6366
assert res.content_type == "application/json"
67+
assert json.loads(res.data) == {"key": "value"}
68+
69+
70+
def test_accept_default_application_cbor(app, cbor_client):
71+
class Index(view.View):
72+
def get(self):
73+
return {"key": "value"}
74+
75+
app.add_url_rule("/", view_func=Index.as_view("index"))
76+
77+
with cbor_client:
78+
res = cbor_client.get("/")
79+
assert res.status_code == 200
80+
assert res.content_type == "application/cbor"
81+
assert cbor2.loads(res.data) == {"key": "value"}
6482

6583

6684
def test_return_response(app, client):
@@ -71,7 +89,7 @@ def get(self):
7189
app.add_url_rule("/", view_func=Index.as_view("index"))
7290

7391
with client:
74-
res = client.get("/", headers=[("Accept", "application/json")])
92+
res = client.get("/")
7593
assert res.status_code == 200
7694
assert res.data == b"GET"
7795

@@ -84,7 +102,7 @@ def get(self):
84102
app.add_url_rule("/", view_func=Index.as_view("index"))
85103

86104
with client:
87-
res = client.post("/", headers=[("Accept", "application/json")])
105+
res = client.post("/")
88106
assert res.status_code == 405
89107

90108

@@ -105,6 +123,7 @@ class Index(view.View):
105123
def get(self):
106124
return "GET"
107125

126+
# Main test
108127
assert Index().get_value() == "GET"
109128

110129

@@ -113,6 +132,7 @@ class Index(view.View):
113132
def post(self):
114133
return "POST"
115134

135+
# Main test
116136
assert Index().get_value() is None
117137

118138

@@ -124,6 +144,7 @@ def post(self):
124144
Index.get = "GET"
125145

126146
with pytest.raises(TypeError):
147+
# Main test
127148
Index().get_value()
128149

129150

@@ -134,7 +155,8 @@ def get(self):
134155

135156
with app_ctx.test_request_context():
136157
assert isinstance(Index().get(), ResponseBase)
137-
assert Index().get().json is None
158+
assert Index().get().headers.get("Content-Type") == "text/html; charset=utf-8"
159+
# Main test
138160
assert Index().get_value() == "GET"
139161

140162

@@ -145,5 +167,6 @@ def get(self):
145167

146168
with app_ctx.test_request_context():
147169
assert isinstance(Index().get(), ResponseBase)
148-
assert Index().get().json is not None
170+
assert Index().get().headers.get("Content-Type") == "application/json"
171+
# Main test
149172
assert Index().get_value() == {"json": "body"}

0 commit comments

Comments
 (0)