11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst http2 = require('http2');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst server = http2.createServer();
101cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
111cb0ef41Sopenharmony_ci  const port = server.address().port;
121cb0ef41Sopenharmony_ci  server.once('request', common.mustCall((request, response) => {
131cb0ef41Sopenharmony_ci    response.addTrailers({
141cb0ef41Sopenharmony_ci      ABC: 123
151cb0ef41Sopenharmony_ci    });
161cb0ef41Sopenharmony_ci    response.setTrailer('ABCD', 123);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci    assert.throws(
191cb0ef41Sopenharmony_ci      () => response.addTrailers({ '': 'test' }),
201cb0ef41Sopenharmony_ci      {
211cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_HTTP_TOKEN',
221cb0ef41Sopenharmony_ci        name: 'TypeError',
231cb0ef41Sopenharmony_ci        message: 'Header name must be a valid HTTP token [""]'
241cb0ef41Sopenharmony_ci      }
251cb0ef41Sopenharmony_ci    );
261cb0ef41Sopenharmony_ci    assert.throws(
271cb0ef41Sopenharmony_ci      () => response.setTrailer('test', undefined),
281cb0ef41Sopenharmony_ci      {
291cb0ef41Sopenharmony_ci        code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
301cb0ef41Sopenharmony_ci        name: 'TypeError',
311cb0ef41Sopenharmony_ci        message: 'Invalid value "undefined" for header "test"'
321cb0ef41Sopenharmony_ci      }
331cb0ef41Sopenharmony_ci    );
341cb0ef41Sopenharmony_ci    assert.throws(
351cb0ef41Sopenharmony_ci      () => response.setTrailer('test', null),
361cb0ef41Sopenharmony_ci      {
371cb0ef41Sopenharmony_ci        code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
381cb0ef41Sopenharmony_ci        name: 'TypeError',
391cb0ef41Sopenharmony_ci        message: 'Invalid value "null" for header "test"'
401cb0ef41Sopenharmony_ci      }
411cb0ef41Sopenharmony_ci    );
421cb0ef41Sopenharmony_ci    assert.throws(
431cb0ef41Sopenharmony_ci      () => response.setTrailer(), // Trailer name undefined
441cb0ef41Sopenharmony_ci      {
451cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_ARG_TYPE',
461cb0ef41Sopenharmony_ci        name: 'TypeError',
471cb0ef41Sopenharmony_ci        message: 'The "name" argument must be of type string. Received ' +
481cb0ef41Sopenharmony_ci                 'undefined'
491cb0ef41Sopenharmony_ci      }
501cb0ef41Sopenharmony_ci    );
511cb0ef41Sopenharmony_ci    assert.throws(
521cb0ef41Sopenharmony_ci      () => response.setTrailer(''),
531cb0ef41Sopenharmony_ci      {
541cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_HTTP_TOKEN',
551cb0ef41Sopenharmony_ci        name: 'TypeError',
561cb0ef41Sopenharmony_ci        message: 'Header name must be a valid HTTP token [""]'
571cb0ef41Sopenharmony_ci      }
581cb0ef41Sopenharmony_ci    );
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    response.end('hello');
611cb0ef41Sopenharmony_ci  }));
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  const url = `http://localhost:${port}`;
641cb0ef41Sopenharmony_ci  const client = http2.connect(url, common.mustCall(() => {
651cb0ef41Sopenharmony_ci    const request = client.request();
661cb0ef41Sopenharmony_ci    request.on('trailers', common.mustCall((headers) => {
671cb0ef41Sopenharmony_ci      assert.strictEqual(headers.abc, '123');
681cb0ef41Sopenharmony_ci      assert.strictEqual(headers.abcd, '123');
691cb0ef41Sopenharmony_ci    }));
701cb0ef41Sopenharmony_ci    request.resume();
711cb0ef41Sopenharmony_ci    request.on('end', common.mustCall(() => {
721cb0ef41Sopenharmony_ci      client.close();
731cb0ef41Sopenharmony_ci      server.close();
741cb0ef41Sopenharmony_ci    }));
751cb0ef41Sopenharmony_ci  }));
761cb0ef41Sopenharmony_ci}));
77