2ee3f393ed70f7139527152b2792470f981af9a1
[platform/upstream/nodejs.git] / deps / npm / node_modules / request / tests / test-tunnel.js
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
7 var server = require('./server')
8   , assert = require('assert')
9   , request = require('../index')
10   , fs = require('fs')
11   , path = require('path')
12   , caFile = path.resolve(__dirname, 'ssl/npm-ca.crt')
13   , ca = fs.readFileSync(caFile)
14   , child_process = require('child_process')
15   , sqConf = path.resolve(__dirname, 'squid.conf')
16   , sqArgs = ['-f', sqConf, '-N', '-d', '5']
17   , proxy = 'http://localhost:3128'
18   , hadError = null
19
20 var squid = child_process.spawn('squid', sqArgs);
21 var ready = false
22
23 squid.stderr.on('data', function (c) {
24   console.error('SQUIDERR ' + c.toString().trim().split('\n')
25                .join('\nSQUIDERR '))
26   ready = c.toString().match(/ready to serve requests|Accepting HTTP Socket connections/i)
27 })
28
29 squid.stdout.on('data', function (c) {
30   console.error('SQUIDOUT ' + c.toString().trim().split('\n')
31                .join('\nSQUIDOUT '))
32 })
33
34 squid.on('error', function (c) {
35   console.error('squid: error '+c)
36   if (c && !ready) {
37     notInstalled()
38     return
39   }
40 })
41
42 squid.on('exit', function (c) {
43   console.error('squid: exit '+c)
44   if (c && !ready) {
45     notInstalled()
46     return
47   }
48
49   if (c) {
50     hadError = hadError || new Error('Squid exited with '+c)
51   }
52   if (hadError) throw hadError
53 })
54
55 setTimeout(function F () {
56   if (!ready) return setTimeout(F, 100)
57   request({ uri: 'https://registry.npmjs.org/'
58           , proxy: 'http://localhost:3128'
59           , strictSSL: true
60           , ca: ca
61           , json: true }, function (er, body) {
62     hadError = er
63     console.log(er || typeof body)
64     if (!er) console.log("ok")
65     squid.kill('SIGKILL')
66   })
67 }, 100)
68
69 function notInstalled() {
70   console.error('squid must be installed to run this test.')
71   console.error('skipping this test. please install squid and run again if you need to test tunneling.')
72   c = null
73   hadError = null
74   process.exit(0)
75 }