| 
 | 1 | +'use strict';  | 
 | 2 | + | 
 | 3 | +const common = require('../common');  | 
 | 4 | +if (!common.hasCrypto)  | 
 | 5 | +  common.skip('missing crypto');  | 
 | 6 | +const assert = require('assert');  | 
 | 7 | +const http2 = require('http2');  | 
 | 8 | + | 
 | 9 | +const {  | 
 | 10 | +  HTTP2_HEADER_PATH,  | 
 | 11 | +  HTTP2_HEADER_STATUS,  | 
 | 12 | +  HTTP2_HEADER_METHOD,  | 
 | 13 | +} = http2.constants;  | 
 | 14 | + | 
 | 15 | +const server = http2.createServer();  | 
 | 16 | +server.on('stream', common.mustCall((stream) => {  | 
 | 17 | +  server.close();  | 
 | 18 | +  stream.session.close();  | 
 | 19 | +  stream.on('wantTrailers', common.mustCall(() => {  | 
 | 20 | +    stream.sendTrailers({ xyz: 'abc' });  | 
 | 21 | +  }));  | 
 | 22 | + | 
 | 23 | +  stream.respond({ [HTTP2_HEADER_STATUS]: 200 }, { waitForTrailers: true });  | 
 | 24 | +  stream.write('some data');  | 
 | 25 | +  stream.end();  | 
 | 26 | +}));  | 
 | 27 | + | 
 | 28 | +server.listen(0, common.mustCall(() => {  | 
 | 29 | +  const port = server.address().port;  | 
 | 30 | +  const client = http2.connect(`http://localhost:${port}`);  | 
 | 31 | +  client.socket.on('close', common.mustCall());  | 
 | 32 | +  const req = client.request({  | 
 | 33 | +    [HTTP2_HEADER_PATH]: '/',  | 
 | 34 | +    [HTTP2_HEADER_METHOD]: 'POST'  | 
 | 35 | +  });  | 
 | 36 | +  req.end();  | 
 | 37 | +  req.on('response', common.mustCall());  | 
 | 38 | +  let data = '';  | 
 | 39 | +  req.on('data', (chunk) => {  | 
 | 40 | +    data += chunk;  | 
 | 41 | +  });  | 
 | 42 | +  req.on('end', common.mustCall(() => {  | 
 | 43 | +    assert.strictEqual(data, 'some data');  | 
 | 44 | +  }));  | 
 | 45 | +  req.on('trailers', common.mustCall((headers) => {  | 
 | 46 | +    assert.strictEqual(headers.xyz, 'abc');  | 
 | 47 | +  }));  | 
 | 48 | +  req.on('close', common.mustCall());  | 
 | 49 | +}));  | 
0 commit comments