11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-internals
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst http2 = require('http2');
91cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
101cb0ef41Sopenharmony_ciconst { Http2Stream } = internalBinding('http2');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst server = http2.createServer();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciHttp2Stream.prototype.respond = () => 1;
151cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  // Send headers
181cb0ef41Sopenharmony_ci  stream.respond({ 'content-type': 'text/plain' });
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  // Should throw if headers already sent
211cb0ef41Sopenharmony_ci  assert.throws(
221cb0ef41Sopenharmony_ci    () => stream.respond(),
231cb0ef41Sopenharmony_ci    {
241cb0ef41Sopenharmony_ci      name: 'Error',
251cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_HEADERS_SENT',
261cb0ef41Sopenharmony_ci      message: 'Response has already been initiated.'
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci  );
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // Should throw if stream already destroyed
311cb0ef41Sopenharmony_ci  stream.destroy();
321cb0ef41Sopenharmony_ci  assert.throws(
331cb0ef41Sopenharmony_ci    () => stream.respond(),
341cb0ef41Sopenharmony_ci    {
351cb0ef41Sopenharmony_ci      name: 'Error',
361cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_INVALID_STREAM',
371cb0ef41Sopenharmony_ci      message: 'The stream has been destroyed'
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci  );
401cb0ef41Sopenharmony_ci}));
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
431cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
441cb0ef41Sopenharmony_ci  const req = client.request();
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  req.on('end', common.mustCall(() => {
471cb0ef41Sopenharmony_ci    client.close();
481cb0ef41Sopenharmony_ci    server.close();
491cb0ef41Sopenharmony_ci  }));
501cb0ef41Sopenharmony_ci  req.resume();
511cb0ef41Sopenharmony_ci  req.end();
521cb0ef41Sopenharmony_ci}));
53