Skip to content

Commit

Permalink
test: fix tests in download.js that need promises
Browse files Browse the repository at this point in the history
Some tests are calling the recently converted promise apis, this
converts them to use the new format.
  • Loading branch information
imatlopez committed Sep 13, 2020
1 parent be1ab9d commit 257a56e
Showing 1 changed file with 74 additions and 122 deletions.
196 changes: 74 additions & 122 deletions test/test-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,199 +14,151 @@ 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])
})

// 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 ||
Expand Down

0 comments on commit 257a56e

Please sign in to comment.