-
-
Notifications
You must be signed in to change notification settings - Fork 916
/
redirects.ts
568 lines (465 loc) 路 14.1 KB
/
redirects.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import {Buffer} from 'buffer';
import test from 'ava';
import type {Handler} from 'express';
import nock from 'nock';
import got, {MaxRedirectsError, RequestError} from '../source/index.js';
import withServer, {withHttpsServer} from './helpers/with-server.js';
const reachedHandler: Handler = (_request, response) => {
const body = 'reached';
response.writeHead(200, {
'content-length': body.length,
});
response.end(body);
};
const finiteHandler: Handler = (_request, response) => {
response.writeHead(302, {
location: '/',
});
response.end();
};
const relativeHandler: Handler = (_request, response) => {
response.writeHead(302, {
location: '/',
});
response.end();
};
const unixProtocol: Handler = (_request, response) => {
response.writeHead(302, {
location: 'unix:/var/run/docker.sock:/containers/json',
});
response.end();
};
const unixHostname: Handler = (_request, response) => {
response.writeHead(302, {
location: 'http://unix:/var/run/docker.sock:/containers/json',
});
response.end();
};
test('cannot redirect to UNIX protocol when UNIX sockets are enabled', withServer, async (t, server, got) => {
server.get('/protocol', unixProtocol);
server.get('/hostname', unixHostname);
t.true(got.defaults.options.enableUnixSockets);
await t.throwsAsync(got('protocol'), {
message: 'Cannot redirect to UNIX socket',
instanceOf: RequestError,
});
await t.throwsAsync(got('hostname'), {
message: 'Cannot redirect to UNIX socket',
instanceOf: RequestError,
});
});
test('cannot redirect to UNIX protocol when UNIX sockets are not enabled', withServer, async (t, server, got) => {
server.get('/protocol', unixProtocol);
server.get('/hostname', unixHostname);
const gotUnixSocketsDisabled = got.extend({enableUnixSockets: false});
t.false(gotUnixSocketsDisabled.defaults.options.enableUnixSockets);
await t.throwsAsync(gotUnixSocketsDisabled('protocol'), {
message: 'Cannot redirect to UNIX socket',
instanceOf: RequestError,
});
await t.throwsAsync(gotUnixSocketsDisabled('hostname'), {
message: 'Cannot redirect to UNIX socket',
instanceOf: RequestError,
});
});
test('follows redirect', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/finite', finiteHandler);
const {body, redirectUrls} = await got('finite');
t.is(body, 'reached');
t.deepEqual(redirectUrls.map(String), [`${server.url}/`]);
});
test('follows 307, 308 redirect', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/temporary', (_request, response) => {
response.writeHead(307, {
location: '/',
});
response.end();
});
server.get('/permanent', (_request, response) => {
response.writeHead(308, {
location: '/',
});
response.end();
});
const temporaryBody = (await got('temporary')).body;
t.is(temporaryBody, 'reached');
const permBody = (await got('permanent')).body;
t.is(permBody, 'reached');
});
test('does not follow redirect when disabled', withServer, async (t, server, got) => {
server.get('/', finiteHandler);
t.is((await got({followRedirect: false})).statusCode, 302);
});
test('relative redirect works', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/relative', relativeHandler);
t.is((await got('relative')).body, 'reached');
});
test('throws on endless redirects - default behavior', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(302, {
location: server.url,
});
response.end();
});
const error = await t.throwsAsync<MaxRedirectsError>(got(''), {message: 'Redirected 10 times. Aborting.'});
t.deepEqual(error?.response.redirectUrls.map(String), Array.from({length: 10}).fill(`${server.url}/`));
t.is(error?.code, 'ERR_TOO_MANY_REDIRECTS');
});
test('custom `maxRedirects` option', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(302, {
location: server.url,
});
response.end();
});
const error = await t.throwsAsync<MaxRedirectsError>(got('', {maxRedirects: 5}), {message: 'Redirected 5 times. Aborting.'});
t.deepEqual(error?.response.redirectUrls.map(String), Array.from({length: 5}).fill(`${server.url}/`));
t.is(error?.code, 'ERR_TOO_MANY_REDIRECTS');
});
test('searchParams are not breaking redirects', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/relativeSearchParam', (request, response) => {
t.is(request.query.bang, '1');
response.writeHead(302, {
location: '/',
});
response.end();
});
t.is((await got('relativeSearchParam', {searchParams: 'bang=1'})).body, 'reached');
});
test('redirects GET and HEAD requests', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(308, {
location: '/',
});
response.end();
});
await t.throwsAsync(got.get(''), {
instanceOf: MaxRedirectsError,
code: 'ERR_TOO_MANY_REDIRECTS',
});
});
test('redirects POST requests', withServer, async (t, server, got) => {
server.post('/', (_request, response) => {
response.writeHead(308, {
location: '/',
});
response.end();
});
await t.throwsAsync(got.post({body: 'wow'}), {
instanceOf: MaxRedirectsError,
code: 'ERR_TOO_MANY_REDIRECTS',
});
});
test('redirects on 303 if GET or HEAD', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.head('/seeOther', (_request, response) => {
response.writeHead(303, {
location: '/',
});
response.end();
});
const {url, headers, request} = await got.head('seeOther');
t.is(url, `${server.url}/`);
t.is(headers['content-length'], 'reached'.length.toString());
t.is(request.options.method, 'HEAD');
});
test('removes body on GET redirect', withServer, async (t, server, got) => {
server.get('/', (request, response) => request.pipe(response));
server.post('/seeOther', (_request, response) => {
response.writeHead(303, {
location: '/',
});
response.end();
});
const {headers, body} = await got.post('seeOther', {body: 'hello'});
t.is(body, '');
t.is(headers['content-length'], '0');
});
test('redirects on 303 response even on post, put, delete', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.post('/seeOther', (_request, response) => {
response.writeHead(303, {
location: '/',
});
response.end();
});
const {url, body} = await got.post('seeOther', {body: 'wow'});
t.is(url, `${server.url}/`);
t.is(body, 'reached');
});
test('redirects from http to https work', withServer, async (t, serverHttp) => {
await withHttpsServer().exec(t, async (t, serverHttps, got) => {
serverHttp.get('/', (_request, response) => {
response.end('http');
});
serverHttps.get('/', (_request, response) => {
response.end('https');
});
serverHttp.get('/httpToHttps', (_request, response) => {
response.writeHead(302, {
location: serverHttps.url,
});
response.end();
});
t.is((await got('httpToHttps', {
prefixUrl: serverHttp.url,
})).body, 'https');
});
});
test('redirects from https to http work', withHttpsServer(), async (t, serverHttps, got) => {
await withServer.exec(t, async (t, serverHttp) => {
serverHttp.get('/', (_request, response) => {
response.end('http');
});
serverHttps.get('/', (_request, response) => {
response.end('https');
});
serverHttps.get('/httpsToHttp', (_request, response) => {
response.writeHead(302, {
location: serverHttp.url,
});
response.end();
});
t.is((await got('httpsToHttp', {
prefixUrl: serverHttps.url,
})).body, 'http');
});
});
test('redirects works with lowercase method', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/relative', relativeHandler);
const {body} = await got('relative', {method: 'head'});
t.is(body, '');
});
test('redirect response contains new url', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/finite', finiteHandler);
const {url} = await got('finite');
t.is(url, `${server.url}/`);
});
test('redirect response contains old url', withServer, async (t, server, got) => {
server.get('/', reachedHandler);
server.get('/finite', finiteHandler);
const {requestUrl} = await got('finite');
t.is(requestUrl.toString(), `${server.url}/finite`);
});
test('redirect response contains UTF-8 with binary encoding', withServer, async (t, server, got) => {
server.get('/utf8-url-%C3%A1%C3%A9', reachedHandler);
server.get('/redirect-with-utf8-binary', (_request, response) => {
response.writeHead(302, {
location: Buffer.from((new URL('/utf8-url-谩茅', server.url)).toString(), 'utf8').toString('binary'),
});
response.end();
});
t.is((await got('redirect-with-utf8-binary')).body, 'reached');
});
test('redirect response contains UTF-8 with URI encoding', withServer, async (t, server, got) => {
server.get('/', (request, response) => {
t.is(request.query.test, 'it鈥檚 ok');
response.end('reached');
});
server.get('/redirect-with-uri-encoded-location', (_request, response) => {
response.writeHead(302, {
location: new URL('/?test=it鈥檚+ok', server.url).toString(),
});
response.end();
});
t.is((await got('redirect-with-uri-encoded-location')).body, 'reached');
});
test('throws on invalid redirect URL', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(302, {
location: 'http://',
});
response.end();
});
await t.throwsAsync(got(''), {
code: 'ERR_INVALID_URL',
});
});
test('port is reset on redirect', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(307, {
location: 'http://localhost',
});
response.end();
});
nock('http://localhost').get('/').reply(200, 'ok');
const {body} = await got('');
t.is(body, 'ok');
});
test('body is reset on GET redirect', withServer, async (t, server, got) => {
server.post('/', (_request, response) => {
response.writeHead(303, {
location: '/',
});
response.end();
});
server.get('/', (_request, response) => {
response.end();
});
await got.post('', {
body: 'foobar',
hooks: {
beforeRedirect: [
options => {
t.is(options.body, undefined);
},
],
},
});
await got.post('', {
json: {foo: 'bar'},
hooks: {
beforeRedirect: [
options => {
t.is(options.body, undefined);
},
],
},
});
await got.post('', {
form: {foo: 'bar'},
hooks: {
beforeRedirect: [
options => {
t.is(options.body, undefined);
},
],
},
});
});
test('body is passed on POST redirect', withServer, async (t, server, got) => {
server.post('/redirect', (_request, response) => {
response.writeHead(302, {
location: '/',
});
response.end();
});
server.post('/', (request, response) => {
request.pipe(response);
});
const {body} = await got.post('redirect', {
body: 'foobar',
hooks: {
beforeRedirect: [
options => {
t.is(options.body, 'foobar');
},
],
},
});
t.is(body, 'foobar');
});
test('method rewriting', withServer, async (t, server, got) => {
server.post('/redirect', (_request, response) => {
response.writeHead(302, {
location: '/',
});
response.end();
});
server.get('/', (_request, response) => {
response.end();
});
server.post('/temporaryRedirect', (_request, response) => {
response.writeHead(307, {
location: '/',
});
response.end();
});
server.post('/', (request, response) => {
request.pipe(response);
});
const {body} = await got.post('redirect', {
body: 'foobar',
methodRewriting: true,
hooks: {
beforeRedirect: [
options => {
t.is(options.body, undefined);
},
],
},
});
t.is(body, '');
// Do not rewrite method on 307 or 308
const {body: temporaryRedirectBody} = await got.post('temporaryRedirect', {
body: 'foobar',
methodRewriting: true,
hooks: {
beforeRedirect: [
options => {
t.is(options.body, 'foobar');
},
],
},
});
t.is(temporaryRedirectBody, 'foobar');
});
test('clears username and password when redirecting to a different hostname', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(302, {
location: 'https://httpbin.org/anything',
});
response.end();
});
const {headers} = await got('', {
username: 'hello',
password: 'world',
}).json<{headers: Record<string, string | undefined>}>();
t.is(headers.Authorization, undefined);
});
test('clears the authorization header when redirecting to a different hostname', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(302, {
location: 'https://httpbin.org/anything',
});
response.end();
});
const {headers} = await got('', {
headers: {
authorization: 'Basic aGVsbG86d29ybGQ=',
},
}).json<{headers: Record<string, string | undefined>}>();
t.is(headers.Authorization, undefined);
});
test('preserves userinfo on redirect to the same origin', withServer, async (t, server) => {
server.get('/redirect', (_request, response) => {
response.writeHead(303, {
location: `http://localhost:${server.port}/`,
});
response.end();
});
server.get('/', (request, response) => {
t.is(request.headers.authorization, 'Basic aGVsbG86d29ybGQ=');
response.end();
});
await got(`http://hello:world@localhost:${server.port}/redirect`);
});
test('clears the host header when redirecting to a different hostname', async t => {
nock('https://testweb.com').get('/redirect').reply(302, undefined, {location: 'https://webtest.com/'});
nock('https://webtest.com').get('/').reply(function (_uri, _body) {
return [200, this.req.getHeader('host')];
});
const resp = await got('https://testweb.com/redirect', {headers: {host: 'wrongsite.com'}});
t.is(resp.body, 'webtest.com');
});
test('correct port on redirect', withServer, async (t, server1, got) => {
await withServer.exec(t, async (t, server2) => {
server1.get('/redirect', (_request, response) => {
response.redirect(`http://${server2.hostname}:${server2.port}/`);
});
server1.get('/', (_request, response) => {
response.end('SERVER1');
});
server2.get('/', (_request, response) => {
response.end('SERVER2');
});
const response = await got(`${server1.url}/redirect`, {prefixUrl: ''});
t.is(response.body, 'SERVER2');
});
});