Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test-download.js « test « node-gyp « node_modules - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe373e3280ce6665bc94712568aefd390e0b2a48 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
'use strict'

const test = require('tap').test
const fs = require('fs')
const path = require('path')
const http = require('http')
const https = require('https')
const install = require('../lib/install')
const semver = require('semver')
const devDir = require('./common').devDir()
const rimraf = require('rimraf')
const gyp = require('../lib/node-gyp')
const log = require('npmlog')

log.level = 'warn'

test('download over http', function (t) {
  t.plan(2)

  var server = http.createServer(function (req, res) {
    t.strictEqual(req.headers['user-agent'],
      'node-gyp v42 (node ' + process.version + ')')
    res.end('ok')
    server.close()
  })

  var host = 'localhost'
  server.listen(0, host, function () {
    var port = this.address().port
    var gyp = {
      opts: {},
      version: '42'
    }
    var url = 'http://' + host + ':' + port
    var req = install.test.download(gyp, {}, url)
    req.on('response', function (res) {
      var body = ''
      res.setEncoding('utf8')
      res.on('data', function (data) {
        body += data
      })
      res.on('end', function () {
        t.strictEqual(body, 'ok')
      })
    })
  })
})

test('download over https with custom ca', function (t) {
  t.plan(3)

  var cert = fs.readFileSync(path.join(__dirname, 'fixtures/server.crt'), 'utf8')
  var key = fs.readFileSync(path.join(__dirname, 'fixtures/server.key'), 'utf8')

  var cafile = path.join(__dirname, '/fixtures/ca.crt')
  var ca = install.test.readCAFile(cafile)
  t.strictEqual(ca.length, 1)

  var options = { ca: ca, cert: cert, key: key }
  var server = https.createServer(options, function (req, res) {
    t.strictEqual(req.headers['user-agent'],
      'node-gyp v42 (node ' + process.version + ')')
    res.end('ok')
    server.close()
  })

  server.on('clientError', function (err) {
    throw err
  })

  var host = 'localhost'
  server.listen(8000, host, function () {
    var port = this.address().port
    var gyp = {
      opts: { cafile: cafile },
      version: '42'
    }
    var url = 'https://' + host + ':' + port
    var req = install.test.download(gyp, {}, url)
    req.on('response', function (res) {
      var body = ''
      res.setEncoding('utf8')
      res.on('data', function (data) {
        body += data
      })
      res.on('end', function () {
        t.strictEqual(body, 'ok')
      })
    })
  })
})

test('download over http with proxy', function (t) {
  t.plan(2)

  var server = http.createServer(function (req, res) {
    t.strictEqual(req.headers['user-agent'],
      'node-gyp v42 (node ' + process.version + ')')
    res.end('ok')
    pserver.close(function () {
      server.close()
    })
  })

  var pserver = http.createServer(function (req, res) {
    t.strictEqual(req.headers['user-agent'],
      'node-gyp v42 (node ' + process.version + ')')
    res.end('proxy ok')
    server.close(function () {
      pserver.close()
    })
  })

  var host = 'localhost'
  server.listen(0, host, function () {
    var port = this.address().port
    pserver.listen(port + 1, host, function () {
      var gyp = {
        opts: {
          proxy: 'http://' + host + ':' + (port + 1)
        },
        version: '42'
      }
      var url = 'http://' + host + ':' + port
      var req = install.test.download(gyp, {}, url)
      req.on('response', function (res) {
        var body = ''
        res.setEncoding('utf8')
        res.on('data', function (data) {
          body += data
        })
        res.on('end', function () {
          t.strictEqual(body, 'proxy ok')
        })
      })
    })
  })
})

test('download over http with noproxy', function (t) {
  t.plan(2)

  var server = http.createServer(function (req, res) {
    t.strictEqual(req.headers['user-agent'],
      'node-gyp v42 (node ' + process.version + ')')
    res.end('ok')
    pserver.close(function () {
      server.close()
    })
  })

  var pserver = http.createServer(function (req, res) {
    t.strictEqual(req.headers['user-agent'],
      'node-gyp v42 (node ' + process.version + ')')
    res.end('proxy ok')
    server.close(function () {
      pserver.close()
    })
  })

  var host = 'localhost'
  server.listen(0, host, function () {
    var port = this.address().port
    pserver.listen(port + 1, host, function () {
      var gyp = {
        opts: {
          proxy: 'http://' + host + ':' + (port + 1),
          noproxy: 'localhost'
        },
        version: '42'
      }
      var url = 'http://' + host + ':' + port
      var req = install.test.download(gyp, {}, url)
      req.on('response', function (res) {
        var body = ''
        res.setEncoding('utf8')
        res.on('data', function (data) {
          body += data
        })
        res.on('end', function () {
          t.strictEqual(body, 'ok')
        })
      })
    })
  })
})

test('download with missing cafile', function (t) {
  t.plan(1)
  var gyp = {
    opts: { cafile: 'no.such.file' }
  }
  try {
    install.test.download(gyp, {}, 'http://bad/')
  } catch (e) {
    t.ok(/no.such.file/.test(e.message))
  }
})

test('check certificate splitting', function (t) {
  var cas = install.test.readCAFile(path.join(__dirname, 'fixtures/ca-bundle.crt'))
  t.plan(2)
  t.strictEqual(cas.length, 2)
  t.notStrictEqual(cas[0], cas[1])
})

// only run this test if we are running a version of Node with predictable version path behavior

test('download headers (actual)', function (t) {
  if (process.env.FAST_TEST ||
      process.release.name !== 'node' ||
      semver.prerelease(process.version) !== null ||
      semver.satisfies(process.version, '<10')) {
    return t.skip('Skipping actual download of headers due to test environment configuration')
  }

  t.plan(17)

  const expectedDir = path.join(devDir, process.version.replace(/^v/, ''))
  rimraf(expectedDir, (err) => {
    t.ifError(err)

    const prog = gyp()
    prog.parseArgv([])
    prog.devDir = devDir
    log.level = 'warn'
    install(prog, [], (err) => {
      t.ifError(err)

      fs.readFile(path.join(expectedDir, 'installVersion'), 'utf8', (err, data) => {
        t.ifError(err)
        t.strictEqual(data, '9\n', 'correct installVersion')
      })

      fs.readdir(path.join(expectedDir, 'include/node'), (err, list) => {
        t.ifError(err)

        t.ok(list.includes('common.gypi'))
        t.ok(list.includes('config.gypi'))
        t.ok(list.includes('node.h'))
        t.ok(list.includes('node_version.h'))
        t.ok(list.includes('openssl'))
        t.ok(list.includes('uv'))
        t.ok(list.includes('uv.h'))
        t.ok(list.includes('v8-platform.h'))
        t.ok(list.includes('v8.h'))
        t.ok(list.includes('zlib.h'))
      })

      fs.readFile(path.join(expectedDir, 'include/node/node_version.h'), 'utf8', (err, contents) => {
        t.ifError(err)

        const lines = contents.split('\n')

        // extract the 3 version parts from the defines to build a valid version string and
        // and check them against our current env version
        const version = ['major', 'minor', 'patch'].reduce((version, type) => {
          const re = new RegExp(`^#define\\sNODE_${type.toUpperCase()}_VERSION`)
          const line = lines.find((l) => re.test(l))
          const i = line ? parseInt(line.replace(/^[^0-9]+([0-9]+).*$/, '$1'), 10) : 'ERROR'
          return `${version}${type !== 'major' ? '.' : 'v'}${i}`
        }, '')

        t.strictEqual(version, process.version)
      })
    })
  })
})