11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto) common.skip('missing crypto');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// An HTTP Agent reuses a TLSSocket, and makes a failed call to `asyncReset`.
71cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/13045
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst https = require('https');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst serverOptions = {
131cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
141cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem'),
151cb0ef41Sopenharmony_ci  ca: fixtures.readKey('ca1-cert.pem')
161cb0ef41Sopenharmony_ci};
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst server = https.createServer(
191cb0ef41Sopenharmony_ci  serverOptions,
201cb0ef41Sopenharmony_ci  common.mustCall((req, res) => {
211cb0ef41Sopenharmony_ci    res.end('hello world\n');
221cb0ef41Sopenharmony_ci  }, 2)
231cb0ef41Sopenharmony_ci);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciserver.listen(
261cb0ef41Sopenharmony_ci  0,
271cb0ef41Sopenharmony_ci  common.mustCall(function() {
281cb0ef41Sopenharmony_ci    const port = this.address().port;
291cb0ef41Sopenharmony_ci    const clientOptions = {
301cb0ef41Sopenharmony_ci      agent: new https.Agent({
311cb0ef41Sopenharmony_ci        keepAlive: true,
321cb0ef41Sopenharmony_ci        rejectUnauthorized: false
331cb0ef41Sopenharmony_ci      }),
341cb0ef41Sopenharmony_ci      port: port
351cb0ef41Sopenharmony_ci    };
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    const req = https.get(
381cb0ef41Sopenharmony_ci      clientOptions,
391cb0ef41Sopenharmony_ci      common.mustCall((res) => {
401cb0ef41Sopenharmony_ci        assert.strictEqual(res.statusCode, 200);
411cb0ef41Sopenharmony_ci        res.on('error', (err) => assert.fail(err));
421cb0ef41Sopenharmony_ci        res.socket.on('error', (err) => assert.fail(err));
431cb0ef41Sopenharmony_ci        res.resume();
441cb0ef41Sopenharmony_ci        // Drain the socket and wait for it to be free to reuse
451cb0ef41Sopenharmony_ci        res.socket.once('free', () => {
461cb0ef41Sopenharmony_ci          // This is the pain point. Internally the Agent will call
471cb0ef41Sopenharmony_ci          // `socket._handle.asyncReset()` and if the _handle does not implement
481cb0ef41Sopenharmony_ci          // `asyncReset` this will throw TypeError
491cb0ef41Sopenharmony_ci          const req2 = https.get(
501cb0ef41Sopenharmony_ci            clientOptions,
511cb0ef41Sopenharmony_ci            common.mustCall((res2) => {
521cb0ef41Sopenharmony_ci              assert.strictEqual(res.statusCode, 200);
531cb0ef41Sopenharmony_ci              res2.on('error', (err) => assert.fail(err));
541cb0ef41Sopenharmony_ci              res2.socket.on('error', (err) => assert.fail(err));
551cb0ef41Sopenharmony_ci              // This should be the end of the test
561cb0ef41Sopenharmony_ci              res2.destroy();
571cb0ef41Sopenharmony_ci              server.close();
581cb0ef41Sopenharmony_ci            })
591cb0ef41Sopenharmony_ci          );
601cb0ef41Sopenharmony_ci          req2.on('error', (err) => assert.fail(err));
611cb0ef41Sopenharmony_ci        });
621cb0ef41Sopenharmony_ci      })
631cb0ef41Sopenharmony_ci    );
641cb0ef41Sopenharmony_ci    req.on('error', (err) => assert.fail(err));
651cb0ef41Sopenharmony_ci  })
661cb0ef41Sopenharmony_ci);
67