-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrapper_test.go
297 lines (248 loc) · 7.91 KB
/
wrapper_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
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
package schema
import (
"bytes"
"io"
"net/http"
"testing"
"encoding/json"
"strings"
"net/http/httptest"
"github.com/gin-gonic/gin"
"github.com/xeipuuv/gojsonschema"
)
var testSchema = `
{
"title": "test",
"type": "object",
"properties": {
"value1": {
"type": "integer"
},
"value2": {
"type": "boolean"
}
},
"required": ["value1"],
"additionalProperties": false
}`
type testBody struct {
Value1 int `json:"value1"`
Value2 bool `json:"value2"`
}
func TestBuildSchemaFromString(t *testing.T) {
sch, err := buildSchemaFromString(testSchema)
if err != nil {
t.Fatalf("cannot build schema from string: %v", err)
}
// call again with same schema
sch2, err2 := buildSchemaFromString(testSchema)
if err2 != nil {
t.Fatalf("cannot build schema from string: %v", err)
}
if sch != sch2 {
t.Errorf("expected schema to be equal between calls: %v", err)
}
// build invalid schema error
_, err = buildSchemaFromString(`{"a": `)
if err == nil {
t.Errorf("build should return error, but received nil")
}
}
func TestBuildSchemaFromStringInvalidSchema(t *testing.T) {
_, err := buildSchemaFromString(`{"a"`)
if err == nil {
t.Errorf("build schema should return error, but returns nil")
} else {
switch err.(type) {
case *ErrCannotBuildSchema:
default:
t.Errorf("build schema should return ErrCannotBuildSchema, but returns %v", err)
}
}
}
func createRequestWithBody(t *testing.T, body io.Reader) *http.Request {
req, err := http.NewRequest("POST", "/", body)
if err != nil {
t.Fatalf("cannot create new request: %v", err)
}
return req
}
func TestValidateBodyUsingSchemaNilBody(t *testing.T) {
req := createRequestWithBody(t, nil)
err := validateBodyUsingSchema(req, nil)
if err != io.EOF {
t.Errorf("validate returns unexpected error, want %v received %v", io.EOF, err)
return
}
}
func TestValidateBodyUsingSchemaEmptyBody(t *testing.T) {
req := createRequestWithBody(t, bytes.NewReader([]byte("")))
err := validateBodyUsingSchema(req, nil)
if err != io.EOF {
t.Errorf("validate returns unexpected error, want %v received %v", io.EOF, err)
return
}
}
func createSchemaFromString(t *testing.T, str string) *gojsonschema.Schema {
sch, err := buildSchemaFromString(str)
if err != nil {
t.Fatalf("cannot create schema: %v", err)
}
return sch
}
func TestValidateBodyUsingSchemaNonJSONBody(t *testing.T) {
ch := createSchemaFromString(t, `{}`)
// check json syntax error
req := createRequestWithBody(t, strings.NewReader(`{a: 1`))
err := validateBodyUsingSchema(req, ch)
if err == nil {
t.Errorf("validate should return syntax error, but nil received")
} else {
switch v := err.(type) {
case *json.SyntaxError:
// ok
default:
t.Errorf("validate should return %T received %T:%v", &json.SyntaxError{}, v, err)
}
}
req2 := createRequestWithBody(t, strings.NewReader(`{"a": 1`))
err = validateBodyUsingSchema(req2, ch)
if err != io.ErrUnexpectedEOF {
t.Errorf("validate should return %v received %v", io.ErrUnexpectedEOF, err)
}
}
func TestValidateBodyUsingSchemaInvalidJSON(t *testing.T) {
sc := createSchemaFromString(t, testSchema)
// bad value1 type
req := createRequestWithBody(t, strings.NewReader(`{"value1": "bad type"}`))
err := validateBodyUsingSchema(req, sc)
if err == nil {
t.Errorf("validate should return error, but received nil")
}
// extra field that not present in schema
req2 := createRequestWithBody(t, strings.NewReader(`{"value1": 1, "value3": 3}`))
err = validateBodyUsingSchema(req2, sc)
if err == nil {
t.Errorf("validate should return error, but received nil")
}
// valid json
req3 := createRequestWithBody(t, strings.NewReader(`{"value1": 1, "value2": true}`))
err = validateBodyUsingSchema(req3, sc)
if err != nil {
t.Errorf("validate should return nil, but received %v", err)
}
}
func getTestServer(handlerFunc gin.HandlerFunc) *httptest.Server {
// disable debug message in console
gin.SetMode(gin.ReleaseMode)
// using `New` instead of `Default` to disable logging and recovery
r := gin.New()
r.POST("/", handlerFunc)
return httptest.NewServer(r)
}
func okHandler(c *gin.Context) {
c.Status(http.StatusOK)
}
func testRequest(t *testing.T, url string, body io.Reader, expectedStatusCode int) {
res, err := http.Post(url, "application/json", body)
if err != nil {
t.Fatalf("error happens during post request %v", err)
}
if res.StatusCode != expectedStatusCode {
t.Errorf("expected response status code %v, but received %v", expectedStatusCode, res.StatusCode)
}
}
func testHandlerResponses(t *testing.T, handler gin.HandlerFunc) {
// ensure server validate requests using `testSchema`
ts := getTestServer(handler)
defer ts.Close()
// nil body
testRequest(t, ts.URL, nil, http.StatusBadRequest)
// empty body
testRequest(t, ts.URL, strings.NewReader(``), http.StatusBadRequest)
// non json, invalid syntax
testRequest(t, ts.URL, strings.NewReader(`{a: 1`), http.StatusBadRequest)
// non json, unexpected eof
testRequest(t, ts.URL, strings.NewReader(`{"a": 1`), http.StatusBadRequest)
// non json, unexpected eof
testRequest(t, ts.URL, strings.NewReader(`{"a": 1`), http.StatusBadRequest)
// invalid json, wrong value1 type
testRequest(t, ts.URL, strings.NewReader(`{"value1": "aaa"}`), http.StatusBadRequest)
// invalid json, additional field that not present in schema
testRequest(t, ts.URL, strings.NewReader(`{"value1": 1, "value3": "aaa"}`), http.StatusBadRequest)
// invalid json, missing required field
testRequest(t, ts.URL, strings.NewReader(`{"value2": true}`), http.StatusBadRequest)
// valid json
testRequest(t, ts.URL, strings.NewReader(`{"value1": 1, "value2": true}`), http.StatusOK)
}
func TestValidate(t *testing.T) {
testHandlerResponses(t, Validate(okHandler, testSchema))
}
func TestValidateSchema(t *testing.T) {
// build schema from string
sc, err := buildSchemaFromString(testSchema)
if err != nil {
t.Errorf("cannot build schema from string %v", testSchema)
}
testHandlerResponses(t, ValidateSchema(okHandler, sc))
}
func makeBindJSONHandler(t *testing.T) gin.HandlerFunc {
return func(c *gin.Context) {
var js testBody
BindJSON(c, testSchema, &js)
c.Status(http.StatusOK)
}
}
func makeBindJSONSchemaHandler(t *testing.T) gin.HandlerFunc {
sch := createSchemaFromString(t, testSchema)
return func(c *gin.Context) {
var js testBody
BindJSONSchema(c, sch, &js)
c.Status(http.StatusOK)
}
}
func TestBindJSONValidate(t *testing.T) {
testHandlerResponses(t, Validate(makeBindJSONHandler(t), testSchema))
}
func TestBindJSONSchemaValidate(t *testing.T) {
testHandlerResponses(t, Validate(makeBindJSONSchemaHandler(t), testSchema))
}
func makeBindJSONUnmarshalHandler(t *testing.T) gin.HandlerFunc {
return func(c *gin.Context) {
var js testBody
BindJSON(c, testSchema, &js)
if js.Value1 != 2 {
t.Errorf("value1 expected to be %v but received %v", 2, js.Value1)
}
if js.Value2 != true {
t.Errorf("value2 expected to be %v but received %v", true, js.Value2)
}
c.Status(http.StatusOK)
}
}
func makeBindJSONSchemaUnmarshalHandler(t *testing.T) gin.HandlerFunc {
sch := createSchemaFromString(t, testSchema)
return func(c *gin.Context) {
var js testBody
BindJSONSchema(c, sch, &js)
if js.Value1 != 2 {
t.Errorf("value1 expected to be %v but received %v", 2, js.Value1)
}
if js.Value2 != true {
t.Errorf("value2 expected to be %v but received %v", true, js.Value2)
}
c.Status(http.StatusOK)
}
}
func testUnmarshalHandler(t *testing.T, handler gin.HandlerFunc) {
// ensure server validate requests using `testSchema`
ts := getTestServer(handler)
defer ts.Close()
testRequest(t, ts.URL, strings.NewReader(`{"value1": 2, "value2": true}`), http.StatusOK)
}
func TestBindJSONUnmarshal(t *testing.T) {
testUnmarshalHandler(t, Validate(makeBindJSONUnmarshalHandler(t), testSchema))
}
func TestBindJSONSchemaUnmarshal(t *testing.T) {
testUnmarshalHandler(t, Validate(makeBindJSONSchemaUnmarshalHandler(t), testSchema))
}