test-redirect.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 server = require('./server')
  9. , assert = require('assert')
  10. , request = require('../index')
  11. ;
  12. var s = server.createServer()
  13. s.listen(s.port, function () {
  14. var server = 'http://localhost:' + s.port;
  15. var hits = {}
  16. var passed = 0;
  17. bouncer(301, 'temp')
  18. bouncer(302, 'perm')
  19. bouncer(302, 'nope')
  20. bouncer(307, 'fwd')
  21. function bouncer(code, label) {
  22. var landing = label+'_landing';
  23. s.on('/'+label, function (req, res) {
  24. hits[label] = true;
  25. res.writeHead(code, {
  26. 'location':server + '/'+landing,
  27. 'set-cookie': 'ham=eggs'
  28. })
  29. res.end()
  30. })
  31. s.on('/'+landing, function (req, res) {
  32. // Make sure the cookie doesn't get included twice, see #139:
  33. // Make sure cookies are set properly after redirect
  34. assert.equal(req.headers.cookie, 'foo=bar; quux=baz; ham=eggs');
  35. hits[landing] = true;
  36. res.writeHead(200)
  37. res.end(req.method.toUpperCase() + ' ' + landing)
  38. })
  39. }
  40. // Permanent bounce
  41. var jar = request.jar()
  42. jar.setCookie('quux=baz', server);
  43. request({uri: server+'/perm', jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  44. if (er) throw er
  45. if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
  46. assert.ok(hits.perm, 'Original request is to /perm')
  47. assert.ok(hits.perm_landing, 'Forward to permanent landing URL')
  48. assert.equal(body, 'GET perm_landing', 'Got permanent landing content')
  49. passed += 1
  50. done()
  51. })
  52. // Temporary bounce
  53. request({uri: server+'/temp', jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  54. if (er) throw er
  55. if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
  56. assert.ok(hits.temp, 'Original request is to /temp')
  57. assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
  58. assert.equal(body, 'GET temp_landing', 'Got temporary landing content')
  59. passed += 1
  60. done()
  61. })
  62. // Prevent bouncing.
  63. request({uri:server+'/nope', jar: jar, headers: {cookie: 'foo=bar'}, followRedirect:false}, function (er, res, body) {
  64. if (er) throw er
  65. if (res.statusCode !== 302) throw new Error('Status is not 302: '+res.statusCode)
  66. assert.ok(hits.nope, 'Original request to /nope')
  67. assert.ok(!hits.nope_landing, 'No chasing the redirect')
  68. assert.equal(res.statusCode, 302, 'Response is the bounce itself')
  69. passed += 1
  70. done()
  71. })
  72. // Should not follow post redirects by default
  73. request.post(server+'/temp', { jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  74. if (er) throw er
  75. if (res.statusCode !== 301) throw new Error('Status is not 301: '+res.statusCode)
  76. assert.ok(hits.temp, 'Original request is to /temp')
  77. assert.ok(!hits.temp_landing, 'No chasing the redirect when post')
  78. assert.equal(res.statusCode, 301, 'Response is the bounce itself')
  79. passed += 1
  80. done()
  81. })
  82. // Should follow post redirects when followAllRedirects true
  83. request.post({uri:server+'/temp', followAllRedirects:true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  84. if (er) throw er
  85. if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
  86. assert.ok(hits.temp, 'Original request is to /temp')
  87. assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
  88. assert.equal(body, 'GET temp_landing', 'Got temporary landing content')
  89. passed += 1
  90. done()
  91. })
  92. request.post({uri:server+'/temp', followAllRedirects:false, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  93. if (er) throw er
  94. if (res.statusCode !== 301) throw new Error('Status is not 301: '+res.statusCode)
  95. assert.ok(hits.temp, 'Original request is to /temp')
  96. assert.ok(!hits.temp_landing, 'No chasing the redirect')
  97. assert.equal(res.statusCode, 301, 'Response is the bounce itself')
  98. passed += 1
  99. done()
  100. })
  101. // Should not follow delete redirects by default
  102. request.del(server+'/temp', { jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  103. if (er) throw er
  104. if (res.statusCode < 301) throw new Error('Status is not a redirect.')
  105. assert.ok(hits.temp, 'Original request is to /temp')
  106. assert.ok(!hits.temp_landing, 'No chasing the redirect when delete')
  107. assert.equal(res.statusCode, 301, 'Response is the bounce itself')
  108. passed += 1
  109. done()
  110. })
  111. // Should not follow delete redirects even if followRedirect is set to true
  112. request.del(server+'/temp', { followRedirect: true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  113. if (er) throw er
  114. if (res.statusCode !== 301) throw new Error('Status is not 301: '+res.statusCode)
  115. assert.ok(hits.temp, 'Original request is to /temp')
  116. assert.ok(!hits.temp_landing, 'No chasing the redirect when delete')
  117. assert.equal(res.statusCode, 301, 'Response is the bounce itself')
  118. passed += 1
  119. done()
  120. })
  121. // Should follow delete redirects when followAllRedirects true
  122. request.del(server+'/temp', {followAllRedirects:true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  123. if (er) throw er
  124. if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
  125. assert.ok(hits.temp, 'Original request is to /temp')
  126. assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
  127. assert.equal(body, 'GET temp_landing', 'Got temporary landing content')
  128. passed += 1
  129. done()
  130. })
  131. request.del(server+'/fwd', {followAllRedirects:true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
  132. if (er) throw er
  133. if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
  134. assert.ok(hits.fwd, 'Original request is to /fwd')
  135. assert.ok(hits.fwd_landing, 'Forward to temporary landing URL')
  136. assert.equal(body, 'DELETE fwd_landing', 'Got temporary landing content')
  137. passed += 1
  138. done()
  139. })
  140. var reqs_done = 0;
  141. function done() {
  142. reqs_done += 1;
  143. if(reqs_done == 10) {
  144. console.log(passed + ' tests passed.')
  145. s.close()
  146. }
  147. }
  148. })