-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
54 lines (48 loc) · 1.35 KB
/
router_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 mux
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestRouteName(t *testing.T) {
name := "test"
r := NewRoute("/", nil, WithName(name))
assertString(t, "name", r.Name(), name)
}
func TestRoutePattern(t *testing.T) {
pattern := "/test"
r := NewRoute(pattern, nil)
assertString(t, "pattern", r.Pattern(), pattern)
}
func TestRouteMethods(t *testing.T) {
method := http.MethodPost
r := NewRoute("/", nil, WithMethod(method))
assertDeepEqual(t, "methods", r.Methods(), []string{method})
}
func TestRouteMethodsAutomaticHEAD(t *testing.T) {
method := http.MethodGet
r := NewRoute("/", nil, WithMethod(method))
assertDeepEqual(t, "methods", r.Methods(), []string{method, http.MethodHead})
}
func TestRouteMiddleware(t *testing.T) {
have := ""
m1 := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
have += "a"
next.ServeHTTP(w, req)
have += "b"
})
}
m2 := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
have += "c"
next.ServeHTTP(w, req)
have += "d"
})
}
h := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {})
r := NewRoute("/", h, WithMiddleware(m1, m2))
w := httptest.NewRecorder()
r.ServeHTTP(w, (*http.Request)(nil))
assertString(t, "middleware", have, "acdb")
}