forked from simonmittag/j8a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_test.go
159 lines (139 loc) · 3.82 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package jabba
import (
"github.com/rs/zerolog"
"net/http"
"regexp"
"testing"
)
func TestRouteMatchRoot(t *testing.T) {
routeNoMatch("/", "", t)
routeMatch("/", "/some", t)
routeMatch("/", "/", t)
routeMatch("/", "/some/more", t)
routeMatch("/", "/some/more?param", t)
routeMatch("/", "/some/more?param=value", t)
routeMatch("/", "/some/more?param=value¶m2=value2", t)
//path is never empty string, http server inserts "/"
}
func TestRouteMatchWithSlug(t *testing.T) {
routeNoMatch("/so", "so", t)
routeNoMatch("/so", "/os", t)
routeMatch("/so", "/some", t)
routeMatch("/so", "/some/more", t)
routeMatch("/so", "/some/more?param", t)
routeMatch("/so", "/some/more?param=value", t)
routeMatch("/so", "/some/more?param=value¶m2=value2", t)
}
func TestRouteMatchWithTerminatedSlug(t *testing.T) {
routeNoMatch("/some/", "some", t)
routeNoMatch("/some/", "", t)
routeNoMatch("/some/", "/", t)
routeNoMatch("/some/", "/some", t)
routeMatch("/some/", "/some/", t)
routeMatch("/some/", "/some/more", t)
routeMatch("/some/", "/some/more?param", t)
routeMatch("/some/", "/some/more?param=value", t)
routeMatch("/some/", "/some/more?param=value¶m2=value2", t)
}
func routeMatch(route string, path string, t *testing.T) {
r := routeFactory(route)
req := requestFactory(path)
if !r.matchURI(req) {
t.Errorf("route %v did not match desired path: %v", route, path)
}
}
func routeNoMatch(route string, path string, t *testing.T) {
r := routeFactory(route)
req := requestFactory(path)
if r.matchURI(req) {
t.Errorf("route %v did match undesired path: %v", route, path)
}
}
func requestFactory(path string) *http.Request {
req, _ := http.NewRequest("GET", path, nil)
req.RequestURI = path
return req
}
func routeFactory(route string) Route {
pR, _ := regexp.Compile("^" + route)
r := Route{
Path: route,
Regex: pR,
}
return r
}
func TestRouteMapDefault(t *testing.T) {
Runner = mockRuntime()
r := Runner.Routes[1]
gotUrl, gotLabel, got := r.mapURL(&Proxy{})
if got != true {
t.Error("routes do not successfully map")
}
wantLabel := "default"
if gotLabel != "default" {
t.Errorf("label did not successfully map. want %v, got %v", wantLabel, gotLabel)
}
wantUrl := URL{
Scheme: "http",
Host: "localhost",
Port: 8084,
}
if *gotUrl != wantUrl {
t.Errorf("url did not successfuly map, want %v, got %v, wantUrl, url", wantUrl, gotUrl)
}
}
func TestRouteMap(t *testing.T) {
Runner = mockRuntime()
r := Runner.Routes[0]
gotUrl, gotLabel, got := r.mapURL(&Proxy{})
if got != true {
t.Error("routes do not successfully map")
}
wantLabel := "simple"
if gotLabel != "simple" {
t.Errorf("label did not successfully map. want %v, got %v", wantLabel, gotLabel)
}
wantUrl := URL{
Scheme: "http",
Host: "localhost",
Port: 8083,
}
if *gotUrl != wantUrl {
t.Errorf("url did not successfuly map, want %v, got %v, wantUrl, url", wantUrl, gotUrl)
}
}
func TestRouteMapIncorrectPolicyFails(t *testing.T) {
Runner = mockRuntime()
r := Runner.Routes[1]
r.Policy = "i'm_not_real"
_, _, got := r.mapURL(&Proxy{})
if got != false {
t.Error("route with bad policy is not allowed to map. this is a configuration error")
}
}
func BenchmarkRouteMatchingRegex(b *testing.B) {
//suppress noise
zerolog.SetGlobalLevel(zerolog.InfoLevel)
config := new(Config).read("./jabba.yml")
config = config.compileRoutePaths().sortRoutes()
for i := 0; i < b.N; i++ {
for _, route := range config.Routes {
if ok := route.matchURI(requestFactory("/mse6")); ok {
break
}
}
}
}
func BenchmarkRouteMatchingString(b *testing.B) {
//suppress noise
zerolog.SetGlobalLevel(zerolog.InfoLevel)
config := new(Config).read("./jabba.yml")
config = config.sortRoutes()
for i := 0; i < b.N; i++ {
for _, route := range config.Routes {
if ok := route.matchURI(requestFactory("/mse6")); ok {
break
}
}
}
}