Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / testswarm / 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('../main.js')
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/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('exit', function (c) {
35   console.error('squid: exit '+c)
36   if (c && !ready) {
37     console.error('squid must be installed to run this test.')
38     console.error('skipping this test. please install squid and run again if you need to test tunneling.')
39     c = null
40     hadError = null
41     process.exit(0)
42     return
43   }
44
45   if (c) {
46     hadError = hadError || new Error('Squid exited with '+c)
47   }
48   if (hadError) throw hadError
49 })
50
51 setTimeout(function F () {
52   if (!ready) return setTimeout(F, 100)
53   request({ uri: 'https://registry.npmjs.org/'
54           , proxy: 'http://localhost:3128'
55           , strictSSL: true
56           , ca: ca
57           , json: true }, function (er, body) {
58     hadError = er
59     console.log(er || typeof body)
60     if (!er) console.log("ok")
61     squid.kill('SIGKILL')
62   })
63 }, 100)