11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst https = require('https');
81cb0ef41Sopenharmony_ciconst { once } = require('events');
91cb0ef41Sopenharmony_ciconst Agent = https.Agent;
101cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst { getEventListeners } = require('events');
131cb0ef41Sopenharmony_ciconst agent = new Agent();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst options = {
161cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
171cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
181cb0ef41Sopenharmony_ci};
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst server = https.createServer(options);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(async () => {
231cb0ef41Sopenharmony_ci  const port = server.address().port;
241cb0ef41Sopenharmony_ci  const host = 'localhost';
251cb0ef41Sopenharmony_ci  const options = {
261cb0ef41Sopenharmony_ci    port: port,
271cb0ef41Sopenharmony_ci    host: host,
281cb0ef41Sopenharmony_ci    rejectUnauthorized: false,
291cb0ef41Sopenharmony_ci    _agentKey: agent.getName({ port, host })
301cb0ef41Sopenharmony_ci  };
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  async function postCreateConnection() {
331cb0ef41Sopenharmony_ci    const ac = new AbortController();
341cb0ef41Sopenharmony_ci    const { signal } = ac;
351cb0ef41Sopenharmony_ci    const connection = agent.createConnection({ ...options, signal });
361cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
371cb0ef41Sopenharmony_ci    ac.abort();
381cb0ef41Sopenharmony_ci    const [err] = await once(connection, 'error');
391cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  async function preCreateConnection() {
431cb0ef41Sopenharmony_ci    const ac = new AbortController();
441cb0ef41Sopenharmony_ci    const { signal } = ac;
451cb0ef41Sopenharmony_ci    ac.abort();
461cb0ef41Sopenharmony_ci    const connection = agent.createConnection({ ...options, signal });
471cb0ef41Sopenharmony_ci    const [err] = await once(connection, 'error');
481cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  async function agentAsParam() {
531cb0ef41Sopenharmony_ci    const ac = new AbortController();
541cb0ef41Sopenharmony_ci    const { signal } = ac;
551cb0ef41Sopenharmony_ci    const request = https.get({
561cb0ef41Sopenharmony_ci      port: server.address().port,
571cb0ef41Sopenharmony_ci      path: '/hello',
581cb0ef41Sopenharmony_ci      agent: agent,
591cb0ef41Sopenharmony_ci      signal,
601cb0ef41Sopenharmony_ci    });
611cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
621cb0ef41Sopenharmony_ci    ac.abort();
631cb0ef41Sopenharmony_ci    const [err] = await once(request, 'error');
641cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
651cb0ef41Sopenharmony_ci  }
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  async function agentAsParamPreAbort() {
681cb0ef41Sopenharmony_ci    const ac = new AbortController();
691cb0ef41Sopenharmony_ci    const { signal } = ac;
701cb0ef41Sopenharmony_ci    ac.abort();
711cb0ef41Sopenharmony_ci    const request = https.get({
721cb0ef41Sopenharmony_ci      port: server.address().port,
731cb0ef41Sopenharmony_ci      path: '/hello',
741cb0ef41Sopenharmony_ci      agent: agent,
751cb0ef41Sopenharmony_ci      signal,
761cb0ef41Sopenharmony_ci    });
771cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
781cb0ef41Sopenharmony_ci    const [err] = await once(request, 'error');
791cb0ef41Sopenharmony_ci    assert.strictEqual(err.name, 'AbortError');
801cb0ef41Sopenharmony_ci  }
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  await postCreateConnection();
831cb0ef41Sopenharmony_ci  await preCreateConnection();
841cb0ef41Sopenharmony_ci  await agentAsParam();
851cb0ef41Sopenharmony_ci  await agentAsParamPreAbort();
861cb0ef41Sopenharmony_ci  server.close();
871cb0ef41Sopenharmony_ci}));
88