-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
165 lines (155 loc) · 4.49 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
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
package slide
import (
"errors"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type MiddlewareSuite struct {
suite.Suite
Slide *Slide
}
func (suite *MiddlewareSuite) SetupTest() {
config := &Config{}
app := InitServer(config)
suite.Slide = app
}
func (suite *MiddlewareSuite) TestAppLevelMiddleware() {
path := "/hey"
response := "hello, world!"
suite.Slide.Use(func(ctx *Ctx) error {
return ctx.Send(http.StatusOK, "response from middleware")
})
suite.Slide.Get(path, func(ctx *Ctx) error {
return ctx.Send(http.StatusOK, response)
})
// first send early response
r, err := http.NewRequest(GET, "http://test"+path, nil)
if assert.Nil(suite.T(), err) {
res, err := testServer(r, suite.Slide)
if assert.Nil(suite.T(), err) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
suite.T().Error(err)
}
assert.Equal(suite.T(), res.StatusCode, http.StatusOK)
assert.Equal(suite.T(), string(body), "response from middleware")
}
}
}
func (suite *MiddlewareSuite) TestAppLevelMultiMiddleware() {
path := "/hey"
response := "hello, world!"
suite.Slide.Use(func(ctx *Ctx) error {
ctx.RequestCtx.Response.Header.Set("hey1", "hey1")
return ctx.Next()
})
suite.Slide.Use(func(ctx *Ctx) error {
ctx.RequestCtx.Response.Header.Set("hey2", "hey2")
return ctx.Next()
})
suite.Slide.Get(path, func(ctx *Ctx) error {
return ctx.Send(http.StatusOK, response)
})
// first send early response
r, err := http.NewRequest(GET, "http://test"+path, nil)
if assert.Nil(suite.T(), err) {
res, err := testServer(r, suite.Slide)
if assert.Nil(suite.T(), err) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
suite.T().Error(err)
}
assert.Equal(suite.T(), res.StatusCode, http.StatusOK)
assert.Equal(suite.T(), res.Header.Get("hey1"), "hey1")
assert.Equal(suite.T(), res.Header.Get("hey2"), "hey2")
assert.Equal(suite.T(), string(body), response)
}
}
}
func (suite *MiddlewareSuite) TestGroupMultiMiddleware() {
path := "/hey"
response := "hello, world!"
groupPath := "/group"
group := suite.Slide.Group(groupPath)
group.Use(func(ctx *Ctx) error {
ctx.RequestCtx.Response.Header.Set("hey1", "hey1")
return ctx.Next()
})
group.Use(func(ctx *Ctx) error {
ctx.RequestCtx.Response.Header.Set("hey2", "hey2")
return ctx.Next()
})
group.Get(path, func(ctx *Ctx) error {
return ctx.Send(http.StatusOK, response)
})
// first send early response
r, err := http.NewRequest(GET, "http://test"+groupPath+path, nil)
if assert.Nil(suite.T(), err) {
res, err := testServer(r, suite.Slide)
if assert.Nil(suite.T(), err) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
suite.T().Error(err)
}
assert.Equal(suite.T(), res.StatusCode, http.StatusOK)
assert.Equal(suite.T(), res.Header.Get("hey1"), "hey1")
assert.Equal(suite.T(), res.Header.Get("hey2"), "hey2")
assert.Equal(suite.T(), string(body), response)
}
}
}
func (suite *MiddlewareSuite) TestAppLevelMiddlewareError() {
path := "/hey"
response := "hello, world!"
suite.Slide.Use(func(ctx *Ctx) error {
return errors.New("error from middleware")
})
suite.Slide.Get(path, func(ctx *Ctx) error {
return ctx.Send(http.StatusOK, response)
})
// first send early response
r, err := http.NewRequest(GET, "http://test"+path, nil)
if assert.Nil(suite.T(), err) {
res, err := testServer(r, suite.Slide)
if assert.Nil(suite.T(), err) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
suite.T().Error(err)
}
assert.Equal(suite.T(), res.StatusCode, http.StatusInternalServerError)
assert.Equal(suite.T(), string(body), "error from middleware")
}
}
}
func (suite *MiddlewareSuite) TestAppLevelMiddlewareMultiError() {
path := "/hey"
response := "hello, world!"
suite.Slide.Use(func(ctx *Ctx) error {
return ctx.Next()
})
suite.Slide.Use(func(ctx *Ctx) error {
return errors.New("error from middleware")
})
suite.Slide.Get(path, func(ctx *Ctx) error {
return ctx.Send(http.StatusOK, response)
})
// first send early response
r, err := http.NewRequest(GET, "http://test"+path, nil)
if assert.Nil(suite.T(), err) {
res, err := testServer(r, suite.Slide)
if assert.Nil(suite.T(), err) {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
suite.T().Error(err)
}
assert.Equal(suite.T(), res.StatusCode, http.StatusInternalServerError)
assert.Equal(suite.T(), string(body), "error from middleware")
}
}
}
func TestMiddleware(t *testing.T) {
suite.Run(t, new(MiddlewareSuite))
}