test-proxy-env.js 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var server = require('./server')
  2. , events = require('events')
  3. , stream = require('stream')
  4. , assert = require('assert')
  5. , fs = require('fs')
  6. , request = require('../index')
  7. , path = require('path')
  8. , util = require('util')
  9. ;
  10. var port = 6768
  11. , called = false
  12. , proxiedHost = 'google.com'
  13. ;
  14. // set up environment variable
  15. process.env.HTTP_PROXY = 'http://localhost:'+port;
  16. var s = server.createServer(port)
  17. s.listen(port, function () {
  18. s.on('http://google.com/', function (req, res) {
  19. called = true
  20. assert.equal(req.headers.host, proxiedHost)
  21. res.writeHeader(200)
  22. res.end()
  23. })
  24. request ({
  25. url: 'http://'+proxiedHost,
  26. /* should read from HTTP_PROXY env var and
  27. // behave as if these arguments where passed:
  28. url: 'http://localhost:'+port,
  29. headers: {host: proxiedHost}
  30. //*/
  31. }, function (err, res, body) {
  32. s.close()
  33. })
  34. })
  35. process.on('exit', function () {
  36. assert.ok(called, 'the request must be made to the proxy server')
  37. })