-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
320 lines (264 loc) · 11 KB
/
json_test.go
File metadata and controls
320 lines (264 loc) · 11 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package problem_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/kodeart/go-problem/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestJsonMarshal(t *testing.T) {
t.Run("should create a new empty instance", func(t *testing.T) {
p := problem.Problem{}
jsonData, err := json.Marshal(p)
require.Nil(t, err)
assert.JSONEq(t, `{}`, string(jsonData))
})
t.Run("should marshal to json", func(t *testing.T) {
p := problem.Problem{
Status: http.StatusForbidden,
Title: "Balance Error",
Detail: "You do not have enough credit.",
Instance: "/",
Type: "/errors/balance",
// Extensions: problem.Extensions{
Extensions: map[string]any{
"balance": 42,
"accounts": []any{"/account/12345", "/account/67890"},
},
}
jsonData, err := json.Marshal(p)
assert.Nil(t, err)
expected := `{"status":403,"instance":"/","title":"Balance Error","detail":"You do not have enough credit.","balance":42,"accounts":["/account/12345","/account/67890"],"type":"/errors/balance"}`
assert.JSONEq(t, expected, string(jsonData))
})
t.Run("should add and remove extensions to instance", func(t *testing.T) {
p := problem.Problem{
Instance: "/",
Detail: "Balance error",
Status: http.StatusBadRequest,
Type: "/errors/balance",
}
p.WithExtension("customField", "customValue").
WithExtension("balance", float64(42)).
WithExtension("accounts", []string{"account1", "account2"}).
WithoutExtension("customField")
body, err := json.Marshal(p)
expected := `{"status":400,"instance":"/","detail":"Balance error","balance":42,"accounts":["account1","account2"],"type":"/errors/balance"}`
require.Nil(t, err)
assert.JSONEq(t, expected, string(body))
})
t.Run("should not set extension value if nil", func(t *testing.T) {
p := problem.Problem{
Extensions: map[string]any{"errors": nil},
}
body, err := json.Marshal(p)
require.Nil(t, err)
assert.JSONEq(t, `{}`, string(body))
})
}
func TestJsonUnmarshal(t *testing.T) {
t.Run("should fail unmarshalling an invalid json", func(t *testing.T) {
var e problem.Problem
jsonData := `{"type": null,`
err := e.UnmarshalJSON( []byte(jsonData))
require.Error(t, err)
assert.Contains(t, err.Error(), "unexpected end of JSON input")
})
t.Run("should fail unmarshalling nil", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal(nil, &e)
require.Error(t, err)
assert.Contains(t, err.Error(), "unexpected end of JSON input")
})
t.Run("should fail unmarshalling an empty string", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(""), &e)
require.Error(t, err)
assert.Contains(t, err.Error(), "unexpected end of JSON input")
})
t.Run("should fail unmarshalling a json array", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(`[]`), &e)
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot unmarshal array")
})
t.Run("should construct from empty json object", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(`{}`), &e)
require.Nil(t, err)
assert.Equal(t, e.Status, 0)
assert.Empty(t, e.Detail)
assert.Empty(t, e.Instance)
assert.Empty(t, e.Title)
assert.Empty(t, e.Type)
assert.Empty(t, e.Extensions)
})
t.Run("should return error if status is empty string", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(`{"status": ""}`), &e)
require.EqualError(t, err, "invalid status type: string")
})
t.Run("should return error if status is a string", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(`{"status": "invalid"}`), &e)
require.EqualError(t, err, "invalid status type: string")
})
t.Run("should return error if status is not a number", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(`{"status": true}`), &e)
require.EqualError(t, err, "invalid status type: bool")
})
t.Run("should return error if status is nil", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(`{"status": null}`), &e)
require.EqualError(t, err, "invalid status type: <nil>")
})
t.Run("should convert the status from string integer", func(t *testing.T) {
var e problem.Problem
err := json.Unmarshal([]byte(`{"status": "200"}`), &e)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, e.Status)
})
t.Run("should unmarshal non rfc-9457 json string", func(t *testing.T) {
var e problem.Problem
legacyError := `{"message": "refactor this message", "code": 500}`
err := json.Unmarshal([]byte(legacyError), &e)
require.Nil(t, err)
assert.Equal(t, "refactor this message", e.Extensions["message"])
assert.Equal(t, float64(500), e.Extensions["code"], "json.Unmarshal() creates a float64")
})
t.Run("should unmarshal only provided fields without extensions", func(t *testing.T) {
var e problem.Problem
jsonData := []byte(`{"status": 200, "detail": "Hello World"}`)
err := json.Unmarshal(jsonData, &e)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, e.Status)
assert.Equal(t, "Hello World", e.Detail)
assert.Equal(t, map[string]any{}, e.Extensions)
// other fields should be empty
assert.Empty(t, e.Instance)
assert.Empty(t, e.Title)
assert.Empty(t, e.Type)
assert.Empty(t, e.Extensions)
})
t.Run("should unmarshal only provided fields with extensions", func(t *testing.T) {
var e problem.Problem
jsonData := `{
"status": 422,
"type": "https://example.net/validation-error",
"title": "Your request is not valid.",
"errors": [
{
"detail": "must be a positive integer",
"pointer": "#/age"
},
{
"detail": "must be 'green', 'red' or 'blue'",
"pointer": "#/profile/color"
}
]
}`
err := json.Unmarshal( []byte(jsonData), &e)
assert.Nil(t, err)
// these should be empty
assert.Empty(t, e.Instance)
assert.Empty(t, e.Detail)
assert.Equal(t, http.StatusUnprocessableEntity, e.Status)
assert.Equal(t, "Your request is not valid.", e.Title)
assert.Equal(t, "https://example.net/validation-error", e.Type)
assert.Equal(t, map[string]any{
"errors": []any{
map[string]any{"detail": "must be a positive integer", "pointer": "#/age"},
map[string]any{"detail": "must be 'green', 'red' or 'blue'", "pointer": "#/profile/color"},
},
}, e.Extensions)
extErrors := e.Extensions["errors"].([]any)
assert.Equal(t, map[string]any{
"detail": "must be a positive integer",
"pointer": "#/age",
}, extErrors[0])
assert.Equal(t, map[string]any{
"detail": "must be 'green', 'red' or 'blue'",
"pointer": "#/profile/color",
}, extErrors[1])
})
t.Run("should unmarshal all fields", func(t *testing.T) {
var e problem.Problem
jsonData :=`{
"status": 403,
"type": "https://example.com/probs/out-of-credit",
"title": "You do not have enough credit.",
"detail": "Your current balance is 30, but that costs 50.",
"instance": "/account/12345/msgs/abc",
"balance": 30,
"accounts": ["/account/12345", "/account/67890"]
}`
err := json.Unmarshal( []byte(jsonData), &e)
require.Nil(t, err)
assert.Equal(t, http.StatusForbidden, e.Status)
assert.Equal(t, "/account/12345/msgs/abc", e.Instance)
assert.Equal(t, "Your current balance is 30, but that costs 50.", e.Detail)
assert.Equal(t, "You do not have enough credit.", e.Title)
assert.Equal(t, "https://example.com/probs/out-of-credit", e.Type)
assert.Equal(t, map[string]any{
"balance": float64(30),
"accounts": []any{"/account/12345", "/account/67890"},
}, e.Extensions)
assert.Equal(t, float64(30), e.Extensions["balance"])
assert.Equal(t, []any{"/account/12345", "/account/67890"}, e.Extensions["accounts"])
})
}
func TestJsonRenderer(t *testing.T) {
t.Run("should render an empty json object if problem is empty", func(t *testing.T) {
w := httptest.NewRecorder()
p := problem.Problem{}
p.JSON(w)
resp := w.Result()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/problem+json", resp.Header.Get("Content-Type"))
assert.Empty(t, resp.Header.Get("Cache-Control"), "caching is fine for empty problem")
assert.Equal(t, `{}`, w.Body.String())
})
t.Run("should render as json response", func(t *testing.T) {
w := httptest.NewRecorder()
p := problem.Problem{
Status: http.StatusServiceUnavailable,
Title: "Service Maintenance",
Detail: "API is under maintenance",
Instance: "/ping",
}
p.WithExtension("version", "1.0.0")
p.WithExtension("maintenance", true)
p.JSON(w)
resp := w.Result()
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
assert.Equal(t, "application/problem+json", resp.Header.Get("Content-Type"))
assert.Equal(t, "no-cache, no-store, must-revalidate", resp.Header.Get("Cache-Control"))
assert.JSONEq(t, `{"title":"Service Maintenance","detail":"API is under maintenance","instance":"/ping","status":503,"version":"1.0.0","maintenance":true}`, w.Body.String())
})
t.Run("should render generic json error if cannot encode the struct", func(t *testing.T) {
w := httptest.NewRecorder()
p := problem.Problem{}
p.WithExtension("bogus", func() {})
p.JSON(w)
resp := w.Result()
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
assert.Equal(t, "application/problem+json", resp.Header.Get("Content-Type"))
assert.Equal(t, "no-cache, no-store, must-revalidate", resp.Header.Get("Cache-Control"))
assert.JSONEq(t, `{"detail":"json: error calling MarshalJSON for type *problem.Problem: json: unsupported type: func()", "status":422, "title":"JSON Encoding Error"}`, w.Body.String())
})
t.Run("should not add cache control header for non cacheable response", func(t *testing.T) {
w := httptest.NewRecorder()
p := problem.Problem{
Status: http.StatusNotFound,
Title: "Page Not Found",
Detail: "The page you are looking for does not exist",
Instance: "/fubar",
}
p.JSON(w)
resp := w.Result()
assert.Empty(t, resp.Header.Get("Cache-Control"), "Cache-Control header is not set")
})
}