-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathquick_post_test.go
352 lines (327 loc) · 10 KB
/
quick_post_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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Package quick provides a high-performance, minimalistic web framework for Go.
//
// This file contains **unit tests** for various functionalities of the Quick framework.
// These tests ensure that the core features of Quick work as expected.
//
// 📌 To run all unit tests, use:
//
// $ go test -v ./...
// $ go test -v
package quick
import (
"encoding/json"
"encoding/xml"
"net/http"
"reflect"
"strings"
"testing"
"github.com/jeffotoni/quick/internal/concat"
)
// TestRoutePOST validates a basic POST route using the Quick framework.
//
// This test registers a route `/v1/user` and verifies:
// - If the response status code is 200 (OK)
// - If the body contains the expected string "Data submitted!"
//
// Useful to ensure that POST routes are working with default behaviors and that `Qtest` handles basic POST requests correctly.
//
// Coverage commands:
// - cover: go test -v -count=1 -cover -failfast -run ^TestRoutePOST$
// - coverHTML: go test -v -count=1 -failfast -cover -coverprofile=coverage.out -run ^TestRoutePOST$; go tool cover -html=coverage.out
func TestRoutePOST(t *testing.T) {
q := New()
q.Post("/v1/user", func(c *Ctx) error {
return c.String("Data submitted!")
})
// Simulate a GET request to "/api/users"
data, err := q.Qtest(QuickTestOptions{
Method: MethodPost,
URI: "/v1/user",
Headers: map[string]string{"Content-Type": "application/json"},
})
if err != nil {
t.Errorf("Error during Qtest: %v", err)
return
}
if data.StatusCode() != 200 {
t.Errorf("Expected status 200, got %d", data.StatusCode())
}
if data.BodyStr() != "Data submitted!" {
t.Errorf("Expected body 'Data submitted!', got '%s'", data.BodyStr())
}
}
// TestQuick_Post validates multiple scenarios for POST requests using the Quick framework.
//
// This test uses various handlers to simulate different parsing behaviors (e.g., body string, body parsing with JSON/XML, and binding).
// It ensures the framework can:
// - Parse and respond with JSON bodies
// - Handle routes with and without parameters
// - Parse XML inputs correctly
// - Handle empty bodies, arrays, and different content types
//
// Each subtest defines expected status codes and response outputs based on the route and payload.
//
// Coverage commands:
// - cover: go test -v -count=1 -cover -failfast -run ^TestQuick_Post$
// - coverHTML: go test -v -count=1 -failfast -cover -coverprofile=coverage.out -run ^TestQuick_Post$; go tool cover -html=coverage.out
func TestQuick_Post(t *testing.T) {
type args struct {
route string
wantCode int
wantOut string
isWantedErr bool
reqBody []byte
reqHeaders map[string]string
}
type myType struct {
Name string `json:"name"`
Age int `json:"age"`
}
type XmlData struct {
XMLName xml.Name `xml:"data"`
Name string `xml:"name"`
Age int `xml:"age"`
}
type myXmlType struct {
XMLName xml.Name `xml:"MyXMLType"`
Data XmlData `xml:"data"`
}
testSuccessMockHandler := func(c *Ctx) error {
c.Set("Content-Type", "application/json")
b := c.Body()
resp := concat.String(`"data":`, string(b))
c.Status(200)
return c.SendString(resp)
}
testSuccessMockHandlerBodyStr := func(c *Ctx) error {
c.Set("Content-Type", "application/json")
resp := concat.String(`"data":`, c.BodyString())
c.Status(200)
return c.SendString(resp)
}
testSuccessMockHandlerString := func(c *Ctx) error {
c.Set("Content-Type", "application/json")
mt := new(myType)
if err := c.BodyParser(mt); err != nil {
t.Errorf("error: %v", err)
}
b, _ := json.Marshal(mt)
resp := concat.String(`"data":`, string(b))
c.Status(200)
return c.String(resp)
}
testSuccessMockHandlerBind := func(c *Ctx) error {
c.Set("Content-Type", "application/json")
mt := new(myType)
if err := c.Bind(&mt); err != nil {
t.Errorf("error: %v", err)
}
b, _ := json.Marshal(mt)
resp := concat.String(`"data":`, string(b))
c.Status(200)
return c.String(resp)
}
testSuccessMockXml := func(c *Ctx) error {
c.Set("Content-Type", ContentTypeTextXML)
mtx := new(myXmlType)
if err := c.Bind(&mtx); err != nil {
t.Errorf("error: %v", err)
}
return c.Status(200).XML(mtx)
}
r := New()
r.Post("/test", testSuccessMockHandler)
r.Post("/testStr", testSuccessMockHandlerBodyStr)
r.Post("/tester/:p1", testSuccessMockHandler)
r.Post("/", testSuccessMockHandlerString)
r.Post("/bind", testSuccessMockHandlerBind)
r.Post("/test/xml", testSuccessMockXml)
tests := []struct {
name string
args args
}{
{
name: "success",
args: args{
route: "/test",
wantCode: 200,
wantOut: `"data":{"name":"jeff", "age":35}`,
isWantedErr: false,
reqBody: []byte(`{"name":"jeff", "age":35}`),
},
},
{
name: "success_body_str",
args: args{
route: "/testStr",
wantCode: 200,
wantOut: `"data":{"name":"jeff", "age":35}`,
isWantedErr: false,
reqBody: []byte(`{"name":"jeff", "age":35}`),
},
},
{
name: "success_param",
args: args{
route: "/tester/some",
wantCode: 200,
wantOut: `"data":{"name":"jeff", "age":35}`,
isWantedErr: false,
reqBody: []byte(`{"name":"jeff", "age":35}`),
},
},
{
name: "success_without_param",
args: args{
route: "/",
wantCode: 200,
wantOut: `"data":{"name":"jeff","age":35}`,
isWantedErr: false,
reqBody: []byte(`{"name":"jeff","age":35}`),
reqHeaders: map[string]string{"Content-Type": "application/json"},
},
},
{
name: "success_bind",
args: args{
route: "/bind",
wantCode: 200,
wantOut: `"data":{"name":"jeff","age":35}`,
isWantedErr: false,
reqBody: []byte(`{"name":"jeff","age":35}`),
reqHeaders: map[string]string{"Content-Type": "application/json"},
},
},
{
name: "success_xml",
args: args{
route: "/test/xml",
wantCode: 200,
wantOut: `<MyXMLType><data><name>Jeff</name><age>35</age></data></MyXMLType>`,
isWantedErr: false,
reqBody: []byte(`<MyXMLType><data><name>Jeff</name><age>35</age></data></MyXMLType>`),
reqHeaders: map[string]string{"Content-Type": ContentTypeTextXML},
},
},
{
name: "success_different_valid_json",
args: args{
route: "/test",
wantCode: 200,
wantOut: `"data":{"title":"Test","status":true}`,
isWantedErr: false,
reqBody: []byte(`{"title":"Test","status":true}`),
reqHeaders: map[string]string{"Content-Type": "application/json"},
},
},
{
name: "success_empty_body",
args: args{
route: "/test",
wantCode: 200,
wantOut: `"data":{}`,
isWantedErr: false,
reqBody: []byte(`{}`),
reqHeaders: map[string]string{"Content-Type": "application/json"},
},
},
{
name: "success_json_with_numbers",
args: args{
route: "/test",
wantCode: 200,
wantOut: `"data":{"value":12345,"percentage":99.9}`,
isWantedErr: false,
reqBody: []byte(`{"value":12345,"percentage":99.9}`),
reqHeaders: map[string]string{"Content-Type": "application/json"},
},
},
{
name: "success_xml_with_different_data",
args: args{
route: "/test/xml",
wantCode: 200,
wantOut: `<MyXMLType><data><name>Maria</name><age>28</age></data></MyXMLType>`,
isWantedErr: false,
reqBody: []byte(`<MyXMLType><data><name>Maria</name><age>28</age></data></MyXMLType>`),
reqHeaders: map[string]string{"Content-Type": "application/xml"},
},
},
{
name: "success_longer_json",
args: args{
route: "/test",
wantCode: 200,
wantOut: `"data":{"name":"jeff","age":35,"city":"São Paulo","country":"Brazil"}`,
isWantedErr: false,
reqBody: []byte(`{"name":"jeff","age":35,"city":"São Paulo","country":"Brazil"}`),
reqHeaders: map[string]string{"Content-Type": "application/json"},
},
},
{
name: "success_json_with_array",
args: args{
route: "/test",
wantCode: 200,
wantOut: `"data":{"items":["item1","item2","item3"]}`,
isWantedErr: false,
reqBody: []byte(`{"items":["item1","item2","item3"]}`),
reqHeaders: map[string]string{"Content-Type": "application/json"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := r.Qtest(QuickTestOptions{
Method: "POST",
URI: tt.args.route,
Headers: tt.args.reqHeaders,
Body: tt.args.reqBody,
})
if (!tt.args.isWantedErr) && err != nil {
t.Errorf("error: %v", err)
return
}
s := strings.TrimSpace(data.BodyStr())
if s != tt.args.wantOut {
t.Errorf("was suppose to return %s and %s come", tt.args.wantOut, data.BodyStr())
return
}
if tt.args.wantCode != data.StatusCode() {
t.Errorf("was suppose to return %d and %d come", tt.args.wantCode, data.StatusCode())
return
}
t.Logf("outputBody -> %v", data.BodyStr())
})
}
}
// Test_extractParamsPost validates the behavior of `extractParamsPost`, responsible for generating an HTTP handler for POST requests.
//
// This test is a placeholder using table-driven testing. It prepares the structure to test different combinations of `Quick` instances
// and handler functions. It uses `reflect.DeepEqual` to compare the expected and returned handler functions.
//
// Note: Test cases should be added to fully cover parsing logic, route matching, and error handling.
//
// Coverage commands:
// - cover: go test -v -count=1 -cover -failfast -run ^Test_extractParamsPost$
// - coverHTML: go test -v -count=1 -failfast -cover -coverprofile=coverage.out -run ^Test_extractParamsPost$; go tool cover -html=coverage.out
func Test_extractParamsPost(t *testing.T) {
type args struct {
quick Quick
handlerFunc func(*Ctx) error
}
tests := []struct {
name string
args args
want http.HandlerFunc
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := extractParamsPost(&tt.args.quick, tt.args.handlerFunc); !reflect.DeepEqual(got, tt.want) {
t.Errorf("extractParamsPost() = %v, want %v", got, tt.want)
}
})
}
}