-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer_test.go
57 lines (47 loc) · 1.97 KB
/
renderer_test.go
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
package problem
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJsonRenderer(t *testing.T) {
t.Run("should render as json if problem struct is empty", func(t *testing.T) {
w := httptest.NewRecorder()
problem := Problem{}
problem.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"))
assert.Equal(t, `{}`, w.Body.String())
})
t.Run("should render as json response", func(t *testing.T) {
w := httptest.NewRecorder()
problem := Problem{
Status: http.StatusServiceUnavailable,
Title: "Service Maintenance",
Detail: "API is under maintenance",
Instance: "/ping",
}
problem.WithExtension("version", "1.0.0")
problem.WithExtension("maintenance", true)
problem.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 create generic error if cannot encode the struct", func(t *testing.T) {
w := httptest.NewRecorder()
problem := Problem{}
problem.WithExtension("bogus", func() {})
problem.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())
})
}