-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose_test.go
125 lines (103 loc) · 2.52 KB
/
compose_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
package compose
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type ComposeSuite struct {
suite.Suite
m1 bool
m1t time.Time
m2 bool
m2t time.Time
h bool
req *http.Request
res http.ResponseWriter
start time.Time
}
func (s *ComposeSuite) SetupTest() {
s.start = time.Now()
s.req = httptest.NewRequest(http.MethodGet, "/", nil)
s.res = testResponseWriter{}
s.m1, s.m2 = false, false
s.m1t, s.m2t = s.start, s.start
}
func (s *ComposeSuite) TestOneMiddleware() {
New(http.HandlerFunc(s.handler)).Use(
middleware1, s,
).Handler().ServeHTTP(s.res, s.req)
s.True(s.h)
s.True(s.m1)
s.False(s.m2)
s.True(s.m1t.After(s.start))
s.True(s.m2t.Equal(s.start))
}
func (s *ComposeSuite) TestManyMiddlewares() {
New(http.HandlerFunc(s.handler)).Use(
middleware1, s,
).Use(
middleware2, s,
).Handler().ServeHTTP(s.res, s.req)
s.True(s.h)
s.True(s.m1)
s.True(s.m2)
s.True(s.m1t.After(s.start))
s.True(s.m2t.After(s.start))
}
func (s *ComposeSuite) TestUseMiddlewares() {
New(http.HandlerFunc(s.handler)).UseMiddlewares(
[]Middleware{
Middleware{Fun: middleware1, Opts: []interface{}{s}},
Middleware{Fun: middleware2, Opts: []interface{}{s}},
},
).Handler().ServeHTTP(s.res, s.req)
s.True(s.h)
s.True(s.m1)
s.True(s.m2)
s.True(s.m1t.After(s.start))
s.True(s.m2t.After(s.start))
}
func (s *ComposeSuite) TestArgumentsPanic() {
s.Panics(func() {
New(http.HandlerFunc(s.handler)).Use(
func(s string) {}, s,
).Handler().ServeHTTP(s.res, s.req)
})
}
func (s *ComposeSuite) TestReturnValuePanic() {
s.Panics(func() {
New(http.HandlerFunc(s.handler)).Use(
func(h http.Handler) {}, s,
).Handler().ServeHTTP(s.res, s.req)
})
}
func (s *ComposeSuite) handler(res http.ResponseWriter, req *http.Request) {
s.h = true
}
func TestCompose(t *testing.T) {
suite.Run(t, new(ComposeSuite))
}
func middleware1(h http.Handler, cs *ComposeSuite) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
cs.m1 = true
cs.m1t = time.Now()
h.ServeHTTP(res, req)
})
}
func middleware2(h http.Handler, cs *ComposeSuite) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
cs.m2 = true
cs.m2t = time.Now()
h.ServeHTTP(res, req)
})
}
type testResponseWriter struct{}
func (t testResponseWriter) Header() http.Header {
return make(http.Header)
}
func (t testResponseWriter) WriteHeader(s int) {}
func (t testResponseWriter) Write(b []byte) (int, error) {
return 0, nil
}