redirect_with_timeout.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var should = require('should')
  2. var needle = require('./../')
  3. describe('follow redirects when read_timeout is set', function () {
  4. it('clear timeout before following redirect', function (done) {
  5. var opts = {
  6. open_timeout: 1000,
  7. read_timeout: 3000,
  8. follow: 5,
  9. user_agent: 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
  10. }
  11. var timedOut = 0
  12. var redirects = 0
  13. var timer = setTimeout(function () {
  14. var hasRedirects = redirects > 0
  15. hasRedirects.should.equal(true)
  16. done()
  17. }, opts.read_timeout || 3000)
  18. var resp = needle.get('http://google.com/', opts, function (err, resp, body) {
  19. var noErr = err === null
  20. var hasBody = body.length > 0
  21. noErr.should.equal(true);
  22. hasBody.should.equal(true);
  23. });
  24. resp.on('redirect', function (location) {
  25. redirects++
  26. // console.info(' Redirected to ', location)
  27. })
  28. resp.on('timeout', function (type) {
  29. timedOut++
  30. timedOut.should.equal(0)
  31. // console.error(' ', type, 'timeout')
  32. clearTimeout(timer)
  33. done()
  34. })
  35. }).timeout(30000)
  36. })