-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
138 lines (130 loc) · 3.3 KB
/
router_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
package celerity
import (
"testing"
"github.com/spf13/afero"
)
func TestRoutePathMatch(t *testing.T) {
t.Run("url params", func(t *testing.T) {
var rp RoutePath = "/users/:id/pages/:pageid"
t.Run("correct path", func(t *testing.T) {
ok, extra := rp.Match("/users/1/pages/23")
if !ok {
t.Error("RoutePath did not match a correct path")
}
if extra != "" {
t.Errorf("should not have any remaining path: %s", extra)
}
})
t.Run("incorrect path", func(t *testing.T) {
ok, _ := rp.Match("/users/1/badpath/23")
if ok {
t.Error("RoutePath did match")
}
})
t.Run("extra path tokens", func(t *testing.T) {
ok, extra := rp.Match("/users/1/pages/23/extra/path")
if !ok {
t.Error("did match")
}
if extra != "extra/path" {
t.Errorf("did not correctly return remaining path: %s", extra)
}
})
})
t.Run("root path", func(t *testing.T) {
var rp RoutePath = "/"
{
ok, xtra := rp.Match("/users")
if !ok {
t.Error("Did not match valid path")
}
if xtra != "/users" {
t.Errorf("Extra path not correct: %s", xtra)
}
}
})
}
func TestRoutePathGetURLParams(t *testing.T) {
var rp RoutePath = "/users/:id/pages/:pageid"
params := rp.GetURLParams("/users/22/pages/12")
if params["id"] != "22" {
t.Errorf("id param should be 22 was %s", params["id"])
}
}
func TestRoutePathWildCardMatch(t *testing.T) {
t.Run("base path wildcard", func(t *testing.T) {
var rp RoutePath = "*"
ok, _ := rp.Match("/some/route/to/test")
if !ok {
t.Error("wldcard path did not match")
}
})
t.Run("in path wildcard", func(t *testing.T) {
var rp RoutePath = "/some/route/*"
ok, _ := rp.Match("/some/route/to/test")
if !ok {
t.Error("wldcard path did not match")
}
})
}
func TestBasicRouteMatch(t *testing.T) {
t.Run("GET", func(t *testing.T) {
r := &BasicRoute{
Method: GET,
Path: "/users",
}
if ok, _ := r.Match(GET, "/users"); !ok {
t.Error("Did not match valid path")
}
if ok, _ := r.Match(GET, "/bad"); ok {
t.Error("Did match invalid path")
}
})
t.Run("POST", func(t *testing.T) {
r := &BasicRoute{
Method: POST,
Path: "/users",
}
if ok, _ := r.Match(GET, "/users"); ok {
t.Error("should not match incorrect method")
}
})
}
func TestLocalFileRoute(t *testing.T) {
adapter := NewMEMAdapter()
FSAdapter = adapter
r := &LocalFileRoute{
Path: "/public/test.txt",
LocalPath: "/files/test.txt",
}
t.Run("without file", func(t *testing.T) {
if ok, _ := r.Match(GET, "/public/test.txt"); ok {
t.Error("should not match non existant file")
}
})
t.Run("with file", func(t *testing.T) {
afero.WriteFile(adapter.MEMFS, "/files/test.txt", []byte("public file"), 0755)
if ok, _ := r.Match(GET, "/public/test.txt"); !ok {
t.Error("should match existing file")
}
})
}
func TestLocalPathRoute(t *testing.T) {
adapter := NewMEMAdapter()
FSAdapter = adapter
r := &LocalPathRoute{
Path: "/public/",
LocalPath: "/files/",
}
t.Run("without file", func(t *testing.T) {
if ok, _ := r.Match(GET, "/public/test.txt"); ok {
t.Error("should not match non existant file")
}
})
t.Run("with file", func(t *testing.T) {
afero.WriteFile(adapter.MEMFS, "/files/test.txt", []byte("public file"), 0755)
if ok, _ := r.Match(GET, "/public/test.txt"); !ok {
t.Error("should match existing file")
}
})
}