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 h2 = require('http2'); 81cb0ef41Sopenharmony_ciconst body = 91cb0ef41Sopenharmony_ci '<html><head></head><body><h1>this is some data</h2></body></html>'; 101cb0ef41Sopenharmony_ciconst trailerKey = 'test-trailer'; 111cb0ef41Sopenharmony_ciconst trailerValue = 'testing'; 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst server = h2.createServer(); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci// We use the lower-level API here 161cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall(onStream)); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifunction onStream(stream, headers, flags) { 191cb0ef41Sopenharmony_ci stream.on('trailers', common.mustCall((headers) => { 201cb0ef41Sopenharmony_ci assert.strictEqual(headers[trailerKey], trailerValue); 211cb0ef41Sopenharmony_ci stream.end(body); 221cb0ef41Sopenharmony_ci })); 231cb0ef41Sopenharmony_ci stream.respond({ 241cb0ef41Sopenharmony_ci 'content-type': 'text/html', 251cb0ef41Sopenharmony_ci ':status': 200 261cb0ef41Sopenharmony_ci }, { waitForTrailers: true }); 271cb0ef41Sopenharmony_ci stream.on('wantTrailers', () => { 281cb0ef41Sopenharmony_ci stream.sendTrailers({ [trailerKey]: trailerValue }); 291cb0ef41Sopenharmony_ci assert.throws( 301cb0ef41Sopenharmony_ci () => stream.sendTrailers({}), 311cb0ef41Sopenharmony_ci { 321cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_TRAILERS_ALREADY_SENT', 331cb0ef41Sopenharmony_ci name: 'Error' 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci ); 361cb0ef41Sopenharmony_ci }); 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci assert.throws( 391cb0ef41Sopenharmony_ci () => stream.sendTrailers({}), 401cb0ef41Sopenharmony_ci { 411cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_TRAILERS_NOT_READY', 421cb0ef41Sopenharmony_ci name: 'Error' 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci ); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciserver.listen(0); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciserver.on('listening', common.mustCall(function() { 501cb0ef41Sopenharmony_ci const client = h2.connect(`http://localhost:${this.address().port}`); 511cb0ef41Sopenharmony_ci const req = client.request({ ':path': '/', ':method': 'POST' }, 521cb0ef41Sopenharmony_ci { waitForTrailers: true }); 531cb0ef41Sopenharmony_ci req.on('wantTrailers', () => { 541cb0ef41Sopenharmony_ci req.sendTrailers({ [trailerKey]: trailerValue }); 551cb0ef41Sopenharmony_ci }); 561cb0ef41Sopenharmony_ci req.on('data', common.mustCall()); 571cb0ef41Sopenharmony_ci req.on('trailers', common.mustCall((headers) => { 581cb0ef41Sopenharmony_ci assert.strictEqual(headers[trailerKey], trailerValue); 591cb0ef41Sopenharmony_ci })); 601cb0ef41Sopenharmony_ci req.on('close', common.mustCall(() => { 611cb0ef41Sopenharmony_ci assert.throws( 621cb0ef41Sopenharmony_ci () => req.sendTrailers({}), 631cb0ef41Sopenharmony_ci { 641cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_INVALID_STREAM', 651cb0ef41Sopenharmony_ci name: 'Error' 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci ); 681cb0ef41Sopenharmony_ci server.close(); 691cb0ef41Sopenharmony_ci client.close(); 701cb0ef41Sopenharmony_ci })); 711cb0ef41Sopenharmony_ci req.end('data'); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci})); 74