-
Notifications
You must be signed in to change notification settings - Fork 22
/
redirect_test.go
230 lines (190 loc) · 8.56 KB
/
redirect_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
package integration
import (
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Redirection", func() {
Describe("exact redirects", func() {
BeforeEach(func() {
addRoute("/foo", NewRedirectRoute("/bar"))
addRoute("/foo-temp", NewRedirectRoute("/bar", "exact", "temporary"))
addRoute("/query-temp", NewRedirectRoute("/bar?query=true", "exact"))
addRoute("/fragment", NewRedirectRoute("/bar#section", "exact"))
addRoute("/preserve-query", NewRedirectRoute("/qux", "exact", "permanent", "preserve"))
reloadRoutes()
})
It("should redirect permanently by default", func() {
resp := routerRequest("/foo")
Expect(resp.StatusCode).To(Equal(301))
})
It("should redirect temporarily when asked to", func() {
resp := routerRequest("/foo-temp")
Expect(resp.StatusCode).To(Equal(302))
})
It("should contain the redirect location", func() {
resp := routerRequest("/foo")
Expect(resp.Header.Get("Location")).To(Equal("/bar"))
})
It("should not preserve the query string for the source by default", func() {
resp := routerRequest("/foo?baz=qux")
Expect(resp.Header.Get("Location")).To(Equal("/bar"))
})
It("should preserve the query string for the source if specified", func() {
resp := routerRequest("/preserve-query?foo=bar")
Expect(resp.Header.Get("Location")).To(Equal("/qux?foo=bar"))
})
It("should preserve the query string for the target", func() {
resp := routerRequest("/query-temp")
Expect(resp.Header.Get("Location")).To(Equal("/bar?query=true"))
})
It("should preserve the fragment for the target", func() {
resp := routerRequest("/fragment")
Expect(resp.Header.Get("Location")).To(Equal("/bar#section"))
})
It("should contain cache headers of 30 mins", func() {
resp := routerRequest("/foo")
Expect(resp.Header.Get("Cache-Control")).To(Equal("max-age=1800, public"))
Expect(
time.Parse(time.RFC1123, resp.Header.Get("Expires")),
).To(BeTemporally(
"~",
time.Now().Add(30*time.Minute),
time.Second,
))
})
})
Describe("prefix redirects", func() {
BeforeEach(func() {
addRoute("/foo", NewRedirectRoute("/bar", "prefix"))
addRoute("/foo-temp", NewRedirectRoute("/bar-temp", "prefix", "temporary"))
addRoute("/qux", NewRedirectRoute("/baz", "prefix", "temporary", "ignore"))
reloadRoutes()
})
It("should redirect permanently to the destination", func() {
resp := routerRequest("/foo")
Expect(resp.StatusCode).To(Equal(301))
Expect(resp.Header.Get("Location")).To(Equal("/bar"))
})
It("should redirect temporarily to the destination when asked to", func() {
resp := routerRequest("/foo-temp")
Expect(resp.StatusCode).To(Equal(302))
Expect(resp.Header.Get("Location")).To(Equal("/bar-temp"))
})
It("should preserve extra path sections when redirecting by default", func() {
resp := routerRequest("/foo/baz")
Expect(resp.Header.Get("Location")).To(Equal("/bar/baz"))
})
It("should ignore extra path sections when redirecting if specified", func() {
resp := routerRequest("/qux/quux")
Expect(resp.Header.Get("Location")).To(Equal("/baz"))
})
It("should preserve the query string when redirecting by default", func() {
resp := routerRequest("/foo?baz=qux")
Expect(resp.Header.Get("Location")).To(Equal("/bar?baz=qux"))
})
It("should not preserve the query string when redirecting if specified", func() {
resp := routerRequest("/qux/quux?foo=bar")
Expect(resp.Header.Get("Location")).To(Equal("/baz"))
})
It("should contain cache headers of 30 mins", func() {
resp := routerRequest("/foo")
Expect(resp.Header.Get("Cache-Control")).To(Equal("max-age=1800, public"))
Expect(
time.Parse(time.RFC1123, resp.Header.Get("Expires")),
).To(BeTemporally(
"~",
time.Now().Add(30*time.Minute),
time.Second,
))
})
It("should handle path-preserving redirects with special characters", func() {
addRoute("/foo%20bar", NewRedirectRoute("/bar%20baz", "prefix"))
reloadRoutes()
resp := routerRequest("/foo bar/something")
Expect(resp.StatusCode).To(Equal(301))
Expect(resp.Header.Get("Location")).To(Equal("/bar%20baz/something"))
})
})
Describe("external redirects", func() {
BeforeEach(func() {
addRoute("/foo", NewRedirectRoute("http://foo.example.com/foo"))
addRoute("/baz", NewRedirectRoute("http://foo.example.com/baz", "exact", "permanent", "preserve"))
addRoute("/bar", NewRedirectRoute("http://bar.example.com/bar", "prefix"))
addRoute("/qux", NewRedirectRoute("http://bar.example.com/qux", "prefix", "permanent", "ignore"))
reloadRoutes()
})
Describe("exact redirect", func() {
It("should redirect to the external URL", func() {
resp := routerRequest("/foo")
Expect(resp.Header.Get("Location")).To(Equal("http://foo.example.com/foo"))
})
It("should not preserve the query string by default", func() {
resp := routerRequest("/foo?foo=qux")
Expect(resp.Header.Get("Location")).To(Equal("http://foo.example.com/foo"))
})
It("should preserve the query string if specified", func() {
resp := routerRequest("/baz?foo=qux")
Expect(resp.Header.Get("Location")).To(Equal("http://foo.example.com/baz?foo=qux"))
})
})
Describe("prefix redirect", func() {
It("should redirect to the external URL", func() {
resp := routerRequest("/bar")
Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/bar"))
})
It("should preserve extra path sections when redirecting by default", func() {
resp := routerRequest("/bar/baz")
Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/bar/baz"))
})
It("should ignore extra path sections when redirecting if specified", func() {
resp := routerRequest("/qux/baz")
Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/qux"))
})
It("should preserve the query string when redirecting", func() {
resp := routerRequest("/bar?baz=qux")
Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/bar?baz=qux"))
})
})
})
Describe("redirects with a _ga parameter", func() {
BeforeEach(func() {
addRoute("/foo", NewRedirectRoute("https://hmrc.service.gov.uk/pay", "prefix", "permanent", "ignore"))
addRoute("/bar", NewRedirectRoute("https://bar.service.gov.uk/bar", "exact", "temporary", "preserve"))
addRoute("/baz", NewRedirectRoute("https://gov.uk/baz-luhrmann", "exact", "permanent", "ignore"))
addRoute("/pay-tax", NewRedirectRoute("https://tax.service.gov.uk/pay", "exact", "permanent", "ignore"))
addRoute("/biz-bank", NewRedirectRoute("https://british-business-bank.co.uk", "prefix", "permanent", "ignore"))
addRoute("/query-paramed", NewRedirectRoute("https://param.servicegov.uk?included-param=true", "exact", "permanent", "ignore"))
reloadRoutes()
})
It("should only preserve the _ga parameter when redirecting to service URLs that want to ignore query params", func() {
resp := routerRequest("/foo?_ga=identifier&blah=xyz")
Expect(resp.Header.Get("Location")).To(Equal("https://hmrc.service.gov.uk/pay?_ga=identifier"))
})
It("should retain all params when redirecting to a route that wants them", func() {
resp := routerRequest("/bar?wanted=param&_ga=xyz&blah=xyz")
Expect(resp.Header.Get("Location")).To(Equal("https://bar.service.gov.uk/bar?wanted=param&_ga=xyz&blah=xyz"))
})
It("should preserve the _ga parameter when redirecting to gov.uk URLs", func() {
resp := routerRequest("/baz?_ga=identifier")
Expect(resp.Header.Get("Location")).To(Equal("https://gov.uk/baz-luhrmann?_ga=identifier"))
})
It("should preserve the _ga parameter when redirecting to service.gov.uk URLs", func() {
resp := routerRequest("/pay-tax?_ga=12345")
Expect(resp.Header.Get("Location")).To(Equal("https://tax.service.gov.uk/pay?_ga=12345"))
})
It("should preserve only the first _ga parameter", func() {
resp := routerRequest("/pay-tax/?_ga=12345&_ga=6789")
Expect(resp.Header.Get("Location")).To(Equal("https://tax.service.gov.uk/pay?_ga=12345"))
})
It("should preserve the _ga param when redirecting to british business bank", func() {
resp := routerRequest("/biz-bank?unwanted=param&_ga=12345")
Expect(resp.Header.Get("Location")).To(Equal("https://british-business-bank.co.uk?_ga=12345"))
})
It("should preserve the _ga param and any existing query string that the target URL has", func() {
resp := routerRequest("/query-paramed?unwanted_param=blah&_ga=12345")
// https://param.servicegov.uk?included-param=true?unwanted_param=blah&_ga=12345
Expect(resp.Header.Get("Location")).To(Equal("https://param.servicegov.uk?_ga=12345&included-param=true"))
})
})
})