test-piped-redirect.js 868 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var http = require('http')
  2. , assert = require('assert')
  3. , request = require('../index')
  4. ;
  5. var portOne = 8968
  6. , portTwo = 8969
  7. ;
  8. // server one
  9. var s1 = http.createServer(function (req, resp) {
  10. if (req.url == '/original') {
  11. resp.writeHeader(302, {'location': '/redirected'})
  12. resp.end()
  13. } else if (req.url == '/redirected') {
  14. resp.writeHeader(200, {'content-type': 'text/plain'})
  15. resp.write('OK')
  16. resp.end()
  17. }
  18. }).listen(portOne);
  19. // server two
  20. var s2 = http.createServer(function (req, resp) {
  21. var x = request('http://localhost:'+portOne+'/original')
  22. req.pipe(x)
  23. x.pipe(resp)
  24. }).listen(portTwo, function () {
  25. var r = request('http://localhost:'+portTwo+'/original', function (err, res, body) {
  26. assert.equal(body, 'OK')
  27. s1.close()
  28. s2.close()
  29. });
  30. // it hangs, so wait a second :)
  31. r.timeout = 1000;
  32. })