1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6const assert = require('assert'); 7const http2 = require('http2'); 8 9const checkWeight = (actual, expect) => { 10 const server = http2.createServer(); 11 server.on('stream', common.mustCall((stream, headers, flags) => { 12 assert.strictEqual(stream.state.weight, expect); 13 stream.respond(); 14 stream.end('test'); 15 })); 16 17 server.listen(0, common.mustCall(() => { 18 const client = http2.connect(`http://localhost:${server.address().port}`); 19 const req = client.request({}, { weight: actual }); 20 21 req.on('data', common.mustCall()); 22 req.on('end', common.mustCall()); 23 req.on('close', common.mustCall(() => { 24 server.close(); 25 client.close(); 26 })); 27 })); 28}; 29 30// When client weight is lower than 1, weight is 1 31checkWeight(-1, 1); 32checkWeight(0, 1); 33 34// 1 - 256 is correct weight 35checkWeight(1, 1); 36checkWeight(16, 16); 37checkWeight(256, 256); 38 39// When client weight is higher than 256, weight is 256 40checkWeight(257, 256); 41checkWeight(512, 256); 42 43// When client weight is undefined, weight is default 16 44checkWeight(undefined, 16); 45