forked from uptrace/bunrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
142 lines (123 loc) · 3.4 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
142
package bunrouter
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestEmptyGroupAndMapping(t *testing.T) {
defer func() {
if err := recover(); err != nil {
// everything is good, it paniced
} else {
t.Error(`Expected NewGroup("")`)
}
}()
New().GET("", func(w http.ResponseWriter, _ Request) error {
return nil
})
}
func TestSubGroupSlashMapping(t *testing.T) {
r := New()
r.NewGroup("/foo").GET("/", func(w http.ResponseWriter, _ Request) error {
w.WriteHeader(200)
return nil
})
var req *http.Request
var recorder *httptest.ResponseRecorder
req, _ = http.NewRequest("GET", "/foo", nil)
recorder = httptest.NewRecorder()
r.ServeHTTP(recorder, req)
if recorder.Code != 301 { // should get redirected
t.Error(`/foo on NewGroup("/foo").GET("/") should result in 301 response, got:`, recorder.Code)
}
req, _ = http.NewRequest("GET", "/foo/", nil)
recorder = httptest.NewRecorder()
r.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Error(`/foo/ on NewGroup("/foo").GET("/") should result in 200 response, got:`, recorder.Code)
}
}
func TestSubGroupEmptyMapping(t *testing.T) {
r := New()
r.NewGroup("/foo").GET("", func(w http.ResponseWriter, _ Request) error {
w.WriteHeader(200)
return nil
})
req, _ := http.NewRequest("GET", "/foo", nil)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Error(`/foo on NewGroup("/foo").GET("") should result in 200 response, got:`, recorder.Code)
}
}
func TestGroupMethods(t *testing.T) {
for _, scenario := range scenarios {
t.Log(scenario.description)
testGroupMethods(t)
}
}
func TestInvalidHandle(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("Bad handle path should have caused a panic")
}
}()
New().NewGroup("/foo").GET("bar", nil)
}
func TestInvalidSubPath(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("Bad sub-path should have caused a panic")
}
}()
New().NewGroup("/foo").NewGroup("bar")
}
func TestInvalidPath(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("Bad path should have caused a panic")
}
}()
New().NewGroup("foo")
}
// Liberally borrowed from router_test
func testGroupMethods(t *testing.T) {
var result string
makeHandler := func(method string) HandlerFunc {
return func(w http.ResponseWriter, r Request) error {
result = method
return nil
}
}
router := New()
// Testing with a sub-group of a group as that will test everything at once
g := router.NewGroup("/base").NewGroup("/user")
g.GET("/:param", makeHandler("GET"))
g.POST("/:param", makeHandler("POST"))
g.PATCH("/:param", makeHandler("PATCH"))
g.PUT("/:param", makeHandler("PUT"))
g.DELETE("/:param", makeHandler("DELETE"))
testMethod := func(method, expect string) {
result = ""
w := httptest.NewRecorder()
r, _ := http.NewRequest(method, "/base/user/"+method, nil)
router.ServeHTTP(w, r)
if expect == "" {
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
}
if expect == "" {
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
} else {
require.Equal(t, expect, result)
}
}
testMethod("GET", "GET")
testMethod("POST", "POST")
testMethod("PATCH", "PATCH")
testMethod("PUT", "PUT")
testMethod("DELETE", "DELETE")
testMethod("HEAD", "")
router.HEAD("/base/user/:param", makeHandler("HEAD"))
testMethod("HEAD", "HEAD")
}