f2e51ffa7953545ebae2269bdec0322dd40a3439
[platform/upstream/nodejs.git] / deps / npm / node_modules / request / tests / test-follow-all.js
1 var request = require('../index');
2 var http = require('http');
3 var requests = 0;
4 var assert = require('assert');
5
6 var server = http.createServer(function (req, res) {
7   requests ++;
8
9   // redirect everything 3 times, no matter what.
10   var c = req.headers.cookie;
11
12   if (!c) c = 0;
13   else c = +c.split('=')[1] || 0;
14
15   if (c > 3) {
16     res.end('ok: '+requests);
17     return;
18   }
19
20   res.setHeader('set-cookie', 'c=' + (c + 1));
21   res.setHeader('location', req.url);
22   res.statusCode = 302;
23   res.end('try again, i guess\n');
24 });
25 server.listen(6767);
26
27 request.post({ url: 'http://localhost:6767/foo',
28                followAllRedirects: true,
29                jar: true,
30                form: { foo: 'bar' } }, function (er, req, body) {
31   if (er) throw er;
32   assert.equal(body, 'ok: 5');
33   assert.equal(requests, 5);
34   console.error('ok - ' + process.version);
35   server.close();
36 });