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