-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_test.go
652 lines (550 loc) · 17.1 KB
/
web_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
* Copyright (c) 2024 Go IoC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/
package web
import (
"bytes"
"context"
"github.com/goioc/di"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
htmlTemplate "html/template"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
textTemplate "text/template"
)
var server *httptest.Server
type endpoint1 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint1"`
}
func (e endpoint1) HandlerFuncName() string {
return "REST"
}
func (e *endpoint1) REST(w http.ResponseWriter) {
_, err := w.Write([]byte("test"))
if err != nil {
panic(err)
}
}
type endpoint2 struct {
method interface{} `web.methods:"post,patch"`
path interface{} `web.path:"/endpoint2"`
}
func (e endpoint2) HandlerFuncName() string {
return "REST"
}
func (e *endpoint2) REST(w http.ResponseWriter, r *http.Request) {
all, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
_, err = w.Write(all)
if err != nil {
panic(err)
}
}
type endpoint3 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint3"`
queries interface{} `web.queries:"foo,bar,id,{id:[0-9]+}"`
}
func (e endpoint3) HandlerFuncName() string {
return "REST"
}
func (e *endpoint3) REST(queryParams url.Values) string {
foo := queryParams.Get("foo")
id := queryParams.Get("id")
return foo + id
}
type endpoint4 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint4"`
headers interface{} `web.headers:"Content-Type,text/plain"`
}
func (e endpoint4) HandlerFuncName() string {
return "REST"
}
func (e *endpoint4) REST(body string) string {
return body
}
type endpoint5 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint5/{key}/{*?}"`
matcher interface{} `web.matcher:"matcher"`
}
func (e endpoint5) HandlerFuncName() string {
return "REST"
}
func (e *endpoint5) REST(pathParams map[string]string) string {
return pathParams["key"]
}
type endpoint6 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint6"`
}
func (e endpoint6) HandlerFuncName() string {
return "REST"
}
func (e *endpoint6) REST(ctx context.Context) string {
return ctx.Value(di.BeanKey("key")).(string)
}
type endpoint7 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint7"`
}
func (e endpoint7) HandlerFuncName() string {
return "REST"
}
func (e *endpoint7) REST(header http.Header) string {
return header.Get("Content-Type")
}
type endpoint8 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint8"`
}
func (e endpoint8) HandlerFuncName() string {
return "REST"
}
func (e *endpoint8) REST(reader io.Reader) string {
all, err := ioutil.ReadAll(reader)
if err != nil {
panic(err)
}
return string(all)
}
type endpoint9 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint9"`
}
func (e endpoint9) HandlerFuncName() string {
return "REST"
}
func (e *endpoint9) REST(readCloser io.ReadCloser) string {
all, err := ioutil.ReadAll(readCloser)
if err != nil {
panic(err)
}
return string(all)
}
type endpoint10 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint10"`
}
func (e endpoint10) HandlerFuncName() string {
return "REST"
}
func (e *endpoint10) REST(body []byte) string {
return string(body)
}
type binaryStruct struct {
a string
}
func (b *binaryStruct) MarshalBinary() (data []byte, err error) {
return []byte(b.a), nil
}
func (b *binaryStruct) UnmarshalBinary(data []byte) error {
b.a = string(data)
return nil
}
type endpoint11 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint11"`
}
func (e endpoint11) HandlerFuncName() string {
return "REST"
}
func (e *endpoint11) REST(body binaryStruct) *binaryStruct {
return &body
}
type textStruct struct {
a string
}
func (t *textStruct) MarshalText() (data []byte, err error) {
return []byte(t.a), nil
}
func (t *textStruct) UnmarshalText(data []byte) error {
t.a = string(data)
return nil
}
type endpoint12 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint12"`
}
func (e endpoint12) HandlerFuncName() string {
return "REST"
}
func (e *endpoint12) REST(body textStruct) *textStruct {
return &body
}
type endpoint13 struct {
method interface{} `web.methods:"POST"`
path interface{} `web.path:"/endpoint13"`
}
func (e endpoint13) HandlerFuncName() string {
return "REST"
}
func (e *endpoint13) REST(body outerStruct) outerStruct {
return body
}
type endpoint14 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint14"`
}
func (e endpoint14) HandlerFuncName() string {
return "REST"
}
func (e *endpoint14) REST() (http.Header, int) {
return map[string][]string{
"my-header": {"my-header-value"},
}, 418
}
type endpoint15 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint15"`
}
func (e endpoint15) HandlerFuncName() string {
return "REST"
}
func (e *endpoint15) REST() []byte {
return []byte("test")
}
type endpoint16 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint16"`
}
func (e endpoint16) HandlerFuncName() string {
return "REST"
}
func (e *endpoint16) REST() io.Reader {
return bytes.NewBufferString("test")
}
type endpoint17 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint17"`
}
func (e endpoint17) HandlerFuncName() string {
return "REST"
}
func (e *endpoint17) REST() io.ReadCloser {
return ioutil.NopCloser(bytes.NewBufferString("test"))
}
const htmlTmpl = `<h1>{{.PageTitle}}</h1>
<ul>
{{range .Todos}}
{{if .Done}}
<li class="done">{{.Title}}</li>
{{else}}
<li>{{.Title}}</li>
{{end}}
{{end}}
</ul>`
const htmlPage = `<h1>My TODO list</h1>
<ul>
<li>Task 1</li>
<li class="done">Task 2</li>
<li class="done">Task 3</li>
</ul>`
type todo struct {
Title string
Done bool
}
type todoPageData struct {
PageTitle string
Todos []todo
}
type endpoint18 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint18"`
}
func (e endpoint18) HandlerFuncName() string {
return "REST"
}
func (e *endpoint18) REST() (htmlTemplate.Template, interface{}) {
tmpl := htmlTemplate.Must(htmlTemplate.New("test").Parse(htmlTmpl))
return *tmpl, todoPageData{
PageTitle: "My TODO list",
Todos: []todo{
{Title: "Task 1", Done: false},
{Title: "Task 2", Done: true},
{Title: "Task 3", Done: true},
},
}
}
type endpoint19 struct {
method interface{} `web.methods:"GET"`
path interface{} `web.path:"/endpoint19"`
}
func (e endpoint19) HandlerFuncName() string {
return "REST"
}
func (e *endpoint19) REST() (textTemplate.Template, interface{}) {
tmpl := textTemplate.Must(textTemplate.New("test").Parse(htmlTmpl))
return *tmpl, todoPageData{
PageTitle: "My TODO list",
Todos: []todo{
{Title: "Task 1", Done: false},
{Title: "Task 2", Done: true},
{Title: "Task 3", Done: true},
},
}
}
type TestSuite struct {
suite.Suite
}
func (suite *TestSuite) SetupSuite() {
_, err := di.RegisterBeanFactory("matcher", di.Singleton, func(ctx context.Context) (interface{}, error) {
matcherFunc := mux.MatcherFunc(func(request *http.Request, match *mux.RouteMatch) bool {
return strings.HasSuffix(request.URL.Path, "bar")
})
return &matcherFunc, nil
})
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint1", reflect.TypeOf((*endpoint1)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint2", reflect.TypeOf((*endpoint2)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint3", reflect.TypeOf((*endpoint3)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint4", reflect.TypeOf((*endpoint4)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint5", reflect.TypeOf((*endpoint5)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint6", reflect.TypeOf((*endpoint6)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint7", reflect.TypeOf((*endpoint7)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint8", reflect.TypeOf((*endpoint8)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint9", reflect.TypeOf((*endpoint9)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint10", reflect.TypeOf((*endpoint10)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint11", reflect.TypeOf((*endpoint11)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint12", reflect.TypeOf((*endpoint12)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint13", reflect.TypeOf((*endpoint13)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint14", reflect.TypeOf((*endpoint14)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint15", reflect.TypeOf((*endpoint15)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint16", reflect.TypeOf((*endpoint16)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint17", reflect.TypeOf((*endpoint17)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint18", reflect.TypeOf((*endpoint18)(nil)))
assert.NoError(suite.T(), err)
_, err = di.RegisterBean("endpoint19", reflect.TypeOf((*endpoint19)(nil)))
assert.NoError(suite.T(), err)
err = di.InitializeContainer()
assert.NoError(suite.T(), err)
Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), di.BeanKey("key"), "value")))
})
})
router, err := CreateRouter()
assert.NoError(suite.T(), err)
server = httptest.NewServer(router)
}
func (suite *TestSuite) TearDownSuite() {
server.Close()
}
func TestWebTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}
func (suite *TestSuite) TestEndpoint1() {
response, err := http.Get(server.URL + "/endpoint1")
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint2() {
buf := bytes.NewBufferString("test")
response, err := http.Post(server.URL+"/endpoint2", "", buf)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
buf = bytes.NewBufferString("test")
request, err := http.NewRequest(http.MethodPatch, server.URL+"/endpoint2", buf)
assert.NotNil(suite.T(), request)
assert.NoError(suite.T(), err)
client := http.Client{}
assert.NotNil(suite.T(), client)
response, err = client.Do(request)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err = ioutil.ReadAll(response.Body)
assert.NotNil(suite.T(), all)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint3() {
response, err := http.Get(server.URL + "/endpoint3?foo=test&id=42")
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 404, response.StatusCode)
response, err = http.Get(server.URL + "/endpoint3?foo=bar&id=42")
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "bar42", string(all))
}
func (suite *TestSuite) TestEndpoint4() {
response, err := http.Post(server.URL+"/endpoint4", "", nil)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 404, response.StatusCode)
buf := bytes.NewBufferString("test")
response, err = http.Post(server.URL+"/endpoint4", "text/plain", buf)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint5() {
response, err := http.Get(server.URL + "/endpoint5/foo/baz")
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 404, response.StatusCode)
response, err = http.Get(server.URL + "/endpoint5/foo/bar")
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "foo", string(all))
}
func (suite *TestSuite) TestEndpoint6() {
response, err := http.Get(server.URL + "/endpoint6")
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "value", string(all))
}
func (suite *TestSuite) TestEndpoint7() {
response, err := http.Post(server.URL+"/endpoint7", "application/json", nil)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "application/json", string(all))
}
func (suite *TestSuite) TestEndpoint8() {
buf := bytes.NewBufferString("test")
response, err := http.Post(server.URL+"/endpoint8", "", buf)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint9() {
buf := bytes.NewBufferString("test")
response, err := http.Post(server.URL+"/endpoint9", "", buf)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint10() {
buf := bytes.NewBufferString("test")
response, err := http.Post(server.URL+"/endpoint10", "", buf)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint11() {
buf := bytes.NewBufferString("test")
response, err := http.Post(server.URL+"/endpoint11", "", buf)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint12() {
buf := bytes.NewBufferString("test")
response, err := http.Post(server.URL+"/endpoint12", "", buf)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint13() {
buf := bytes.NewBufferString(jsonData)
response, err := http.Post(server.URL+"/endpoint13", "", buf)
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), jsonData, string(all))
}
func (suite *TestSuite) TestEndpoint14() {
response, err := http.Get(server.URL + "/endpoint14")
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 418, response.StatusCode)
assert.Equal(suite.T(), "my-header-value", response.Header.Get("my-header"))
}
func (suite *TestSuite) TestEndpoint15() {
response, err := http.Get(server.URL + "/endpoint15")
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint16() {
response, err := http.Get(server.URL + "/endpoint16")
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint17() {
response, err := http.Get(server.URL + "/endpoint17")
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "test", string(all))
}
func (suite *TestSuite) TestEndpoint18() {
response, err := http.Get(server.URL + "/endpoint18")
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), htmlPage, string(all))
}
func (suite *TestSuite) TestEndpoint19() {
response, err := http.Get(server.URL + "/endpoint19")
assert.NotNil(suite.T(), response)
assert.NoError(suite.T(), err)
all, err := ioutil.ReadAll(response.Body)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), htmlPage, string(all))
}