From 6d4db658e9685ce44b1c8d4b7ed3d15812adf6c2 Mon Sep 17 00:00:00 2001 From: Matias Lopez Date: Sun, 13 Sep 2020 12:36:46 -0400 Subject: [PATCH] test: fix tests in download.js that need promises Some tests are calling the recently converted promise apis, this converts them to use the new format. --- test/test-download.js | 196 ++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 122 deletions(-) diff --git a/test/test-download.js b/test/test-download.js index fe373e3280..8535f9f098 100644 --- a/test/test-download.js +++ b/test/test-download.js @@ -14,191 +14,143 @@ const log = require('npmlog') log.level = 'warn' -test('download over http', function (t) { +test('download over http', (t) => { t.plan(2) - var server = http.createServer(function (req, res) { - t.strictEqual(req.headers['user-agent'], - 'node-gyp v42 (node ' + process.version + ')') + const server = http.createServer((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 = { + const host = 'localhost' + return new Promise(resolve => server.listen(0, host, async () => { + const { port } = server.address() + const 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') - }) - }) - }) + const url = `http://${host}:${port}` + const res = await install.test.download(gyp, {}, url) + t.strictEqual(await res.text(), 'ok') + resolve() + })) }) -test('download over https with custom ca', function (t) { +test('download over https with custom ca', async (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') + const [cert, key] = await Promise.all([ + fs.promises.readFile(path.join(__dirname, 'fixtures/server.crt'), 'utf8'), + fs.promises.readFile(path.join(__dirname, 'fixtures/server.key'), 'utf8') + ]) - var cafile = path.join(__dirname, '/fixtures/ca.crt') - var ca = install.test.readCAFile(cafile) + const cafile = path.join(__dirname, '/fixtures/ca.crt') + const ca = await 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 + ')') + const options = { ca: ca, cert: cert, key: key } + const server = https.createServer(options, (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 - }) + server.on('clientError', (err) => { throw err }) - var host = 'localhost' - server.listen(8000, host, function () { - var port = this.address().port - var gyp = { - opts: { cafile: cafile }, + const host = 'localhost' + return new Promise(resolve => server.listen(0, host, async () => { + const { port } = server.address() + const gyp = { + opts: { 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') - }) - }) - }) + const url = `https://${host}:${port}` + const res = await install.test.download(gyp, {}, url) + t.strictEqual(await res.text(), 'ok') + resolve() + })) }) -test('download over http with proxy', function (t) { +test('download over http with proxy', (t) => { t.plan(2) - var server = http.createServer(function (req, res) { - t.strictEqual(req.headers['user-agent'], - 'node-gyp v42 (node ' + process.version + ')') + const server = http.createServer((_, res) => { res.end('ok') - pserver.close(function () { - server.close() - }) + pserver.close(() => { server.close() }) }) - var pserver = http.createServer(function (req, res) { - t.strictEqual(req.headers['user-agent'], - 'node-gyp v42 (node ' + process.version + ')') + const pserver = http.createServer((req, res) => { + t.strictEqual(req.headers['user-agent'], `node-gyp v42 (node ${process.version})`) res.end('proxy ok') - server.close(function () { - pserver.close() - }) + server.close(() => { pserver.close() }) }) - var host = 'localhost' - server.listen(0, host, function () { - var port = this.address().port - pserver.listen(port + 1, host, function () { - var gyp = { + const host = 'localhost' + return new Promise(resolve => server.listen(0, host, () => { + const { port } = server.address() + pserver.listen(port + 1, host, async () => { + const gyp = { opts: { - proxy: 'http://' + host + ':' + (port + 1) + 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') - }) - }) + const url = `http://${host}:${port}` + const res = await install.test.download(gyp, {}, url) + t.strictEqual(await res.text(), 'proxy ok') + resolve() }) - }) + })) }) -test('download over http with noproxy', function (t) { +test('download over http with noproxy', (t) => { t.plan(2) - var server = http.createServer(function (req, res) { - t.strictEqual(req.headers['user-agent'], - 'node-gyp v42 (node ' + process.version + ')') + const server = http.createServer((req, res) => { + t.strictEqual(req.headers['user-agent'], `node-gyp v42 (node ${process.version})`) res.end('ok') - pserver.close(function () { - server.close() - }) + pserver.close(() => { server.close() }) }) - var pserver = http.createServer(function (req, res) { - t.strictEqual(req.headers['user-agent'], - 'node-gyp v42 (node ' + process.version + ')') + const pserver = http.createServer((_, res) => { res.end('proxy ok') - server.close(function () { - pserver.close() - }) + server.close(() => { pserver.close() }) }) - var host = 'localhost' - server.listen(0, host, function () { - var port = this.address().port - pserver.listen(port + 1, host, function () { - var gyp = { + const host = 'localhost' + return new Promise(resolve => server.listen(0, host, () => { + const { port } = server.address() + pserver.listen(port + 1, host, async () => { + const gyp = { opts: { - proxy: 'http://' + host + ':' + (port + 1), - noproxy: 'localhost' + proxy: `http://${host}:${port + 1}`, + noproxy: host }, 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') - }) - }) + const url = `http://${host}:${port}` + const res = await install.test.download(gyp, {}, url) + t.strictEqual(await res.text(), 'ok') + resolve() }) - }) + })) }) -test('download with missing cafile', function (t) { +test('download with missing cafile', async (t) => { t.plan(1) - var gyp = { + const gyp = { opts: { cafile: 'no.such.file' } } try { - install.test.download(gyp, {}, 'http://bad/') + await 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')) +test('check certificate splitting', async (t) => { + const cas = await install.test.readCAFile(path.join(__dirname, 'fixtures/ca-bundle.crt')) t.plan(2) t.strictEqual(cas.length, 2) t.notStrictEqual(cas[0], cas[1]) @@ -206,7 +158,7 @@ test('check certificate splitting', function (t) { // only run this test if we are running a version of Node with predictable version path behavior -test('download headers (actual)', function (t) { +test('download headers (actual)', async (t) => { if (process.env.FAST_TEST || process.release.name !== 'node' || semver.prerelease(process.version) !== null ||