This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
104 lines (84 loc) · 2.21 KB
/
middleware_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
package partial
import (
"net/http"
"testing"
"github.com/DavidCai1993/request"
"github.com/stretchr/testify/suite"
"github.com/teambition/gear"
)
const (
Addr = ":28080"
Addr2 = ":28081"
)
type MiddlewareSuite struct {
suite.Suite
app *gear.App
defualtQueryApp *gear.App
}
type S1 struct {
A *string `json:"a"`
B *int `json:"b"`
C *float64 `json:"c"`
}
func (s *MiddlewareSuite) SetupSuite() {
s.app = gear.New()
s.app.Set(gear.SetSender, New(Options{Query: "c"}))
s.app.Use(func(ctx *gear.Context) error {
a := "aaa"
b := 3
c := float64(2.2)
return ctx.Send(http.StatusOK, S1{A: &a, B: &b, C: &c})
})
go func() {
s.Require().Nil(s.app.Listen(Addr))
}()
s.defualtQueryApp = gear.New()
s.defualtQueryApp.Set(gear.SetSender, New(Options{}))
s.defualtQueryApp.Use(func(ctx *gear.Context) error {
a := "aaa"
b := 3
c := float64(2.2)
return ctx.Send(http.StatusOK, S1{A: &a, B: &b, C: &c})
})
go func() {
s.Require().Nil(s.defualtQueryApp.Listen(Addr2))
}()
}
func (s *MiddlewareSuite) TestVaildQuery() {
json, err := request.Get("http://127.0.0.1" + Addr + "?c=a,c").JSON(new(S1))
s.Nil(err)
s.Equal(*(json.(*S1).A), "aaa")
s.Nil(json.(*S1).B)
s.Equal(*(json.(*S1).C), float64(2.2))
}
func (s *MiddlewareSuite) TestOtherQuery() {
json, err := request.Get("http://127.0.0.1" + Addr + "?c=e,f").JSON(new(S1))
s.Nil(err)
s.Nil(json.(*S1).A)
s.Nil(json.(*S1).B)
s.Nil(json.(*S1).C)
}
func (s *MiddlewareSuite) TestEmptyQuery() {
json, err := request.Get("http://127.0.0.1" + Addr + "?c=").JSON(new(S1))
s.Nil(err)
s.Equal(*(json.(*S1).A), "aaa")
s.Equal(*(json.(*S1).B), 3)
s.Equal(*(json.(*S1).C), float64(2.2))
}
func (s *MiddlewareSuite) TestWithoutQuery() {
json, err := request.Get("http://127.0.0.1" + Addr + "?d=d,v&&e=f").JSON(new(S1))
s.Nil(err)
s.Equal(*(json.(*S1).A), "aaa")
s.Equal(*(json.(*S1).B), 3)
s.Equal(*(json.(*S1).C), float64(2.2))
}
func (s *MiddlewareSuite) TestDefaultQueryApp() {
json, err := request.Get("http://127.0.0.1" + Addr2 + "?fields=a,c").JSON(new(S1))
s.Nil(err)
s.Equal(*(json.(*S1).A), "aaa")
s.Nil(json.(*S1).B)
s.Equal(*(json.(*S1).C), float64(2.2))
}
func TestMiddleware(t *testing.T) {
suite.Run(t, new(MiddlewareSuite))
}