-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
test-follow-all.js
56 lines (47 loc) 路 1011 Bytes
/
test-follow-all.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
'use strict'
var http = require('http')
, request = require('../index')
, tape = require('tape')
var server = http.createServer(function (req, res) {
// redirect everything 3 times, no matter what.
var c = req.headers.cookie
if (!c) {
c = 0
} else {
c = +c.split('=')[1] || 0
}
if (c > 3) {
res.end('ok')
return
}
res.setHeader('set-cookie', 'c=' + (c + 1))
res.setHeader('location', req.url)
res.statusCode = 302
res.end('try again')
})
tape('setup', function(t) {
server.listen(6767, function() {
t.end()
})
})
tape('followAllRedirects', function(t) {
var redirects = 0
request.post({
url: 'http://localhost:6767/foo',
followAllRedirects: true,
jar: true,
form: { foo: 'bar' }
}, function (err, res, body) {
t.equal(err, null)
t.equal(body, 'ok')
t.equal(redirects, 4)
t.end()
}).on('redirect', function() {
redirects++
})
})
tape('cleanup', function(t) {
server.close(function() {
t.end()
})
})