-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_test.go
71 lines (54 loc) · 1.62 KB
/
route_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
package cork
import (
"strings"
"testing"
)
func TestIndex(t *testing.T) {
route, _ := NewRoute("/", GET)
if !route.isRoot {
t.Error("Index route is not a root path.")
}
if len(route.Segments) <= 0 {
t.Error("Incorrect number of segments for index route")
}
if route.Method != "GET" {
t.Error("Incorrect method was set.")
}
}
func TestSingleLiteral(t *testing.T) {
route, _ := NewRoute("/literalpath", GET)
if route.isRoot {
t.Error("Should not be a root template.")
}
if len(route.Segments) != 1 {
t.Errorf("Should only have one segment but has %v segments.", len(route.Segments))
}
if route.Segments[0].Name != "literalpath" {
t.Errorf("Segment should not have name %v", route.Segments[0].Name)
}
}
func TestSegments(t *testing.T) {
route, _ := NewRoute("/foo/bar/cheese", GET)
if len(route.Segments) != 3 {
t.Errorf("Should only have three segments but has %v segments.", len(route.Segments))
}
if route.Segments[0].Name != "foo" {
t.Errorf("Should have 'foo' as first segment but has '%s' instead.", route.Segments[0].Name)
}
}
func TestInvalidPathSeparator(t *testing.T) {
_, err := NewRoute("foo/cork/open", GET)
if err != nil && !strings.HasPrefix(err.Error(), "Please specify a full path for route") {
t.Errorf("Should have failed due to missing leading slash.")
}
}
func TestSimpleVariableMatch(t *testing.T) {
route, _ := NewRoute("/foo/{bar}", GET)
if route == nil {
t.Error("Should have parsed simple variable path.")
}
segment := route.Segments[1]
if !segment.Variable || segment.Name != "bar" {
t.Errorf("Should have variable named 'bar'. Segment is %v", segment.Variable)
}
}