1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6const assert = require('assert'); 7const h2 = require('http2'); 8const { PADDING_STRATEGY_CALLBACK } = h2.constants; 9 10function selectPadding(frameLen, max) { 11 assert.strictEqual(typeof frameLen, 'number'); 12 assert.strictEqual(typeof max, 'number'); 13 assert(max >= frameLen); 14 return max; 15} 16 17// selectPadding will be called three times: 18// 1. For the client request headers frame 19// 2. For the server response headers frame 20// 3. For the server response data frame 21const options = { 22 paddingStrategy: PADDING_STRATEGY_CALLBACK, 23 selectPadding: common.mustCall(selectPadding, 3) 24}; 25 26const server = h2.createServer(options); 27server.on('stream', common.mustCall(onStream)); 28 29function onStream(stream, headers, flags) { 30 stream.respond({ 31 'content-type': 'text/html', 32 ':status': 200 33 }); 34 stream.end('hello world'); 35} 36 37server.listen(0); 38 39server.on('listening', common.mustCall(() => { 40 const client = h2.connect(`http://localhost:${server.address().port}`, 41 options); 42 43 const req = client.request({ ':path': '/' }); 44 req.on('response', common.mustCall()); 45 req.resume(); 46 req.on('end', common.mustCall(() => { 47 server.close(); 48 client.close(); 49 })); 50 req.end(); 51})); 52