test-download.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict'
  2. var fs = require('fs')
  3. var http = require('http')
  4. var https = require('https')
  5. var test = require('tape')
  6. var install = require('../lib/install')
  7. test('download over http', function (t) {
  8. t.plan(2)
  9. var server = http.createServer(function (req, res) {
  10. t.strictEqual(req.headers['user-agent'],
  11. 'node-gyp v42 (node ' + process.version + ')')
  12. res.end('ok')
  13. server.close()
  14. })
  15. var host = '127.0.0.1'
  16. server.listen(0, host, function () {
  17. var port = this.address().port
  18. var gyp = {
  19. opts: {},
  20. version: '42',
  21. }
  22. var url = 'http://' + host + ':' + port
  23. var req = install.test.download(gyp, {}, url)
  24. req.on('response', function (res) {
  25. var body = ''
  26. res.setEncoding('utf8')
  27. res.on('data', function(data) {
  28. body += data
  29. })
  30. res.on('end', function() {
  31. t.strictEqual(body, 'ok')
  32. })
  33. })
  34. })
  35. })
  36. test('download over https with custom ca', function (t) {
  37. t.plan(3)
  38. var cert = fs.readFileSync(__dirname + '/fixtures/server.crt', 'utf8')
  39. var key = fs.readFileSync(__dirname + '/fixtures/server.key', 'utf8')
  40. var cafile = __dirname + '/fixtures/ca.crt'
  41. var ca = install.test.readCAFile(cafile)
  42. t.strictEqual(ca.length, 1)
  43. var options = { ca: ca, cert: cert, key: key }
  44. var server = https.createServer(options, function (req, res) {
  45. t.strictEqual(req.headers['user-agent'],
  46. 'node-gyp v42 (node ' + process.version + ')')
  47. res.end('ok')
  48. server.close()
  49. })
  50. server.on('clientError', function (err) {
  51. throw err
  52. })
  53. var host = '127.0.0.1'
  54. server.listen(8000, host, function () {
  55. var port = this.address().port
  56. var gyp = {
  57. opts: { cafile: cafile },
  58. version: '42',
  59. }
  60. var url = 'https://' + host + ':' + port
  61. var req = install.test.download(gyp, {}, url)
  62. req.on('response', function (res) {
  63. var body = ''
  64. res.setEncoding('utf8')
  65. res.on('data', function(data) {
  66. body += data
  67. })
  68. res.on('end', function() {
  69. t.strictEqual(body, 'ok')
  70. })
  71. })
  72. })
  73. })
  74. test('download with missing cafile', function (t) {
  75. t.plan(1)
  76. var gyp = {
  77. opts: { cafile: 'no.such.file' },
  78. }
  79. try {
  80. install.test.download(gyp, {}, 'http://bad/')
  81. } catch (e) {
  82. t.ok(/no.such.file/.test(e.message))
  83. }
  84. })
  85. test('check certificate splitting', function (t) {
  86. var cas = install.test.readCAFile(__dirname + '/fixtures/ca-bundle.crt')
  87. t.plan(2)
  88. t.strictEqual(cas.length, 2)
  89. t.notStrictEqual(cas[0], cas[1])
  90. })