-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy_route_test.go
256 lines (253 loc) · 9.05 KB
/
proxy_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package gobis_test
import (
"encoding/json"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/orange-cloudfoundry/gobis"
"gopkg.in/yaml.v2"
"net/http"
)
var _ = Describe("ProxyRoute", func() {
Context("UnmarshallJSON", func() {
It("should complain when check not passing", func() {
var route ProxyRoute
jsonRoute := `{"path": "/app/**", "url": "http://my.proxified.api"}`
err := json.Unmarshal([]byte(jsonRoute), &route)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("name"))
})
It("should create route when check pass", func() {
var route ProxyRoute
jsonRoute := `{"path": "/app/**", "url": "http://my.proxified.api", "name": "myroute"}`
err := json.Unmarshal([]byte(jsonRoute), &route)
Expect(err).ShouldNot(HaveOccurred())
Expect(route.Path.String()).Should(Equal("/app/**"))
Expect(route.Url).Should(Equal("http://my.proxified.api"))
Expect(route.Name).Should(Equal("myroute"))
})
Context("with hosts passthrough set", func() {
It("should create route when check pass", func() {
var route ProxyRoute
yamlRoute := `{
"path": "/app/**",
"url": "http://my.proxified.api",
"name": "myroute",
"hosts_passthrough": ["myhost.com", "*.myhost.com"]}`
err := yaml.Unmarshal([]byte(yamlRoute), &route)
Expect(err).ShouldNot(HaveOccurred())
Expect(route.HostsPassthrough).Should(HaveLen(2))
Expect(route.HostsPassthrough[0].String()).Should(Equal("myhost.com"))
Expect(route.HostsPassthrough[1].String()).Should(Equal("*.myhost.com"))
})
})
})
Context("UnmarshallYAML", func() {
It("should complain when check not passing", func() {
var route ProxyRoute
yamlRoute := "path: /app/**\nurl: http://my.proxified.api"
err := yaml.Unmarshal([]byte(yamlRoute), &route)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("name"))
})
It("should create route when check pass", func() {
var route ProxyRoute
yamlRoute := `path: /app/**
url: http://my.proxified.api
name: myroute`
err := yaml.Unmarshal([]byte(yamlRoute), &route)
Expect(err).ShouldNot(HaveOccurred())
Expect(route.Path.String()).Should(Equal("/app/**"))
Expect(route.Url).Should(Equal("http://my.proxified.api"))
Expect(route.Name).Should(Equal("myroute"))
})
Context("with hosts passthrough set", func() {
It("should create route when check pass", func() {
var route ProxyRoute
yamlRoute := `path: /app/**
url: http://my.proxified.api
name: myroute
hosts_passthrough:
- "myhost.com"
- "*.myhost.com"`
err := yaml.Unmarshal([]byte(yamlRoute), &route)
Expect(err).ShouldNot(HaveOccurred())
Expect(route.HostsPassthrough).Should(HaveLen(2))
Expect(route.HostsPassthrough[0].String()).Should(Equal("myhost.com"))
Expect(route.HostsPassthrough[1].String()).Should(Equal("*.myhost.com"))
})
})
})
Context("UpstreamUrl", func() {
It("should return original request url if option ForwardedHeader not set", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
Url: "http://my.proxified.api",
}
req, _ := http.NewRequest("GET", "http://localhost.com/path", nil)
upstreamUrl := route.UpstreamUrl(req)
Expect(upstreamUrl.String()).Should(Equal("http://my.proxified.api"))
})
It("should return original request url if option ForwardedHeader is set but not found in request", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
ForwardedHeader: "X-Forwarded-Url",
Url: "http://my.proxified.api",
}
req, _ := http.NewRequest("GET", "http://localhost.com/path", nil)
upstreamUrl := route.UpstreamUrl(req)
Expect(upstreamUrl.String()).Should(Equal("http://my.proxified.api"))
})
It("should return url without path from header set in option ForwardedHeader if it's set and found", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
ForwardedHeader: "X-Forwarded-Url",
Url: "http://my.proxified.api",
}
req, _ := http.NewRequest("GET", "http://localhost.com/path", nil)
req.Header.Set("X-Forwarded-Url", "http://other.url.com/otherpath")
upstreamUrl := route.UpstreamUrl(req)
Expect(upstreamUrl.String()).Should(Equal("http://other.url.com"))
})
})
Context("RequestPath", func() {
It("should return original request path if option ForwardedHeader not set", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
Url: "http://my.proxified.api",
}
req, _ := http.NewRequest("GET", "http://localhost.com/path", nil)
path := route.RequestPath(req)
Expect(path).Should(Equal("/path"))
})
It("should return original request path if option ForwardedHeader is set but not found in request", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
ForwardedHeader: "X-Forwarded-Url",
Url: "http://my.proxified.api",
}
req, _ := http.NewRequest("GET", "http://localhost.com/path", nil)
path := route.RequestPath(req)
Expect(path).Should(Equal("/path"))
})
It("should return path from url found in header set in option ForwardedHeader if it's set and found", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
ForwardedHeader: "X-Forwarded-Url",
Url: "http://my.proxified.api",
}
req, _ := http.NewRequest("GET", "http://localhost.com/path", nil)
req.Header.Set("X-Forwarded-Url", "http://other.url.com/otherpath")
path := route.RequestPath(req)
Expect(path).Should(Equal("/otherpath"))
})
})
Context("Check", func() {
It("should complain when no name is provided", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
Url: "http://my.proxified.api",
}
err := route.Check()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("name"))
})
It("should complain when no path is provided", func() {
route := ProxyRoute{
Name: "myroute",
Url: "http://my.proxified.api",
}
err := route.Check()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("path"))
})
It("should complain when no url is provided", func() {
route := ProxyRoute{
Path: NewPathMatcher("/app/**"),
Name: "my route",
}
err := route.Check()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("URL"))
})
It("should complain if url is set to localhost", func() {
route := ProxyRoute{
Name: "my route",
Path: NewPathMatcher("/app/**"),
Url: "http://localhost",
}
err := route.Check()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("localhost or 127.0.0.1"))
})
It("should complain if url is set to 127.0.0.1", func() {
route := ProxyRoute{
Name: "my route",
Path: NewPathMatcher("/app/**"),
Url: "http://127.0.0.1",
}
err := route.Check()
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("localhost or 127.0.0.1"))
})
Context("Check path", func() {
It("should match /**", func() {
route := ProxyRoute{
Name: "my route",
Path: NewPathMatcher("/**"),
Url: "http://my.proxified.api",
}
err := route.Check()
Expect(err).ShouldNot(HaveOccurred())
matcher := route.RouteMatcher()
Expect(matcher.MatchString("/firstlevel")).Should(BeTrue())
Expect(matcher.MatchString("/firstlevel/secondlevel")).Should(BeTrue())
})
It("should match /*", func() {
route := ProxyRoute{
Name: "my route",
Path: NewPathMatcher("/*"),
Url: "http://my.proxified.api",
}
err := route.Check()
Expect(err).ShouldNot(HaveOccurred())
matcher := route.RouteMatcher()
Expect(matcher.MatchString("/app")).Should(BeTrue())
Expect(matcher.MatchString("/app/secondlevel")).Should(BeFalse())
})
It("should not match /app/*", func() {
route := ProxyRoute{
Name: "my route",
Path: NewPathMatcher("/app/*"),
Url: "http://my.proxified.api",
}
err := route.Check()
Expect(err).ShouldNot(HaveOccurred())
matcher := route.RouteMatcher()
Expect(matcher.MatchString("/foo")).Should(BeFalse())
Expect(matcher.MatchString("/app")).Should(BeTrue())
Expect(matcher.MatchString("/app/secondlevel")).Should(BeTrue())
Expect(matcher.MatchString("/app/secondlevel/thirdlevel")).Should(BeFalse())
})
It("should not match /app/**", func() {
route := ProxyRoute{
Name: "my route",
Path: NewPathMatcher("/app/**"),
Url: "http://my.proxified.api",
}
err := route.Check()
Expect(err).ShouldNot(HaveOccurred())
matcher := route.RouteMatcher()
Expect(matcher.MatchString("/foo")).Should(BeFalse())
Expect(matcher.MatchString("/app")).Should(BeTrue())
Expect(matcher.MatchString("/app/secondlevel")).Should(BeTrue())
Expect(matcher.MatchString("/app/secondlevel/thirdlevel")).Should(BeTrue())
})
It("should not match /*/app", func() {
Expect(func() { NewPathMatcher("/*/app") }).Should(Panic())
})
It("should not match /app/***", func() {
Expect(func() { NewPathMatcher("/app/***") }).Should(Panic())
})
})
})
})