-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_test.go
180 lines (144 loc) · 4.3 KB
/
json_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
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
package render_test
import (
"errors"
"math"
"net/http"
"net/http/httptest"
"testing"
"gopkg.in/gavv/httpexpect.v1"
"github.com/ifreddyrondon/bastion/render"
)
type address struct {
Address string `json:"address"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
}
func TestNewJSONRenderer(t *testing.T) {
t.Parallel()
a := address{"test address", 1, 1}
expected := map[string]interface{}{"address": "test address", "lat": 1, "lng": 1}
rr := httptest.NewRecorder()
render.NewJSON().Response(rr, http.StatusOK, &a)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusOK).
JSON().Object().Equal(expected)
}
func TestJSONResponse(t *testing.T) {
t.Parallel()
a := address{"test address", 1, 1}
expected := map[string]interface{}{"address": "test address", "lat": 1, "lng": 1}
rr := httptest.NewRecorder()
render.JSON.Response(rr, http.StatusOK, &a)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusOK).
JSON().Object().Equal(expected)
}
func TestJSONRendererOptions(t *testing.T) {
t.Parallel()
a := &address{"test address", 1, 1}
tt := []struct {
name string
opts []func(*render.JSONRender)
a *address
expected string
}{
{
"marshal without indent",
[]func(*render.JSONRender){},
a,
"{\"address\":\"test address\",\"lat\":1,\"lng\":1}\n",
},
{
"marshal with indent (pretty print)",
[]func(*render.JSONRender){render.PrettyPrintJSON()},
a,
"{\n \"address\": \"test address\",\n \"lat\": 1,\n \"lng\": 1\n}\n",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rr := httptest.NewRecorder()
render.NewJSON(tc.opts...).Response(rr, http.StatusOK, tc.a)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusOK).
Body().
Equal(tc.expected)
})
}
}
func TestJSONResponseError(t *testing.T) {
t.Parallel()
rr := httptest.NewRecorder()
render.JSON.Response(rr, http.StatusOK, math.Inf(1))
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusInternalServerError).
Text().
Equal("json: unsupported value: +Inf\n")
}
func TestJSONSend(t *testing.T) {
t.Parallel()
a := address{"test address", 1, 1}
expected := map[string]interface{}{"address": "test address", "lat": 1, "lng": 1}
rr := httptest.NewRecorder()
render.JSON.Send(rr, &a)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusOK).
JSON().Object().Equal(expected)
}
func TestJSONCreated(t *testing.T) {
t.Parallel()
a := address{"test address", 1, 1}
expected := map[string]interface{}{"address": "test address", "lat": 1, "lng": 1}
rr := httptest.NewRecorder()
render.JSON.Created(rr, &a)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusCreated).
JSON().Object().Equal(expected)
}
func TestJSONNoContent(t *testing.T) {
t.Parallel()
rr := httptest.NewRecorder()
render.JSON.NoContent(rr)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusNoContent).NoContent()
}
func TestJSONBadRequest(t *testing.T) {
t.Parallel()
e := errors.New("test")
expected := map[string]interface{}{"message": "test", "error": "Bad Request", "status": 400}
rr := httptest.NewRecorder()
render.JSON.BadRequest(rr, e)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusBadRequest).
JSON().Object().Equal(expected)
}
func TestJSONNotFound(t *testing.T) {
t.Parallel()
e := errors.New("test")
expected := map[string]interface{}{"message": "test", "error": "Not Found", "status": 404}
rr := httptest.NewRecorder()
render.JSON.NotFound(rr, e)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusNotFound).
JSON().Object().Equal(expected)
}
func TestJSONMethodNotAllowed(t *testing.T) {
t.Parallel()
e := errors.New("test")
expected := map[string]interface{}{"message": "test", "error": "Method Not Allowed", "status": 405}
rr := httptest.NewRecorder()
render.JSON.MethodNotAllowed(rr, e)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusMethodNotAllowed).
JSON().Object().Equal(expected)
}
func TestJSONInternalServerError(t *testing.T) {
t.Parallel()
e := errors.New("test")
expected := map[string]interface{}{"message": "test", "error": "Internal Server Error", "status": 500}
rr := httptest.NewRecorder()
render.JSON.InternalServerError(rr, e)
httpexpect.NewResponse(t, rr.Result()).
Status(http.StatusInternalServerError).
JSON().Object().Equal(expected)
}