-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware_test.go
269 lines (200 loc) · 6.7 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
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
package muxter
import (
"bytes"
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestRecoverMiddleware(t *testing.T) {
mux := New()
panicMsg := "I can't even right now..."
recoverMiddleware := Recover(func(recovered interface{}, w http.ResponseWriter, r *http.Request, c Context) {
if recovered != panicMsg {
t.Errorf("expected recovery value to be: '%v' but got: '%v'", panicMsg, recovered)
}
w.WriteHeader(500)
io.WriteString(w, "calm down buddy.")
})
mux.Use(recoverMiddleware)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request, c Context) {
panic(panicMsg)
})
w, r := httptest.NewRecorder(), httptest.NewRequest("GET", "/anywhere", nil)
mux.ServeHTTP(w, r)
if w.Code != 500 {
t.Errorf("expected code to be 500 but got: %d", w.Code)
}
expectedPayload := "calm down buddy."
if actual := w.Body.String(); actual != expectedPayload {
t.Errorf("expected response body to be %q but got %q", expectedPayload, actual)
}
}
func TestMethodMiddleware(t *testing.T) {
t.Run("GET", func(t *testing.T) {
mux := New()
handler := new(HandlerMock)
mux.Get("/", handler)
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/path", nil)
mux.ServeHTTP(w, r)
if len(handler.ServeHTTPxCalls()) > 0 {
t.Fatalf("expected handler to not be called but was")
}
if w.Code != 405 {
t.Fatalf("expected code to be 405 but got: %d", w.Code)
}
})
t.Run("custom method not allowed handler", func(t *testing.T) {
mux := New()
expectedBody := "Custom 405!"
mux.SetMethodNotAllowedHandlerFunc(func(w http.ResponseWriter, r *http.Request, c Context) {
w.WriteHeader(405)
io.WriteString(w, expectedBody)
})
mux.PatchFunc("/", func(w http.ResponseWriter, r *http.Request, c Context) {})
r := httptest.NewRequest("PUT", "/", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
if w.Code != 405 {
t.Errorf("expected error code to be 405 but got %d", w.Code)
}
if body := w.Body.String(); body != expectedBody {
t.Errorf("expected body to be %q but got %q", expectedBody, body)
}
})
}
func TestGetMiddleware(t *testing.T) {
mux := New()
mux.GetFunc(
"/",
func(w http.ResponseWriter, r *http.Request, c Context) {
w.Header().Set("X-Custom", "value")
io.WriteString(w, "hello!")
},
)
// GET
w, r := httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil)
mux.ServeHTTP(w, r)
if xcustom := w.Header().Get("X-Custom"); xcustom != "value" {
t.Errorf("expected X-Custom header to equal %q but got %q", "value", xcustom)
}
expectedBody := "hello!"
if body := w.Body.String(); body != expectedBody {
t.Errorf("expected body to be %q but got %q", expectedBody, body)
}
// HEAD
w, r = httptest.NewRecorder(), httptest.NewRequest("HEAD", "/", nil)
mux.ServeHTTP(w, r)
if length := w.Body.Len(); length != 0 {
t.Errorf("expected length to be empty but got body of length %d", length)
}
if xcustom := w.Header().Get("X-Custom"); xcustom != "value" {
t.Errorf("expected X-Custom header to equal %q but got %q", "value", xcustom)
}
if contentLength := w.Header().Get("Content-Length"); contentLength != "6" {
t.Errorf("expected content-length to be 6 but got %q", contentLength)
}
// POST
w, r = httptest.NewRecorder(), httptest.NewRequest("POST", "/", nil)
mux.ServeHTTP(w, r)
if w.Code != 405 {
t.Errorf("expected statusCode to be 405 but got %d", w.Code)
}
}
func TestDecompress(t *testing.T) {
mux := New()
mux.HandleFunc(
"/",
func(w http.ResponseWriter, r *http.Request, c Context) {
io.Copy(w, r.Body)
},
Decompress,
)
gzipReader := func(value string) io.Reader {
buf := new(bytes.Buffer)
gw := gzip.NewWriter(buf)
io.WriteString(gw, "hello world!")
gw.Flush()
gw.Close()
return buf
}
w, r := httptest.NewRecorder(), httptest.NewRequest("POST", "/", gzipReader("hello world!"))
r.Header.Set("Content-Encoding", "gzip")
mux.ServeHTTP(w, r)
expected := "hello world!"
if actual := w.Body.String(); actual != expected {
t.Errorf("expected body to be %q but got %q", expected, actual)
}
// Without Content-Encoding header should be skipped
w, r = httptest.NewRecorder(), httptest.NewRequest("POST", "/", gzipReader("hello world!"))
mux.ServeHTTP(w, r)
expected = "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff\xcaH\xcd\xc9\xc9W(\xcf/\xcaIQ\x04\x00\x00\x00\xff\xff\x01\x00\x00\xff\xffm´\x03\f\x00\x00\x00"
if actual := w.Body.String(); actual != expected {
t.Errorf("expected body to be %q but got %q", expected, actual)
}
}
func TestDecompressNoContent(t *testing.T) {
mux := New()
mux.HandleFunc(
"/",
func(w http.ResponseWriter, r *http.Request, c Context) {
io.Copy(w, r.Body)
},
Decompress,
)
w, r := httptest.NewRecorder(), httptest.NewRequest("POST", "/", nil)
r.Header.Set("Content-Encoding", "gzip")
mux.ServeHTTP(w, r)
expected := ""
if actual := w.Body.String(); actual != expected {
t.Errorf("expected body to be %q but got %q", expected, actual)
}
}
func TestCompress(t *testing.T) {
mux := New()
mux.HandleFunc(
"/",
func(w http.ResponseWriter, r *http.Request, c Context) {
io.WriteString(w, "hello world!")
},
Compress(),
)
w, r := httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil)
r.Header.Set("Accept-Encoding", "gzip")
mux.ServeHTTP(w, r)
expectedEncoding := "gzip"
if actualEncoding := w.Header().Get("Content-Encoding"); actualEncoding != expectedEncoding {
t.Errorf("expected content-encoding to be %q but got %q", expectedEncoding, actualEncoding)
}
expectedBody := "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff\xcaH\xcd\xc9\xc9W(\xcf/\xcaIQ\x04\x04\x00\x00\xff\xffm´\x03\f\x00\x00\x00"
if actualBody := w.Body.String(); expectedBody != actualBody {
t.Errorf("expected body to be %q but got %q", expectedBody, actualBody)
}
}
func TestSkipped(t *testing.T) {
mux := New()
cors := Skip(DefaultCORS, func(r *http.Request) bool { return r.Header.Get("origin") == "" })
mux.Use(cors)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request, c Context) {})
w, r := httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil)
mux.ServeHTTP(w, r)
expectedAbsentHeaders := []string{"Access-Control-Allow-Origin"}
for _, header := range expectedAbsentHeaders {
if value := w.Header().Get(header); value != "" {
t.Errorf("expected no value for header %q but got %q", header, value)
}
}
w, r = httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil)
r.Header.Set("Origin", "http://locahost.test")
mux.ServeHTTP(w, r)
expectedHeaders := map[string]string{
"Access-Control-Allow-Origin": "*",
}
for header, expected := range expectedHeaders {
if actual := w.Header().Get(header); actual != expected {
t.Errorf("expected header %q to have value %q but got %q", header, expected, actual)
}
}
}