aboutsummaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/request/tests/test-tunnel.js
blob: 2f7934dffeb483772bb0cc99a3aa56e7cba5f051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// test that we can tunnel a https request over an http proxy
// keeping all the CA and whatnot intact.
//
// Note: this requires that squid is installed.
// If the proxy fails to start, we'll just log a warning and assume success.

var server = require('./server')
  , assert = require('assert')
  , request = require('../index')
  , fs = require('fs')
  , path = require('path')
  , caFile = path.resolve(__dirname, 'ssl/npm-ca.crt')
  , ca = fs.readFileSync(caFile)
  , child_process = require('child_process')
  , sqConf = path.resolve(__dirname, 'squid.conf')
  , sqArgs = ['-f', sqConf, '-N', '-d', '5']
  , proxy = 'http://localhost:3128'
  , hadError = null

var squid = child_process.spawn('squid', sqArgs);
var ready = false

squid.stderr.on('data', function (c) {
  console.error('SQUIDERR ' + c.toString().trim().split('\n')
               .join('\nSQUIDERR '))
  ready = c.toString().match(/ready to serve requests|Accepting HTTP Socket connections/i)
})

squid.stdout.on('data', function (c) {
  console.error('SQUIDOUT ' + c.toString().trim().split('\n')
               .join('\nSQUIDOUT '))
})

squid.on('exit', function (c) {
  console.error('squid: exit '+c)
  if (c && !ready) {
    console.error('squid must be installed to run this test.')
    console.error('skipping this test. please install squid and run again if you need to test tunneling.')
    c = null
    hadError = null
    process.exit(0)
    return
  }

  if (c) {
    hadError = hadError || new Error('Squid exited with '+c)
  }
  if (hadError) throw hadError
})

setTimeout(function F () {
  if (!ready) return setTimeout(F, 100)
  request({ uri: 'https://registry.npmjs.org/'
          , proxy: 'http://localhost:3128'
          , strictSSL: true
          , ca: ca
          , json: true }, function (er, body) {
    hadError = er
    console.log(er || typeof body)
    if (!er) console.log("ok")
    squid.kill('SIGKILL')
  })
}, 100)