3
3
from werkzeug .wrappers import Response as ResponseBase
4
4
from flask import make_response
5
5
6
+ import json
7
+ import cbor2
8
+
6
9
import pytest
7
10
8
11
@@ -53,14 +56,29 @@ def post(self):
53
56
def test_accept_default_application_json (app , client ):
54
57
class Index (view .View ):
55
58
def get (self ):
56
- return "GET"
59
+ return { "key" : "value" }
57
60
58
61
app .add_url_rule ("/" , view_func = Index .as_view ("index" ))
59
62
60
63
with client :
61
- res = client .get ("/" , headers = [( "Accept" , "application/json" )] )
64
+ res = client .get ("/" )
62
65
assert res .status_code == 200
63
66
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" }
64
82
65
83
66
84
def test_return_response (app , client ):
@@ -71,7 +89,7 @@ def get(self):
71
89
app .add_url_rule ("/" , view_func = Index .as_view ("index" ))
72
90
73
91
with client :
74
- res = client .get ("/" , headers = [( "Accept" , "application/json" )] )
92
+ res = client .get ("/" )
75
93
assert res .status_code == 200
76
94
assert res .data == b"GET"
77
95
@@ -84,7 +102,7 @@ def get(self):
84
102
app .add_url_rule ("/" , view_func = Index .as_view ("index" ))
85
103
86
104
with client :
87
- res = client .post ("/" , headers = [( "Accept" , "application/json" )] )
105
+ res = client .post ("/" )
88
106
assert res .status_code == 405
89
107
90
108
@@ -105,6 +123,7 @@ class Index(view.View):
105
123
def get (self ):
106
124
return "GET"
107
125
126
+ # Main test
108
127
assert Index ().get_value () == "GET"
109
128
110
129
@@ -113,6 +132,7 @@ class Index(view.View):
113
132
def post (self ):
114
133
return "POST"
115
134
135
+ # Main test
116
136
assert Index ().get_value () is None
117
137
118
138
@@ -124,6 +144,7 @@ def post(self):
124
144
Index .get = "GET"
125
145
126
146
with pytest .raises (TypeError ):
147
+ # Main test
127
148
Index ().get_value ()
128
149
129
150
@@ -134,7 +155,8 @@ def get(self):
134
155
135
156
with app_ctx .test_request_context ():
136
157
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
138
160
assert Index ().get_value () == "GET"
139
161
140
162
@@ -145,5 +167,6 @@ def get(self):
145
167
146
168
with app_ctx .test_request_context ():
147
169
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
149
172
assert Index ().get_value () == {"json" : "body" }
0 commit comments