|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +var fs = require('fs') |
3 | 4 | var http = require('http') |
| 5 | +var https = require('https') |
4 | 6 | var test = require('tape') |
5 | 7 | var install = require('../lib/install') |
6 | 8 |
|
@@ -35,3 +37,66 @@ test('download over http', function (t) { |
35 | 37 | }) |
36 | 38 | }) |
37 | 39 | }) |
| 40 | + |
| 41 | +test('download over https with custom ca', function (t) { |
| 42 | + t.plan(3) |
| 43 | + |
| 44 | + var cert = fs.readFileSync(__dirname + '/fixtures/server.crt', 'utf8') |
| 45 | + var key = fs.readFileSync(__dirname + '/fixtures/server.key', 'utf8') |
| 46 | + |
| 47 | + var cafile = __dirname + '/fixtures/ca.crt' |
| 48 | + var ca = install.test.readCAFile(cafile) |
| 49 | + t.strictEqual(ca.length, 1) |
| 50 | + |
| 51 | + var options = { ca: ca, cert: cert, key: key } |
| 52 | + var server = https.createServer(options, function (req, res) { |
| 53 | + t.strictEqual(req.headers['user-agent'], |
| 54 | + 'node-gyp v42 (node ' + process.version + ')') |
| 55 | + res.end('ok') |
| 56 | + server.close() |
| 57 | + }) |
| 58 | + |
| 59 | + server.on('clientError', function (err) { |
| 60 | + throw err |
| 61 | + }) |
| 62 | + |
| 63 | + var host = '127.0.0.1' |
| 64 | + server.listen(8000, host, function () { |
| 65 | + var port = this.address().port |
| 66 | + var gyp = { |
| 67 | + opts: { cafile: cafile }, |
| 68 | + version: '42', |
| 69 | + } |
| 70 | + var url = 'https://' + host + ':' + port |
| 71 | + var req = install.test.download(gyp, {}, url) |
| 72 | + req.on('response', function (res) { |
| 73 | + var body = '' |
| 74 | + res.setEncoding('utf8') |
| 75 | + res.on('data', function(data) { |
| 76 | + body += data |
| 77 | + }) |
| 78 | + res.on('end', function() { |
| 79 | + t.strictEqual(body, 'ok') |
| 80 | + }) |
| 81 | + }) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +test('download with missing cafile', function (t) { |
| 86 | + t.plan(1) |
| 87 | + var gyp = { |
| 88 | + opts: { cafile: 'no.such.file' }, |
| 89 | + } |
| 90 | + try { |
| 91 | + install.test.download(gyp, {}, 'http://bad/') |
| 92 | + } catch (e) { |
| 93 | + t.ok(/no.such.file/.test(e.message)) |
| 94 | + } |
| 95 | +}) |
| 96 | + |
| 97 | +test('check certificate splitting', function (t) { |
| 98 | + var cas = install.test.readCAFile(__dirname + '/fixtures/ca-bundle.crt') |
| 99 | + t.plan(2) |
| 100 | + t.strictEqual(cas.length, 2) |
| 101 | + t.notStrictEqual(cas[0], cas[1]) |
| 102 | +}) |
0 commit comments