1'use strict'; 2const common = require('../common'); 3if (!common.hasCrypto) 4 common.skip('missing crypto'); 5 6const assert = require('assert'); 7const https = require('https'); 8const { once } = require('events'); 9const Agent = https.Agent; 10const fixtures = require('../common/fixtures'); 11 12const { getEventListeners } = require('events'); 13const agent = new Agent(); 14 15const options = { 16 key: fixtures.readKey('agent1-key.pem'), 17 cert: fixtures.readKey('agent1-cert.pem') 18}; 19 20const server = https.createServer(options); 21 22server.listen(0, common.mustCall(async () => { 23 const port = server.address().port; 24 const host = 'localhost'; 25 const options = { 26 port: port, 27 host: host, 28 rejectUnauthorized: false, 29 _agentKey: agent.getName({ port, host }) 30 }; 31 32 async function postCreateConnection() { 33 const ac = new AbortController(); 34 const { signal } = ac; 35 const connection = agent.createConnection({ ...options, signal }); 36 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 37 ac.abort(); 38 const [err] = await once(connection, 'error'); 39 assert.strictEqual(err.name, 'AbortError'); 40 } 41 42 async function preCreateConnection() { 43 const ac = new AbortController(); 44 const { signal } = ac; 45 ac.abort(); 46 const connection = agent.createConnection({ ...options, signal }); 47 const [err] = await once(connection, 'error'); 48 assert.strictEqual(err.name, 'AbortError'); 49 } 50 51 52 async function agentAsParam() { 53 const ac = new AbortController(); 54 const { signal } = ac; 55 const request = https.get({ 56 port: server.address().port, 57 path: '/hello', 58 agent: agent, 59 signal, 60 }); 61 assert.strictEqual(getEventListeners(signal, 'abort').length, 1); 62 ac.abort(); 63 const [err] = await once(request, 'error'); 64 assert.strictEqual(err.name, 'AbortError'); 65 } 66 67 async function agentAsParamPreAbort() { 68 const ac = new AbortController(); 69 const { signal } = ac; 70 ac.abort(); 71 const request = https.get({ 72 port: server.address().port, 73 path: '/hello', 74 agent: agent, 75 signal, 76 }); 77 assert.strictEqual(getEventListeners(signal, 'abort').length, 0); 78 const [err] = await once(request, 'error'); 79 assert.strictEqual(err.name, 'AbortError'); 80 } 81 82 await postCreateConnection(); 83 await preCreateConnection(); 84 await agentAsParam(); 85 await agentAsParamPreAbort(); 86 server.close(); 87})); 88