-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathkami_old_test.go
273 lines (239 loc) · 7.83 KB
/
kami_old_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
270
271
272
273
// +build !go1.7
package kami_test
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/zenazn/goji/web/mutil"
"golang.org/x/net/context"
"github.com/guregu/kami"
)
func TestKami(t *testing.T) {
kami.Reset()
kami.Cancel = true
done := make(chan struct{})
expect := func(ctx context.Context, i int) context.Context {
if prev := ctx.Value(i - 1).(int); prev != i-1 {
t.Error("missing", i)
}
if curr := ctx.Value(i); curr != nil {
t.Error("pre-existing", i)
}
return context.WithValue(ctx, i, i)
}
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = context.WithValue(ctx, 1, 1)
ctx = context.WithValue(ctx, "handler", new(bool))
ctx = context.WithValue(ctx, "done", new(bool))
ctx = context.WithValue(ctx, "recovered", new(bool))
go func() {
<-ctx.Done()
close(done)
}()
return ctx
})
kami.Use("/a/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = expect(ctx, 2)
return ctx
})
kami.Use("/a/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = expect(ctx, 3)
return ctx
})
kami.Use("/a/b", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = expect(ctx, 4)
return ctx
})
kami.Use("/a/*files", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = expect(ctx, 5)
return ctx
})
kami.Use("/a/*files", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = expect(ctx, 6)
return ctx
})
kami.Get("/a/b", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if prev := ctx.Value(6).(int); prev != 6 {
t.Error("handler: missing", 6)
}
*(ctx.Value("handler").(*bool)) = true
w.WriteHeader(http.StatusTeapot)
})
kami.After("/a/*files", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = expect(ctx, 8)
if !*(ctx.Value("handler").(*bool)) {
t.Error("ran before handler")
}
return ctx
})
kami.After("/a/*files", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx = expect(ctx, 7)
if !*(ctx.Value("handler").(*bool)) {
t.Error("ran before handler")
}
return ctx
})
kami.After("/a/b", kami.Afterware(func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
ctx = expect(ctx, 9)
return ctx
}))
kami.After("/a/", func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
ctx = expect(ctx, 11)
return ctx
})
kami.After("/a/", func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
ctx = expect(ctx, 10)
return ctx
})
kami.After("/", func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
if status := w.Status(); status != http.StatusTeapot {
t.Error("wrong status", status)
}
ctx = expect(ctx, 12)
*(ctx.Value("done").(*bool)) = true
panic("🍣")
return nil
})
kami.PanicHandler = func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if got := kami.Exception(ctx); got.(string) != "🍣" {
t.Error("panic handler: expected sushi, got", got)
}
if !*(ctx.Value("done").(*bool)) {
t.Error("didn't finish")
}
*(ctx.Value("recovered").(*bool)) = true
}
kami.LogHandler = func(ctx context.Context, w mutil.WriterProxy, r *http.Request) {
if !*(ctx.Value("recovered").(*bool)) {
t.Error("didn't recover")
}
}
expectResponseCode(t, "GET", "/a/b", http.StatusTeapot)
select {
case <-done:
// ok
case <-time.After(10 * time.Second):
panic("didn't cancel")
}
}
func TestLoggerAndPanic(t *testing.T) {
kami.Reset()
// test logger with panic
status := 0
kami.LogHandler = func(ctx context.Context, w mutil.WriterProxy, r *http.Request) {
status = w.Status()
}
kami.PanicHandler = kami.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
err := kami.Exception(ctx)
if err != "test panic" {
t.Error("unexpected exception:", err)
}
w.WriteHeader(http.StatusServiceUnavailable)
})
kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
kami.Put("/ok", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
expectResponseCode(t, "POST", "/test", http.StatusServiceUnavailable)
if status != http.StatusServiceUnavailable {
t.Error("log handler received wrong status code", status, "≠", http.StatusServiceUnavailable)
}
// test loggers without panics
expectResponseCode(t, "PUT", "/ok", http.StatusOK)
if status != http.StatusOK {
t.Error("log handler received wrong status code", status, "≠", http.StatusOK)
}
}
func TestPanickingLogger(t *testing.T) {
kami.Reset()
kami.LogHandler = func(ctx context.Context, w mutil.WriterProxy, r *http.Request) {
t.Log("log handler")
panic("test panic")
}
kami.PanicHandler = kami.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
t.Log("panic handler")
err := kami.Exception(ctx)
if err != "test panic" {
t.Error("unexpected exception:", err)
}
w.WriteHeader(http.StatusServiceUnavailable)
})
kami.Options("/test", noop)
expectResponseCode(t, "OPTIONS", "/test", http.StatusServiceUnavailable)
}
func TestNotFound(t *testing.T) {
kami.Reset()
kami.Use("/missing/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "ok", true)
})
kami.NotFound(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ok, _ := ctx.Value("ok").(bool)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusTeapot)
})
expectResponseCode(t, "GET", "/missing/hello", http.StatusTeapot)
}
func TestNotFoundDefault(t *testing.T) {
kami.Reset()
expectResponseCode(t, "GET", "/missing/hello", http.StatusNotFound)
}
func TestMethodNotAllowed(t *testing.T) {
kami.Reset()
kami.Use("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "ok", true)
})
kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
kami.MethodNotAllowed(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ok, _ := ctx.Value("ok").(bool)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusTeapot)
})
expectResponseCode(t, "GET", "/test", http.StatusTeapot)
}
func TestEnableMethodNotAllowed(t *testing.T) {
kami.Reset()
kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
// Handling enabled by default
expectResponseCode(t, "GET", "/test", http.StatusMethodNotAllowed)
// Not found deals with it when handling disabled
kami.EnableMethodNotAllowed(false)
expectResponseCode(t, "GET", "/test", http.StatusNotFound)
// And MethodNotAllowed status when handling enabled
kami.EnableMethodNotAllowed(true)
expectResponseCode(t, "GET", "/test", http.StatusMethodNotAllowed)
}
func TestMethodNotAllowedDefault(t *testing.T) {
kami.Reset()
kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
expectResponseCode(t, "GET", "/test", http.StatusMethodNotAllowed)
}
func noop(ctx context.Context, w http.ResponseWriter, r *http.Request) {}
func noopMW(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return ctx
}
func expectResponseCode(t *testing.T, method, path string, expected int) {
resp := httptest.NewRecorder()
req, err := http.NewRequest(method, path, nil)
if err != nil {
t.Fatal(err)
}
kami.Handler().ServeHTTP(resp, req)
if resp.Code != expected {
t.Error("should return HTTP", http.StatusText(expected)+":", resp.Code, "≠", expected)
}
}