test-follow-all.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. try {
  2. require('tough-cookie')
  3. } catch (e) {
  4. console.error('tough-cookie must be installed to run this test.')
  5. console.error('skipping this test. please install tough-cookie and run again if you need to test this feature.')
  6. process.exit(0)
  7. }
  8. var request = require('../index');
  9. var http = require('http');
  10. var requests = 0;
  11. var assert = require('assert');
  12. var server = http.createServer(function (req, res) {
  13. requests ++;
  14. // redirect everything 3 times, no matter what.
  15. var c = req.headers.cookie;
  16. if (!c) c = 0;
  17. else c = +c.split('=')[1] || 0;
  18. if (c > 3) {
  19. res.end('ok: '+requests);
  20. return;
  21. }
  22. res.setHeader('set-cookie', 'c=' + (c + 1));
  23. res.setHeader('location', req.url);
  24. res.statusCode = 302;
  25. res.end('try again, i guess\n');
  26. });
  27. server.listen(6767);
  28. request.post({ url: 'http://localhost:6767/foo',
  29. followAllRedirects: true,
  30. jar: true,
  31. form: { foo: 'bar' } }, function (er, req, body) {
  32. if (er) throw er;
  33. assert.equal(body, 'ok: 5');
  34. assert.equal(requests, 5);
  35. console.error('ok - ' + process.version);
  36. server.close();
  37. });