11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst http2 = require('http2');
91cb0ef41Sopenharmony_ciconst { inspect } = require('util');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst optionsWithTypeError = {
121cb0ef41Sopenharmony_ci  offset: 'number',
131cb0ef41Sopenharmony_ci  length: 'number',
141cb0ef41Sopenharmony_ci  statCheck: 'function'
151cb0ef41Sopenharmony_ci};
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst types = {
181cb0ef41Sopenharmony_ci  boolean: true,
191cb0ef41Sopenharmony_ci  function: () => {},
201cb0ef41Sopenharmony_ci  number: 1,
211cb0ef41Sopenharmony_ci  object: {},
221cb0ef41Sopenharmony_ci  array: [],
231cb0ef41Sopenharmony_ci  null: null,
241cb0ef41Sopenharmony_ci  symbol: Symbol('test')
251cb0ef41Sopenharmony_ci};
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst fname = fixtures.path('elipses.txt');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst server = http2.createServer();
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // Check for all possible TypeError triggers on options
341cb0ef41Sopenharmony_ci  Object.keys(optionsWithTypeError).forEach((option) => {
351cb0ef41Sopenharmony_ci    Object.keys(types).forEach((type) => {
361cb0ef41Sopenharmony_ci      if (type === optionsWithTypeError[option]) {
371cb0ef41Sopenharmony_ci        return;
381cb0ef41Sopenharmony_ci      }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci      assert.throws(
411cb0ef41Sopenharmony_ci        () => stream.respondWithFile(fname, {
421cb0ef41Sopenharmony_ci          'content-type': 'text/plain'
431cb0ef41Sopenharmony_ci        }, {
441cb0ef41Sopenharmony_ci          [option]: types[type]
451cb0ef41Sopenharmony_ci        }),
461cb0ef41Sopenharmony_ci        {
471cb0ef41Sopenharmony_ci          name: 'TypeError',
481cb0ef41Sopenharmony_ci          code: 'ERR_INVALID_ARG_VALUE',
491cb0ef41Sopenharmony_ci          message: `The property 'options.${option}' is invalid. ` +
501cb0ef41Sopenharmony_ci            `Received ${inspect(types[type])}`
511cb0ef41Sopenharmony_ci        }
521cb0ef41Sopenharmony_ci      );
531cb0ef41Sopenharmony_ci    });
541cb0ef41Sopenharmony_ci  });
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  // Should throw if :status 204, 205 or 304
571cb0ef41Sopenharmony_ci  [204, 205, 304].forEach((status) => assert.throws(
581cb0ef41Sopenharmony_ci    () => stream.respondWithFile(fname, {
591cb0ef41Sopenharmony_ci      'content-type': 'text/plain',
601cb0ef41Sopenharmony_ci      ':status': status,
611cb0ef41Sopenharmony_ci    }),
621cb0ef41Sopenharmony_ci    {
631cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_PAYLOAD_FORBIDDEN',
641cb0ef41Sopenharmony_ci      message: `Responses with ${status} status must not have a payload`
651cb0ef41Sopenharmony_ci    }
661cb0ef41Sopenharmony_ci  ));
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  // Should throw if headers already sent
691cb0ef41Sopenharmony_ci  stream.respond({ ':status': 200 });
701cb0ef41Sopenharmony_ci  assert.throws(
711cb0ef41Sopenharmony_ci    () => stream.respondWithFile(fname, {
721cb0ef41Sopenharmony_ci      'content-type': 'text/plain'
731cb0ef41Sopenharmony_ci    }),
741cb0ef41Sopenharmony_ci    {
751cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_HEADERS_SENT',
761cb0ef41Sopenharmony_ci      message: 'Response has already been initiated.'
771cb0ef41Sopenharmony_ci    }
781cb0ef41Sopenharmony_ci  );
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  // Should throw if stream already destroyed
811cb0ef41Sopenharmony_ci  stream.destroy();
821cb0ef41Sopenharmony_ci  assert.throws(
831cb0ef41Sopenharmony_ci    () => stream.respondWithFile(fname, {
841cb0ef41Sopenharmony_ci      'content-type': 'text/plain'
851cb0ef41Sopenharmony_ci    }),
861cb0ef41Sopenharmony_ci    {
871cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_INVALID_STREAM',
881cb0ef41Sopenharmony_ci      message: 'The stream has been destroyed'
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci  );
911cb0ef41Sopenharmony_ci}));
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
941cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
951cb0ef41Sopenharmony_ci  const req = client.request();
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  req.on('close', common.mustCall(() => {
981cb0ef41Sopenharmony_ci    client.close();
991cb0ef41Sopenharmony_ci    server.close();
1001cb0ef41Sopenharmony_ci  }));
1011cb0ef41Sopenharmony_ci  req.end();
1021cb0ef41Sopenharmony_ci}));
103