-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_item_test.go
69 lines (60 loc) · 1.5 KB
/
route_item_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
package go_restful_routes
import (
"net/http"
"testing"
)
func Test_fillKey(t *testing.T) {
// hash key should be filled
item, _ := newRouteItem("/users", fakeHandler, []string{http.MethodGet})
item.fillKey()
if len(item.key) != 1+32 {
t.Fail()
}
// the hash of two different items should be different
otherItem, _ := newRouteItem("/users/", fakeHandler, []string{http.MethodGet})
otherItem.fillKey()
if item.key == otherItem.key {
t.Fail()
}
}
func Test_newRouteItem(t *testing.T) {
handler := func(writer http.ResponseWriter, request *http.Request) {}
var err error
var item *routeItem
// path should not be empty
item, err = newRouteItem("", handler, []string{http.MethodGet})
if err == nil || item != nil {
t.Fail()
}
// methods could be empty
item, err = newRouteItem("/", handler, []string{})
if err != nil || item == nil {
t.Fail()
}
// success
item, err = newRouteItem("/", handler, []string{http.MethodGet})
if err != nil || item == nil {
t.Fail()
} else if item.key == "" {
t.Fail()
}
}
func Test_validHTTPMethod(t *testing.T) {
item, _ := newRouteItem("/", fakeHandler, []string{http.MethodGet, http.MethodPost})
if !item.validHTTPMethod(http.MethodPost) {
t.Fail()
}
if item.validHTTPMethod(http.MethodDelete) {
t.Fail()
}
}
// allow all
func Test_validHTTPMethod_emptyMethods(t *testing.T) {
item, _ := newRouteItem("/", fakeHandler, nil)
if !item.validHTTPMethod(http.MethodPost) {
t.Fail()
}
if !item.validHTTPMethod(http.MethodDelete) {
t.Fail()
}
}