11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ciconst h2 = require('http2');
91cb0ef41Sopenharmony_ciconst { kSocket } = require('internal/http2/util');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst body =
121cb0ef41Sopenharmony_ci  '<html><head></head><body><h1>this is some data</h2></body></html>';
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst server = h2.createServer();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// We use the lower-level API here
171cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
181cb0ef41Sopenharmony_ci  stream.on('aborted', common.mustCall());
191cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall());
201cb0ef41Sopenharmony_ci  stream.respond();
211cb0ef41Sopenharmony_ci  stream.write(body);
221cb0ef41Sopenharmony_ci  // Purposefully do not end()
231cb0ef41Sopenharmony_ci}));
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() {
261cb0ef41Sopenharmony_ci  const client = h2.connect(`http://localhost:${this.address().port}`);
271cb0ef41Sopenharmony_ci  const req = client.request();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  req.on('response', common.mustCall(() => {
301cb0ef41Sopenharmony_ci    // Send a premature socket close
311cb0ef41Sopenharmony_ci    client[kSocket].destroy();
321cb0ef41Sopenharmony_ci  }));
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  req.resume();
351cb0ef41Sopenharmony_ci  req.on('end', common.mustCall());
361cb0ef41Sopenharmony_ci  req.on('close', common.mustCall(() => server.close()));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  // On the client, the close event must call
391cb0ef41Sopenharmony_ci  client.on('close', common.mustCall());
401cb0ef41Sopenharmony_ci}));
41