-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux_test.go
54 lines (48 loc) · 1.47 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
package mux2
import (
"net/http"
"net/http/httptest"
"testing"
)
var muxTests = []struct {
method string
path string
code int
body string
pattern string
}{
{"GET", "/x", 200, "/x", "/x"},
{"GET", "/./x", 301, "dontCare", "/x"},
{"PUT", "/x", 404, "dontCare", ""},
{"GET", "/x/123/abc", 200, "/x/*", "/x/"},
{"PUT", "/x/12345", 200, "/x/*", "/x/"},
{"PUT", "/abc/../blob", 301, "dontCare", "/blob"},
{"PUT", "/blob", 200, "put /blob", "/blob"},
{"GET", "/x/a", 200, "/x/a", "/x/a"},
{"GET", "/123/x", 200, "/venue=123/x", "/!venueID/x"},
}
func TestMux(t *testing.T) {
m := New()
m.Get("/:venueID/x", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("/venue=" + Param(r, "venueID") + "/x"))
}))
m.Get("/x/a", stringHandler("/x/a"))
m.Handle("/x/", stringHandler("/x/*"))
m.Get("/x", stringHandler("/x"))
m.Get("/", stringHandler("/"))
m.Put("/blob", stringHandler("put /blob"))
for _, tt := range muxTests {
w := httptest.NewRecorder()
r := httptest.NewRequest(tt.method, tt.path, nil)
m.ServeHTTP(w, r)
_, pattern := m.Handler(r)
if w.Code != tt.code || (tt.body != "dontCare" && w.Body.String() != tt.body) || pattern != tt.pattern {
t.Errorf("%s %s = %d %s (%s), want %d %s (%s)", tt.method, tt.path, w.Code, w.Body.String(), pattern, tt.code, tt.body, tt.pattern)
}
}
}
func stringHandler(s string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(s))
})
}