forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-redirect.js
562 lines (512 loc) Β· 15.3 KB
/
test-redirect.js
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
'use strict'
var server = require('./server')
var assert = require('assert')
var request = require('../index')
var tape = require('tape')
var http = require('http')
var destroyable = require('server-destroy')
var Promise = require('bluebird')
var s = server.createServer()
var ss = server.createSSLServer()
var hits = {}
var jar = request.jar()
destroyable(s)
destroyable(ss)
s.on('/ssl', function (req, res) {
res.writeHead(302, {
location: ss.url + '/'
})
res.end()
})
ss.on('/', function (req, res) {
res.writeHead(200)
res.end('SSL')
})
function createRedirectEndpoint (code, label, landing) {
s.on('/' + label, function (req, res) {
hits[label] = true
res.writeHead(code, {
'location': s.url + '/' + landing,
'set-cookie': 'ham=eggs'
})
res.end()
})
}
function createLandingEndpoint (landing) {
s.on('/' + landing, function (req, res) {
// Make sure the cookie doesn't get included twice, see #139:
// Make sure cookies are set properly after redirect
assert.equal(req.headers.cookie, 'foo=bar; quux=baz; ham=eggs')
hits[landing] = true
res.writeHead(200, {'x-response': req.method.toUpperCase() + ' ' + landing})
res.end(req.method.toUpperCase() + ' ' + landing)
})
}
function bouncer (code, label, hops) {
var hop
var landing = label + '_landing'
var currentLabel
var currentLanding
hops = hops || 1
if (hops === 1) {
createRedirectEndpoint(code, label, landing)
} else {
for (hop = 0; hop < hops; hop++) {
currentLabel = (hop === 0) ? label : label + '_' + (hop + 1)
currentLanding = (hop === hops - 1) ? landing : label + '_' + (hop + 2)
createRedirectEndpoint(code, currentLabel, currentLanding)
}
}
createLandingEndpoint(landing)
}
tape('setup', function (t) {
s.listen(0, function () {
ss.listen(0, function () {
bouncer(301, 'temp')
bouncer(301, 'double', 2)
bouncer(301, 'treble', 3)
bouncer(302, 'perm')
bouncer(302, 'nope')
bouncer(307, 'fwd')
t.end()
})
})
})
tape('permanent bounce', function (t) {
jar.setCookie('quux=baz', s.url)
hits = {}
request({
uri: s.url + '/perm',
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.perm, 'Original request is to /perm')
t.ok(hits.perm_landing, 'Forward to permanent landing URL')
t.equal(body, 'GET perm_landing', 'Got permanent landing content')
t.end()
})
})
tape('preserve HEAD method when using followAllRedirects', function (t) {
jar.setCookie('quux=baz', s.url)
hits = {}
request({
method: 'HEAD',
uri: s.url + '/perm',
followAllRedirects: true,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.perm, 'Original request is to /perm')
t.ok(hits.perm_landing, 'Forward to permanent landing URL')
t.equal(res.headers['x-response'], 'HEAD perm_landing', 'Got permanent landing content')
t.end()
})
})
tape('temporary bounce', function (t) {
hits = {}
request({
uri: s.url + '/temp',
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(hits.temp_landing, 'Forward to temporary landing URL')
t.equal(body, 'GET temp_landing', 'Got temporary landing content')
t.end()
})
})
tape('prevent bouncing', function (t) {
hits = {}
request({
uri: s.url + '/nope',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: false
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 302)
t.ok(hits.nope, 'Original request to /nope')
t.ok(!hits.nope_landing, 'No chasing the redirect')
t.equal(res.statusCode, 302, 'Response is the bounce itself')
t.end()
})
})
tape('should not follow post redirects by default', function (t) {
hits = {}
request.post(s.url + '/temp', {
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 301)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(!hits.temp_landing, 'No chasing the redirect when post')
t.equal(res.statusCode, 301, 'Response is the bounce itself')
t.end()
})
})
tape('should follow post redirects when followallredirects true', function (t) {
hits = {}
request.post({
uri: s.url + '/temp',
followAllRedirects: true,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(hits.temp_landing, 'Forward to temporary landing URL')
t.equal(body, 'GET temp_landing', 'Got temporary landing content')
t.end()
})
})
tape('should follow post redirects when followallredirects true and followOriginalHttpMethod is enabled', function (t) {
hits = {}
request.post({
uri: s.url + '/temp',
followAllRedirects: true,
followOriginalHttpMethod: true,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(hits.temp_landing, 'Forward to temporary landing URL')
t.equal(body, 'POST temp_landing', 'Got temporary landing content')
t.end()
})
})
tape('should not follow post redirects when followallredirects false', function (t) {
hits = {}
request.post({
uri: s.url + '/temp',
followAllRedirects: false,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 301)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(!hits.temp_landing, 'No chasing the redirect')
t.equal(res.statusCode, 301, 'Response is the bounce itself')
t.end()
})
})
tape('should not follow delete redirects by default', function (t) {
hits = {}
request.del(s.url + '/temp', {
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.ok(res.statusCode >= 301 && res.statusCode < 400, 'Status is a redirect')
t.ok(hits.temp, 'Original request is to /temp')
t.ok(!hits.temp_landing, 'No chasing the redirect when delete')
t.equal(res.statusCode, 301, 'Response is the bounce itself')
t.end()
})
})
tape('should not follow delete redirects even if followredirect is set to true', function (t) {
hits = {}
request.del(s.url + '/temp', {
followRedirect: true,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 301)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(!hits.temp_landing, 'No chasing the redirect when delete')
t.equal(res.statusCode, 301, 'Response is the bounce itself')
t.end()
})
})
tape('should follow delete redirects when followallredirects true', function (t) {
hits = {}
request.del(s.url + '/temp', {
followAllRedirects: true,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(hits.temp_landing, 'Forward to temporary landing URL')
t.equal(body, 'GET temp_landing', 'Got temporary landing content')
t.end()
})
})
tape('should follow 307 delete redirects when followallredirects true', function (t) {
hits = {}
request.del(s.url + '/fwd', {
followAllRedirects: true,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.fwd, 'Original request is to /fwd')
t.ok(hits.fwd_landing, 'Forward to temporary landing URL')
t.equal(body, 'DELETE fwd_landing', 'Got temporary landing content')
t.end()
})
})
tape('double bounce', function (t) {
hits = {}
request({
uri: s.url + '/double',
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.double, 'Original request is to /double')
t.ok(hits.double_2, 'Forward to temporary landing URL')
t.ok(hits.double_landing, 'Forward to landing URL')
t.equal(body, 'GET double_landing', 'Got temporary landing content')
t.end()
})
})
tape('double bounce terminated after first redirect', function (t) {
function filterDouble (response) {
return (response.headers.location || '').indexOf('double_2') === -1
}
hits = {}
request({
uri: s.url + '/double',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: filterDouble
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 301)
t.ok(hits.double, 'Original request is to /double')
t.equal(res.headers.location, s.url + '/double_2', 'Current location should be ' + s.url + '/double_2')
t.end()
})
})
tape('triple bounce terminated after second redirect', function (t) {
function filterTreble (response) {
return (response.headers.location || '').indexOf('treble_3') === -1
}
hits = {}
request({
uri: s.url + '/treble',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: filterTreble
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 301)
t.ok(hits.treble, 'Original request is to /treble')
t.equal(res.headers.location, s.url + '/treble_3', 'Current location should be ' + s.url + '/treble_3')
t.end()
})
})
tape('asynchronous followRedirect filter via callback', function (t) {
function filterAsync (response, callback) {
setImmediate(function () { callback(null, true) })
return false // it should ignore this due to the function arity
}
hits = {}
request({
uri: s.url + '/temp',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: filterAsync
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(hits.temp_landing, 'Forward to temporary landing URL')
t.equal(body, 'GET temp_landing', 'Got temporary landing content')
t.end()
})
})
tape('asynchronous followRedirect filter via callback with error', function (t) {
var sampleError = new Error('Error during followRedirect callback')
function filterAsync (response, callback) {
setImmediate(function () { callback(sampleError) })
}
hits = {}
request({
uri: s.url + '/temp',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: filterAsync
}, function (err, res, body) {
t.equal(err, sampleError)
t.end()
})
})
tape('asynchronous followRedirect filter via promise', function (t) {
function filterPromise (response) {
return Promise.resolve(false)
}
hits = {}
request({
uri: s.url + '/temp',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: filterPromise
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 301)
t.ok(hits.temp, 'Original request is to /temp')
t.ok(!hits.temp_landing, 'No chasing the redirect promise returns false')
t.equal(res.statusCode, 301, 'Response is the bounce itself')
t.end()
})
})
tape('asynchronous followRedirect filter via promise (rejected)', function (t) {
var sampleError = new Error('Rejected followRedirect promise')
function filterPromise (response) {
return Promise.reject(sampleError)
}
hits = {}
request({
uri: s.url + '/temp',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: filterPromise
}, function (err, res, body) {
t.equal(err, sampleError)
t.end()
})
})
tape('overridden redirect url', function (t) {
hits = {}
request({
uri: s.url + '/temp',
jar: jar,
headers: { cookie: 'foo=bar' },
followRedirect: function (response) {
if (/temp_landing/.test(response.headers.location)) return s.url + '/perm'
return true
}
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.ok(hits.perm, 'Original request to /perm')
t.ok(hits.perm_landing, 'Forward to permanent landing URL')
t.equal(body, 'GET perm_landing', 'Got permanent landing content')
t.end()
})
})
tape('http to https redirect', function (t) {
hits = {}
request.get({
uri: require('url').parse(s.url + '/ssl'),
rejectUnauthorized: false,
allowInsecureRedirect: true
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(body, 'SSL', 'Got SSL redirect')
t.end()
})
})
tape('http to https redirect should fail without the explicit "allowInsecureRedirect" option', function (t) {
hits = {}
request.get({
uri: require('url').parse(s.url + '/ssl'),
rejectUnauthorized: false
}, function (err, res, body) {
t.notEqual(err, null)
t.equal(err.code, 'ERR_INVALID_PROTOCOL', 'Failed to cross-protocol redirect')
t.end()
})
})
tape('should have referer header by default when following redirect', function (t) {
request.post({
uri: s.url + '/temp',
jar: jar,
followAllRedirects: true,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.end()
})
.on('redirect', function () {
t.equal(this.headers.referer, s.url + '/temp')
})
})
tape('should not have referer header when removeRefererHeader is true', function (t) {
request.post({
uri: s.url + '/temp',
jar: jar,
followAllRedirects: true,
removeRefererHeader: true,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.end()
})
.on('redirect', function () {
t.equal(this.headers.referer, undefined)
})
})
tape('should preserve referer header set in the initial request when removeRefererHeader is true', function (t) {
request.post({
uri: s.url + '/temp',
jar: jar,
followAllRedirects: true,
removeRefererHeader: true,
headers: { cookie: 'foo=bar', referer: 'http://awesome.com' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.end()
})
.on('redirect', function () {
t.equal(this.headers.referer, 'http://awesome.com')
})
})
tape('should use same agent class on redirect', function (t) {
var agent
var calls = 0
var agentOptions = {}
function FakeAgent (agentOptions) {
var createConnection
agent = new http.Agent(agentOptions)
createConnection = agent.createConnection
agent.createConnection = function () {
calls++
return createConnection.apply(agent, arguments)
}
return agent
}
hits = {}
request.get({
uri: s.url + '/temp',
jar: jar,
headers: { cookie: 'foo=bar' },
agentOptions: agentOptions,
agentClass: FakeAgent
}, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(body, 'GET temp_landing', 'Got temporary landing content')
t.equal(calls, 2)
t.ok(this.agent === agent, 'Reinstantiated the user-specified agent')
t.ok(this.agentOptions === agentOptions, 'Reused agent options')
t.end()
})
})
tape('cleanup', function (t) {
s.destroy(function () {
ss.destroy(function () {
t.end()
})
})
})