Initial commit
[yaffs-website] / node_modules / node-gyp / test / test-download.js
1 'use strict'
2
3 var fs = require('fs')
4 var http = require('http')
5 var https = require('https')
6 var test = require('tape')
7 var install = require('../lib/install')
8
9 test('download over http', function (t) {
10   t.plan(2)
11
12   var server = http.createServer(function (req, res) {
13     t.strictEqual(req.headers['user-agent'],
14                   'node-gyp v42 (node ' + process.version + ')')
15     res.end('ok')
16     server.close()
17   })
18
19   var host = '127.0.0.1'
20   server.listen(0, host, function () {
21     var port = this.address().port
22     var gyp = {
23       opts: {},
24       version: '42',
25     }
26     var url = 'http://' + host + ':' + port
27     var req = install.test.download(gyp, {}, url)
28     req.on('response', function (res) {
29       var body = ''
30       res.setEncoding('utf8')
31       res.on('data', function(data) {
32         body += data
33       })
34       res.on('end', function() {
35         t.strictEqual(body, 'ok')
36       })
37     })
38   })
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 })