-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
mux_test.go
134 lines (113 loc) · 3.43 KB
/
mux_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
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"
)
// TODO: this mostly a copy/paste of kami_test.go, rewrite it!
func TestKamiMux(t *testing.T) {
mux := kami.New()
// normal stuff
mux.Use("/mux/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "test1", "1")
})
mux.Use("/mux/v2/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "test2", "2")
})
mux.Get("/mux/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)
}
})
// 404 stuff
mux.Use("/mux/missing/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "ok", true)
})
mux.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)
})
// 405 stuff
mux.Use("/mux/method_not_allowed", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "ok", true)
})
mux.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)
})
mux.Post("/mux/method_not_allowed", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
panic("test panic")
})
stdMux := http.NewServeMux()
stdMux.Handle("/mux/", mux)
// test normal stuff
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/mux/v2/papers/3", nil)
if err != nil {
t.Fatal(err)
}
stdMux.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))
}
// test 404
resp = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/mux/missing/hello", nil)
if err != nil {
t.Fatal(err)
}
stdMux.ServeHTTP(resp, req)
if resp.Code != http.StatusTeapot {
t.Error("should return HTTP Teapot", resp.Code, "≠", http.StatusTeapot)
}
// test 405
resp = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/mux/method_not_allowed", nil)
if err != nil {
t.Fatal(err)
}
stdMux.ServeHTTP(resp, req)
if resp.Code != http.StatusTeapot {
t.Error("should return HTTP Teapot", resp.Code, "≠", http.StatusTeapot)
}
// test EnableMethodNotAllowed method
resp = httptest.NewRecorder()
req, err = http.NewRequest("GET", "/mux/method_not_allowed", nil)
if err != nil {
t.Fatal(err)
}
// Reset NotFound handler to receive default 404 instead of custom handler 418(Teapot)
mux.NotFound(nil)
mux.EnableMethodNotAllowed(false)
stdMux.ServeHTTP(resp, req)
if resp.Code != http.StatusNotFound {
t.Error("should return HTTP NotFound", resp.Code, "≠", http.StatusNotFound)
}
}