node/test/parallel/test-https-abortcontroller.js
Nitzan Uziely 711e210d35 http: fix double AbortSignal registration
Fix an issue where AbortSignals are registered twice

PR-URL: https://github.com/nodejs/node/pull/37730
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2021-03-20 12:52:04 -07:00

41 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const https = require('https');
const assert = require('assert');
const { once, getEventListeners } = require('events');
const options = {
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem')
};
(async () => {
const { port, server } = await new Promise((resolve) => {
const server = https.createServer(options, () => {});
server.listen(0, () => resolve({ port: server.address().port, server }));
});
try {
const ac = new AbortController();
const req = https.request({
host: 'localhost',
port,
path: '/',
method: 'GET',
rejectUnauthorized: false,
signal: ac.signal,
});
assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 1);
process.nextTick(() => ac.abort());
const [ err ] = await once(req, 'error');
assert.strictEqual(err.name, 'AbortError');
assert.strictEqual(err.code, 'ABORT_ERR');
} finally {
server.close();
}
})().then(common.mustCall());