11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst { mustCall } = common;
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst http2 = require('http2');
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst {
131cb0ef41Sopenharmony_ci  HTTP2_HEADER_PATH,
141cb0ef41Sopenharmony_ci  HTTP2_HEADER_METHOD,
151cb0ef41Sopenharmony_ci} = http2.constants;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// This tests verifies that calling `req.socket.destroy()` via
181cb0ef41Sopenharmony_ci// setImmediate does not crash.
191cb0ef41Sopenharmony_ci// Fixes https://github.com/nodejs/node/issues/22855.
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst app = http2.createServer(mustCall((req, res) => {
221cb0ef41Sopenharmony_ci  res.end('hello');
231cb0ef41Sopenharmony_ci  setImmediate(() => req.socket.destroy());
241cb0ef41Sopenharmony_ci}));
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciapp.listen(0, mustCall(() => {
271cb0ef41Sopenharmony_ci  const session = http2.connect(`http://localhost:${app.address().port}`);
281cb0ef41Sopenharmony_ci  const request = session.request({
291cb0ef41Sopenharmony_ci    [HTTP2_HEADER_PATH]: '/',
301cb0ef41Sopenharmony_ci    [HTTP2_HEADER_METHOD]: 'get'
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci  request.once('response', mustCall((headers, flags) => {
331cb0ef41Sopenharmony_ci    let data = '';
341cb0ef41Sopenharmony_ci    request.on('data', (chunk) => { data += chunk; });
351cb0ef41Sopenharmony_ci    request.on('end', mustCall(() => {
361cb0ef41Sopenharmony_ci      assert.strictEqual(data, 'hello');
371cb0ef41Sopenharmony_ci      session.close();
381cb0ef41Sopenharmony_ci      app.close();
391cb0ef41Sopenharmony_ci    }));
401cb0ef41Sopenharmony_ci  }));
411cb0ef41Sopenharmony_ci  request.end();
421cb0ef41Sopenharmony_ci}));
43