1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6const assert = require('assert'); 7const http2 = require('http2'); 8const { PADDING_STRATEGY_ALIGNED, PADDING_STRATEGY_CALLBACK } = http2.constants; 9const makeDuplexPair = require('../common/duplexpair'); 10 11{ 12 const testData = '<h1>Hello World.</h1>'; 13 const server = http2.createServer({ 14 paddingStrategy: PADDING_STRATEGY_ALIGNED 15 }); 16 server.on('stream', common.mustCall((stream, headers) => { 17 stream.respond({ 18 'content-type': 'text/html', 19 ':status': 200 20 }); 21 stream.end(testData); 22 })); 23 24 const { clientSide, serverSide } = makeDuplexPair(); 25 26 // The lengths of the expected writes... note that this is highly 27 // sensitive to how the internals are implemented. 28 const serverLengths = [24, 9, 9, 32]; 29 const clientLengths = [9, 9, 48, 9, 1, 21, 1]; 30 31 // Adjust for the 24-byte preamble and two 9-byte settings frames, and 32 // the result must be equally divisible by 8 33 assert.strictEqual( 34 (serverLengths.reduce((i, n) => i + n) - 24 - 9 - 9) % 8, 0); 35 36 // Adjust for two 9-byte settings frames, and the result must be equally 37 // divisible by 8 38 assert.strictEqual( 39 (clientLengths.reduce((i, n) => i + n) - 9 - 9) % 8, 0); 40 41 serverSide.on('data', common.mustCall((chunk) => { 42 assert.strictEqual(chunk.length, serverLengths.shift()); 43 }, serverLengths.length)); 44 clientSide.on('data', common.mustCall((chunk) => { 45 assert.strictEqual(chunk.length, clientLengths.shift()); 46 }, clientLengths.length)); 47 48 server.emit('connection', serverSide); 49 50 const client = http2.connect('http://localhost:80', { 51 paddingStrategy: PADDING_STRATEGY_ALIGNED, 52 createConnection: common.mustCall(() => clientSide) 53 }); 54 55 const req = client.request({ ':path': '/a' }); 56 57 req.on('response', common.mustCall()); 58 59 req.setEncoding('utf8'); 60 req.on('data', common.mustCall((data) => { 61 assert.strictEqual(data, testData); 62 })); 63 req.on('close', common.mustCall(() => { 64 clientSide.destroy(); 65 clientSide.end(); 66 })); 67 req.end(); 68} 69 70// PADDING_STRATEGY_CALLBACK has been aliased to mean aligned padding. 71assert.strictEqual(PADDING_STRATEGY_ALIGNED, PADDING_STRATEGY_CALLBACK); 72