-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm_test.go
187 lines (178 loc) · 4.84 KB
/
mm_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
package mm_test
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/k1LoW/httpstub"
"github.com/2manymws/mm"
)
type testBuilder struct {
buildFunc func(req *http.Request) (func(next http.Handler) http.Handler, bool)
}
func (b *testBuilder) Middleware(req *http.Request) (func(next http.Handler) http.Handler, bool) {
return b.buildFunc(req)
}
func TestMM(t *testing.T) {
// testHeaderMw is a test middleware
// Set "X-Test" header to all requests
testHeaderMw := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Test", "test")
next.ServeHTTP(w, r)
})
}
// testRewriteMw is a test middleware
testRewriteMw := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rr := httptest.NewRecorder()
next.ServeHTTP(rr, r)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Rewrited!"))
})
}
tests := []struct {
name string
builders []mm.Builder
req *http.Request
want *http.Response
wantBody string
}{
{
name: "No middleware",
builders: nil,
req: &http.Request{Method: http.MethodGet, URL: mustParseURL("http://example.com")},
want: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": {"text/plain; charset=utf-8"}},
},
wantBody: "Hello",
},
{
name: "Set testHeaderMw to all requests",
builders: []mm.Builder{
&testBuilder{
buildFunc: func(req *http.Request) (func(next http.Handler) http.Handler, bool) {
return testHeaderMw, true
},
},
},
req: &http.Request{Method: http.MethodGet, URL: mustParseURL("http://example.com")},
want: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": {"text/plain; charset=utf-8"},
"X-Test": {"test"},
},
},
wantBody: "Hello",
},
{
name: "Set testRewiteMw to all requests",
builders: []mm.Builder{
&testBuilder{
buildFunc: func(req *http.Request) (func(next http.Handler) http.Handler, bool) {
return testRewriteMw, true
},
},
},
req: &http.Request{Method: http.MethodGet, URL: mustParseURL("http://example.com")},
want: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": {"text/plain; charset=utf-8"},
},
},
wantBody: "Rewrited!",
},
{
name: "Set testHeaderMw to only GET requests (1/2)",
builders: []mm.Builder{
&testBuilder{
buildFunc: func(req *http.Request) (func(next http.Handler) http.Handler, bool) {
if req.Method != http.MethodHead {
return nil, false
}
return testHeaderMw, true
},
},
},
req: &http.Request{Method: http.MethodGet, URL: mustParseURL("http://example.com")},
want: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": {"text/plain; charset=utf-8"},
},
},
wantBody: "Hello",
},
{
name: "Set testHeaderMw to only GET requests (2/2)",
builders: []mm.Builder{
&testBuilder{
buildFunc: func(req *http.Request) (func(next http.Handler) http.Handler, bool) {
if req.Method != http.MethodGet {
return nil, false
}
return testHeaderMw, true
},
},
},
req: &http.Request{Method: http.MethodGet, URL: mustParseURL("http://example.com")},
want: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": {"text/plain; charset=utf-8"},
"X-Test": {"test"},
},
},
wantBody: "Hello",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httpstub.NewRouter(t)
r.Match(func(r *http.Request) bool { return true }).Response(http.StatusOK, []byte("Hello"))
m := mm.New(tt.builders...)
ts := httptest.NewServer(m(r))
tu := mustParseURL(ts.URL)
t.Cleanup(ts.Close)
tc := ts.Client()
tt.req.URL.Scheme = tu.Scheme
tt.req.URL.Host = tu.Host
got, err := tc.Do(tt.req)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = got.Body.Close()
})
opts := []cmp.Option{
cmpopts.IgnoreFields(http.Response{}, "Status", "Proto", "ProtoMajor", "ProtoMinor", "ContentLength", "TransferEncoding", "Uncompressed", "Trailer", "Request", "Close", "Body"),
}
// header ignore fields
got.Header.Del("Content-Length")
got.Header.Del("Date")
if diff := cmp.Diff(tt.want, got, opts...); diff != "" {
t.Error(diff)
}
b, err := io.ReadAll(got.Body)
if err != nil {
t.Fatal(err)
}
if got := string(b); got != tt.wantBody {
t.Errorf("Body: got %q, want %q", got, tt.wantBody)
}
})
}
}
func mustParseURL(urlstr string) *url.URL {
u, err := url.Parse(urlstr)
if err != nil {
panic(err) //nostyle:dontpanic
}
return u
}