forked from guregu/kami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kami_test.go
150 lines (127 loc) · 4.01 KB
/
kami_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
package kami_test
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/zenazn/goji/web/mutil"
"golang.org/x/net/context"
"github.com/guregu/kami"
)
func TestKami(t *testing.T) {
kami.Reset()
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "test1", "1")
})
kami.Use("/v2/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "test2", "2")
})
kami.Get("/v2/papers/:page", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
page := kami.Param(ctx, "page")
if page == "" {
panic("blank page")
}
io.WriteString(w, page)
test1 := ctx.Value("test1").(string)
test2 := ctx.Value("test2").(string)
if test1 != "1" || test2 != "2" {
t.Error("unexpected ctx value:", test1, test2)
}
})
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/v2/papers/3", nil)
if err != nil {
t.Fatal(err)
}
kami.Handler().ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Error("should return HTTP OK", resp.Code, "≠", http.StatusOK)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if string(data) != "3" {
t.Error("expected page 3, got", string(data))
}
}
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)
w.Write([]byte("error 503"))
})
kami.Post("/test", noop)
expectResponseCode(t, "POST", "/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 noop(ctx context.Context, w http.ResponseWriter, r *http.Request) {}
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)
}
}