-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting_table_test.go
73 lines (66 loc) · 1.53 KB
/
routing_table_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
package go_restful_routes
import (
"net/http"
"testing"
)
func TestNewRoutingTable(t *testing.T) {
// should initialize each map and slice
routingTable := NewRoutingTable()
if routingTable.mux == nil {
t.Fail()
}
if routingTable.full == nil {
t.Fail()
}
if routingTable.fast == nil {
t.Fail()
}
if routingTable.regex == nil {
t.Fail()
}
if routingTable.match == nil {
t.Fail()
}
}
func TestRegister(t *testing.T) {
handler := func(writer http.ResponseWriter, request *http.Request) {}
var item *routeItem
var err error
var table *RoutingTable
// add simple path to fast group
table = NewRoutingTable()
item, err = table.Register("/", handler, []string{http.MethodGet})
if err != nil || item == nil {
t.Fail()
}
if len(table.full) != 1 && len(table.fast) != 1 {
t.Fail()
}
// add regex path to regex group
table = NewRoutingTable()
item, err = table.Register("{^/[a-z]+\\[[0-9]+\\]$}", handler, []string{http.MethodGet})
if err != nil || item == nil {
t.Fail()
}
if len(table.full) != 1 && len(table.regex) != 1 {
t.Fail()
}
// add params path to match group
table = NewRoutingTable()
item, err = table.Register("/users/{:userId}/info/", handler, []string{http.MethodGet})
if err != nil || item == nil {
t.Fail()
}
if len(table.full) != 1 && len(table.match) != 1 {
t.Fail()
}
// ignore it when path or methods invalid
table = NewRoutingTable()
item, err = table.Register("", handler, []string{http.MethodGet})
if err == nil || item != nil {
t.Fail()
}
if len(table.full) != 0 {
t.Fail()
}
}