test-tunnel.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // test that we can tunnel a https request over an http proxy
  2. // keeping all the CA and whatnot intact.
  3. //
  4. // Note: this requires that squid is installed.
  5. // If the proxy fails to start, we'll just log a warning and assume success.
  6. var server = require('./server')
  7. , assert = require('assert')
  8. , request = require('../index')
  9. , fs = require('fs')
  10. , path = require('path')
  11. , caFile = path.resolve(__dirname, 'ssl/npm-ca.crt')
  12. , ca = fs.readFileSync(caFile)
  13. , child_process = require('child_process')
  14. , sqConf = path.resolve(__dirname, 'squid.conf')
  15. , sqArgs = ['-f', sqConf, '-N', '-d', '5']
  16. , proxy = 'http://localhost:3128'
  17. , hadError = null
  18. var squid = child_process.spawn('squid', sqArgs);
  19. var ready = false
  20. squid.stderr.on('data', function (c) {
  21. console.error('SQUIDERR ' + c.toString().trim().split('\n')
  22. .join('\nSQUIDERR '))
  23. ready = c.toString().match(/ready to serve requests|Accepting HTTP Socket connections/i)
  24. })
  25. squid.stdout.on('data', function (c) {
  26. console.error('SQUIDOUT ' + c.toString().trim().split('\n')
  27. .join('\nSQUIDOUT '))
  28. })
  29. squid.on('error', function (c) {
  30. console.error('squid: error '+c)
  31. if (c && !ready) {
  32. notInstalled()
  33. return
  34. }
  35. })
  36. squid.on('exit', function (c) {
  37. console.error('squid: exit '+c)
  38. if (c && !ready) {
  39. notInstalled()
  40. return
  41. }
  42. if (c) {
  43. hadError = hadError || new Error('Squid exited with '+c)
  44. }
  45. if (hadError) throw hadError
  46. })
  47. setTimeout(function F () {
  48. if (!ready) return setTimeout(F, 100)
  49. request({ uri: 'https://registry.npmjs.org/'
  50. , proxy: 'http://localhost:3128'
  51. , strictSSL: true
  52. , ca: ca
  53. , json: true }, function (er, body) {
  54. hadError = er
  55. console.log(er || typeof body)
  56. if (!er) console.log("ok")
  57. squid.kill('SIGKILL')
  58. })
  59. }, 100)
  60. function notInstalled() {
  61. console.error('squid must be installed to run this test.')
  62. console.error('skipping this test. please install squid and run again if you need to test tunneling.')
  63. c = null
  64. hadError = null
  65. process.exit(0)
  66. }