-
Notifications
You must be signed in to change notification settings - Fork 2
/
usecase_test.go
286 lines (246 loc) · 7.64 KB
/
usecase_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
package webfmwk
import (
"net/http"
"testing"
"github.com/burgesQ/gommon/webtest"
validator "github.com/go-playground/validator/v10"
"github.com/stretchr/testify/require"
)
type customContext struct {
Context
Value string
}
type testSerial struct {
A string `json:"test"`
}
type userForm struct {
Firstname string `json:"first_name" validate:"required,alpha"`
Lastname string `json:"last_name" validate:"required,custom"`
}
type queryParam struct {
Some *int `json:"some,omitempty" schema:"some" validate:"omitempty,min=-1"`
Pretty bool `json:"pretty" schema:"pretty"`
}
//nolint:forcetypeassert
func initUseCaseServer(t *testing.T) *Server {
t.Helper()
var (
s, e = InitServer(
CheckIsUp(), SetPrefix("/api"),
WithHandlers(func(next HandlerFunc) HandlerFunc {
return HandlerFunc(func(c Context) error {
cc := customContext{c, "turlu"}
return next(cc)
})
}),
)
routes = RoutesPerPrefix{
"/hello": {
{
Verbe: "GET", Path: "", Handler: func(c Context) error {
return c.JSONBlob(http.StatusOK, []byte(`{ "message": "hello world" }`))
},
},
{
Verbe: "GET", Path: "/{who}", Handler: func(c Context) error {
content := `{ "message": "hello ` + c.GetVar("who") + `" }`
return c.JSONBlob(http.StatusOK, []byte(content))
},
},
},
"/test": {
{
Verbe: "GET", Path: "query", Handler: func(c Context) error {
return c.JSONOk(c.GetQuery())
},
},
{
Verbe: "GET", Path: "Context", Handler: func(c Context) error {
return c.JSONBlob(http.StatusOK, []byte(`{ "message": "hello `+
c.(customContext).Value+`" }`))
},
},
{
Verbe: "GET", Path: "/queryToStruct", Handler: func(c Context) error {
qp := queryParam{}
if e := c.DecodeAndValidateQP(&qp); e != nil {
return e
}
return c.JSONOk(qp)
},
},
},
"": {
{
Verbe: "GET", Path: "/routes", Handler: func(c Context) error {
return c.JSON(http.StatusOK, &testSerial{"hello"})
},
},
{
Verbe: "POST", Path: "/world", Handler: func(c Context) error {
anonymous := userForm{}
if e := c.FetchAndValidateContent(&anonymous); e != nil {
return e
}
return c.JSONCreated(anonymous)
},
},
},
}
)
require.Nil(t, e)
s.RouteApplier(routes)
return s
}
func TestUseCase(t *testing.T) {
s := initUseCaseServer(t)
require.Nil(t, RegisterValidatorRule("custom", func(fi validator.FieldLevel) bool {
return fi.Field().String() != "fail"
}))
require.Nil(t, RegisterValidatorTrans("custom", "'{0} is invalid :)"))
// RegisterValidatorAlias("alpha", "letters")
defer func() { require.Nil(t, s.ShutdownAndWait()) }()
go s.Start(_testPort)
<-s.isReady
const (
_reqNTest = iota
_pushNTest
_pushNTestContain
_deleteNTest
// _patchNTest
// _putNTest
)
tests := map[string]struct {
headers [][2]string
url string
body string
pushContent []byte
action int
code int
header bool
bodyDiffer bool
}{
"hello world": {
action: _reqNTest, header: true, url: "/api/hello",
body: `{"message":"hello world"}`, code: http.StatusOK,
},
"not found": {
action: _reqNTest, header: true, url: "/api/undef",
body: `{"status":404,"message":"not found"}`, code: http.StatusNotFound,
},
"not allowed": {
action: _deleteNTest, header: true, url: "/api/hello",
body: `{"status":405,"message":"method not allowed"}`, code: http.StatusMethodNotAllowed,
},
"simple fetch": {
action: _reqNTest, url: "/api/routes",
body: `{"test":"hello"}`, code: http.StatusOK,
},
"url params": {
action: _reqNTest, url: "/api/hello/you",
body: `{"message":"hello you"}`, code: http.StatusOK,
},
"query params": {
action: _reqNTest, bodyDiffer: true, url: "/api/testquery?pretty=1",
body: `{"pretty":["1"]}`, code: http.StatusOK,
},
"query to struct": {
action: _reqNTest, url: "/api/test/queryToStruct",
body: `{"pretty":false}`, code: http.StatusOK,
},
"query to struct invalide value": {
action: _reqNTest, url: "/api/test/queryToStruct?some=-5",
body: `{"message":{"some":"some must be -1 or greater"},"status":422}`, code: http.StatusUnprocessableEntity,
},
"query to struct invalide field": {
action: _reqNTest, url: "/api/test/queryToStruct?else=true",
body: `{"message":"schema: invalid path \"else\"","status":422}`, code: http.StatusUnprocessableEntity,
},
"query to struct filled": {
action: _reqNTest, url: "/api/test/queryToStruct?some=10&pretty",
body: "{\n \"some\": 10,\n \"pretty\": false\n}", code: http.StatusOK,
},
"query params pretty": {
action: _reqNTest, url: "/api/testquery?pretty",
code: http.StatusOK, body: `{}`,
},
"context": {
action: _reqNTest, bodyDiffer: true, url: "/api/testContext",
body: `{"message":"hello tutu"}`, code: http.StatusOK,
},
"push": {
action: _pushNTest, url: "/api/world", pushContent: []byte(`{"first_name":"jean", "last_name":"claude"}`),
body: `{"first_name":"jean","last_name":"claude"}`, code: http.StatusCreated,
},
"push_wrong_header": {
action: _pushNTest, url: "/api/world", pushContent: []byte(`{"first_name":"jean", "last_name":"claude"}`),
headers: [][2]string{{"Content-Type", "plain-text"}},
body: `{"message":"Content-Type is not application/json","status":406}`, code: http.StatusNotAcceptable,
},
"push_form_miss_field": {
action: _pushNTestContain, url: "/api/world", pushContent: []byte(`{"first_name":"jean"}`),
body: `last_name is a required field`, code: http.StatusUnprocessableEntity,
},
"push_invalid_empty": {
action: _pushNTest, url: "/api/world", pushContent: []byte(`{}`), bodyDiffer: true,
body: `{}`, code: http.StatusUnprocessableEntity,
},
"push_invalid_wrong": {
action: _pushNTest, url: "/api/world", pushContent: []byte(`{`),
body: UnprocessablePayloadErrorStr, code: http.StatusUnprocessableEntity,
},
"push_custom": {
action: _pushNTest, url: "/api/world", pushContent: []byte(`{"first_name":"uno", "last_name":"fail"}`),
body: `{"message":{"last_name":"'last_name is invalid :)"},"status":422}`, code: http.StatusUnprocessableEntity,
},
// TODO: test GET/DELETE/PATCH/PUT ?
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
// t.Helper()
switch test.action {
case _reqNTest:
webtest.RequestAndTestAPI(t, _testAddr+test.url,
func(t *testing.T, resp *http.Response) {
t.Helper()
if test.header {
for _, testVal := range []string{"Content-Type", "Accept", "Produce"} {
webtest.Header(t, testVal, jsonEncode, resp)
}
}
if test.bodyDiffer {
webtest.BodyDiffere(t, test.body, resp)
} else {
webtest.Body(t, test.body, resp)
}
webtest.StatusCode(t, test.code, resp)
})
case _deleteNTest:
webtest.DeleteAndTestAPI(t, _testAddr+test.url,
func(t *testing.T, resp *http.Response) {
t.Helper()
webtest.Body(t, test.body, resp)
webtest.StatusCode(t, test.code, resp)
})
case _pushNTest:
webtest.PushAndTestAPI(t, _testAddr+test.url, test.pushContent,
func(t *testing.T, resp *http.Response) {
t.Helper()
if test.bodyDiffer {
webtest.BodyDiffere(t, test.body, resp)
} else {
webtest.Body(t, test.body, resp)
}
webtest.StatusCode(t, test.code, resp)
}, test.headers...)
case _pushNTestContain:
webtest.PushAndTestAPI(t, _testAddr+test.url, test.pushContent,
func(t *testing.T, resp *http.Response) {
t.Helper()
webtest.BodyContains(t, test.body, resp)
webtest.StatusCode(t, test.code, resp)
}, test.headers...)
}
})
}
}