11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Fixes: https://github.com/nodejs/node/issues/42713
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciif (!common.hasCrypto) {
61cb0ef41Sopenharmony_ci  // Remove require('assert').fail when issue is fixed and test
71cb0ef41Sopenharmony_ci  // is moved out of the known_issues directory.
81cb0ef41Sopenharmony_ci  require('assert').fail('missing crypto');
91cb0ef41Sopenharmony_ci  common.skip('missing crypto');
101cb0ef41Sopenharmony_ci}
111cb0ef41Sopenharmony_ciconst assert = require('assert');
121cb0ef41Sopenharmony_ciconst http2 = require('http2');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst {
151cb0ef41Sopenharmony_ci  HTTP2_HEADER_PATH,
161cb0ef41Sopenharmony_ci  HTTP2_HEADER_STATUS,
171cb0ef41Sopenharmony_ci  HTTP2_HEADER_METHOD,
181cb0ef41Sopenharmony_ci} = http2.constants;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst server = http2.createServer();
211cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
221cb0ef41Sopenharmony_ci  server.close();
231cb0ef41Sopenharmony_ci  stream.session.close();
241cb0ef41Sopenharmony_ci  stream.on('wantTrailers', common.mustCall(() => {
251cb0ef41Sopenharmony_ci    stream.sendTrailers({ xyz: 'abc' });
261cb0ef41Sopenharmony_ci  }));
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  stream.respond({ [HTTP2_HEADER_STATUS]: 200 }, { waitForTrailers: true });
291cb0ef41Sopenharmony_ci  stream.write('some data');
301cb0ef41Sopenharmony_ci  stream.end();
311cb0ef41Sopenharmony_ci}));
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
341cb0ef41Sopenharmony_ci  const port = server.address().port;
351cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${port}`);
361cb0ef41Sopenharmony_ci  client.socket.on('close', common.mustCall());
371cb0ef41Sopenharmony_ci  const req = client.request({
381cb0ef41Sopenharmony_ci    [HTTP2_HEADER_PATH]: '/',
391cb0ef41Sopenharmony_ci    [HTTP2_HEADER_METHOD]: 'POST',
401cb0ef41Sopenharmony_ci  });
411cb0ef41Sopenharmony_ci  req.end();
421cb0ef41Sopenharmony_ci  req.on('response', common.mustCall());
431cb0ef41Sopenharmony_ci  let data = '';
441cb0ef41Sopenharmony_ci  req.on('data', (chunk) => {
451cb0ef41Sopenharmony_ci    data += chunk;
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci  req.on('end', common.mustCall(() => {
481cb0ef41Sopenharmony_ci    assert.strictEqual(data, 'some data');
491cb0ef41Sopenharmony_ci  }));
501cb0ef41Sopenharmony_ci  req.on('trailers', common.mustCall((headers) => {
511cb0ef41Sopenharmony_ci    assert.strictEqual(headers.xyz, 'abc');
521cb0ef41Sopenharmony_ci  }));
531cb0ef41Sopenharmony_ci  req.on('close', common.mustCall());
541cb0ef41Sopenharmony_ci}));
55