-
Notifications
You must be signed in to change notification settings - Fork 11
/
group_test.go
141 lines (115 loc) · 4.17 KB
/
group_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
package xmux
import (
"net/http"
"testing"
"context"
"github.com/rs/xhandler"
"github.com/stretchr/testify/assert"
)
func TestRouteGroupOfARouteGroup(t *testing.T) {
var get bool
mux := New()
foo := mux.NewGroup("/foo") // creates /foo group
bar := foo.NewGroup("/bar")
bar.GET("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
get = true
}))
w := new(mockResponseWriter)
r, _ := http.NewRequest("GET", "/foo/bar/GET", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, get, "routing GET /foo/bar/GET failed")
}
func TestRouteNewGroupStripTrailingSlash(t *testing.T) {
var get bool
mux := New()
foo := mux.NewGroup("/foo/")
foo.GET("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
get = true
}))
w := new(mockResponseWriter)
r, _ := http.NewRequest("GET", "/foo/GET", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, get, "routing GET /foo/GET failed")
}
func TestRouteNewGroupError(t *testing.T) {
mux := New()
assert.Panics(t, func() {
mux.NewGroup("foo")
})
assert.Panics(t, func() {
mux.NewGroup("/foo").NewGroup("bar")
})
}
func TestGroupAdaptors(t *testing.T) {
mux := New()
foo := mux.NewGroup("/foo")
var handle, handleFunc bool
foo.Handle("GET", "/handle", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle = true
}))
foo.HandleFunc("GET", "/handleFunc", func(w http.ResponseWriter, r *http.Request) {
handleFunc = true
})
w := new(mockResponseWriter)
r, _ := http.NewRequest("GET", "/foo/handle", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, handle, "routing failed")
w = new(mockResponseWriter)
r, _ = http.NewRequest("GET", "/foo/handleFunc", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, handleFunc, "routing failed")
}
func TestRouteGroupAPI(t *testing.T) {
var get, head, options, post, put, patch, delete, ctx bool
mux := New()
group := mux.NewGroup("/foo") // creates /foo group
group.GET("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
get = true
}))
group.HEAD("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
head = true
}))
group.OPTIONS("/GET", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
options = true
}))
group.POST("/POST", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
post = true
}))
group.PUT("/PUT", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
put = true
}))
group.PATCH("/PATCH", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
patch = true
}))
group.DELETE("/DELETE", xhandler.HandlerFuncC(func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
delete = true
}))
group.HandleFuncC("GET", "/Context", func(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
ctx = true
})
w := new(mockResponseWriter)
r, _ := http.NewRequest("GET", "/foo/GET", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, get, "routing /foo/GET failed")
r, _ = http.NewRequest("HEAD", "/foo/GET", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, head, "routing /foo/GET failed")
r, _ = http.NewRequest("OPTIONS", "/foo/GET", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, options, "routing /foo/GET failed")
r, _ = http.NewRequest("POST", "/foo/POST", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, post, "routing /foo/POST failed")
r, _ = http.NewRequest("PUT", "/foo/PUT", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, put, "routing /foo/PUT failed")
r, _ = http.NewRequest("PATCH", "/foo/PATCH", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, patch, "routing /foo/PATCH failed")
r, _ = http.NewRequest("DELETE", "/foo/DELETE", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, delete, "routing /foo/DELETE failed")
r, _ = http.NewRequest("GET", "/foo/Context", nil)
mux.ServeHTTPC(context.Background(), w, r)
assert.True(t, ctx, "routing /foo/Context failed")
}