-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathredirect.spec.ts
342 lines (265 loc) · 12.4 KB
/
redirect.spec.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* @adonisjs/http-server
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import supertest from 'supertest'
import { test } from '@japa/runner'
import { AppFactory } from '@adonisjs/application/factories'
import { EncryptionFactory } from '@adonisjs/encryption/factories'
import { RouterFactory } from '../factories/router.js'
import { httpServer } from '../factories/http_server.js'
import { ResponseFactory } from '../factories/response.js'
const BASE_URL = new URL('./app/', import.meta.url)
const app = new AppFactory().create(BASE_URL, () => {})
const encryption = new EncryptionFactory().create()
const router = new RouterFactory().merge({ app, encryption }).create()
test.group('Redirect', () => {
test('redirect to given url', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('/foo')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/foo')
})
test('redirect to given url with query string', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('/foo', true)
response.finish()
})
const { header } = await supertest(url).get('/?username=romain').redirects(1)
assert.equal(header.location, '/foo?username=romain')
})
test('redirect to given url and forward current query string', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs().toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/?username=romain').redirects(1)
assert.equal(header.location, '/foo?username=romain')
})
test('redirect to given url with custom query string', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs('username', 'romain').toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/foo?username=romain')
})
test('redirect to given url with custom query string overwriting the forward rules', async ({
assert,
}) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs().withQs('username', 'romain').toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/?username=virk').redirects(1)
assert.equal(header.location, '/foo?username=romain')
})
test('redirect to given url with custom query given as object', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response
.redirect()
.withQs({
username: 'romain',
username2: 'virk',
})
.toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/foo?username=romain&username2=virk')
})
test('do not set query string when originally there was no query string', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('/foo', true)
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/foo')
})
test('redirect to given url and set custom statusCode', async () => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('/foo', false, 301)
response.finish()
})
await supertest(url).get('/').redirects(1).expect(301)
})
test('redirect to given url and set custom statusCode using fluent API', async () => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().status(301).toPath('/foo')
response.finish()
})
await supertest(url).get('/').redirects(1).expect(301)
})
test('redirect back to referrer', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('back')
response.finish()
})
const { header } = await supertest(url).get('/').set('referrer', '/foo').redirects(1)
assert.equal(header.location, '/foo')
})
test('redirect back to referrer with existing query string', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs().back()
response.finish()
})
const { header } = await supertest(url).get('/').set('referrer', '/foo?name=virk').redirects(1)
assert.equal(header.location, '/foo?name=virk')
})
test('redirect back to referrer with query string', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs({ name: 'virk' }).back()
response.finish()
})
const { header } = await supertest(url).get('/').set('referer', '/foo?name=virk').redirects(1)
assert.equal(header.location, '/foo?name=virk')
})
test('redirect back to root (/) when referrer header is not set', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('back')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/')
})
test('redirect back to root (/) when referrer header is empty', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect('back')
response.finish()
})
const { header } = await supertest(url).get('/').set('referer', '').redirects(1)
assert.equal(header.location, '/')
})
test('redirect to given route', async ({ assert }) => {
const route = new RouterFactory().merge({ app, encryption }).create()
route.get('posts', 'PostsController.index').as('posts.index')
route.commit()
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router: route }).create()
response.redirect().toRoute('posts.index')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/posts')
})
test('redirect to given route with params', async ({ assert }) => {
const route = new RouterFactory().merge({ app, encryption }).create()
route.get('posts/:id', 'PostsController.show').as('post.show')
route.commit()
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router: route }).create()
response.redirect().toRoute('post.show', { id: 1 })
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/posts/1')
})
test('redirect to given route with domain', async ({ assert }) => {
const route = new RouterFactory().merge({ app, encryption }).create()
route
.get('posts/create', 'PostsController.create')
.as('post.create')
.domain('domain.example.com')
route.commit()
const response = new ResponseFactory().merge({ encryption, router: route }).create()
response.redirect().toRoute('post.create', {}, { domain: 'domain.example.com' })
/**
* Header location cannot be protocol agnostic. We need to add support for
* defining domain protocols in the router and then this test should pass
*/
assert.equal(response.getHeader('location'), '/posts/create')
})
test('redirect to given route and forward query string', async ({ assert }) => {
const route = new RouterFactory().merge({ app, encryption }).create()
route.get('posts/:id', 'PostsController.show').as('post.show')
route.commit()
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router: route }).create()
response.redirect().withQs().toRoute('post.show', { id: 1 })
response.finish()
})
const { header } = await supertest(url).get('/?published=true').redirects(1)
assert.equal(header.location, '/posts/1?published=true')
})
test('redirect to given route and add qs via makeRoute', async ({ assert }) => {
const route = new RouterFactory().merge({ app, encryption }).create()
route.get('posts/:id', 'PostsController.show').as('post.show')
route.commit()
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router: route }).create()
response
.redirect()
.withQs('user', 'virk')
.toRoute('post.show', { id: 1 }, { qs: { published: true } })
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/posts/1?user=virk&published=true')
})
test('throw when given route is not found', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
assert.throws(() => {
response.redirect().toRoute('posts')
}, 'Cannot lookup route "posts"')
response.finish()
})
await supertest(url).get('/').redirects(1)
})
test('merge query string values when withQs is called multiple times', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs('username', 'romain').withQs('age', 28).toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/foo?username=romain&age=28')
})
test('merge query string with current url qs values', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs().withQs('age', 28).toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/?username=virk').redirects(1)
assert.equal(header.location, '/foo?username=virk&age=28')
})
test('do not set query string original url has no qs', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs().toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/foo')
})
test('clear existing qs', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const response = new ResponseFactory().merge({ req, res, encryption, router }).create()
response.redirect().withQs('name', 'virk').clearQs().toPath('/foo')
response.finish()
})
const { header } = await supertest(url).get('/').redirects(1)
assert.equal(header.location, '/foo')
})
})