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 Countdown = require('../common/countdown'); 9 10const server = http2.createServer(); 11 12server.on('stream', common.mustCall((stream) => { 13 stream.respond(); 14 stream.end(); 15})); 16 17server.listen(0, common.mustCall(() => { 18 const client = http2.connect(`http://localhost:${server.address().port}`); 19 20 const countdown = new Countdown(2, () => { 21 server.close(); 22 client.close(); 23 }); 24 25 // Request 1 will fail because there are two content-length header values 26 assert.throws( 27 () => { 28 client.request({ 29 ':method': 'POST', 30 'content-length': 1, 31 'Content-Length': 2 32 }); 33 }, { 34 code: 'ERR_HTTP2_HEADER_SINGLE_VALUE', 35 name: 'TypeError', 36 message: 'Header field "content-length" must only have a single value' 37 } 38 ); 39 40 { 41 // Request 2 will succeed 42 const req = client.request({ 43 ':method': 'POST', 44 'content-length': 1 45 }); 46 req.resume(); 47 req.on('end', common.mustCall()); 48 req.on('close', common.mustCall(() => countdown.dec())); 49 req.end('a'); 50 } 51 52 { 53 // Request 3 will fail because nghttp2 does not allow the content-length 54 // header to be set for non-payload bearing requests... 55 const req = client.request({ 'content-length': 1 }); 56 req.resume(); 57 req.on('end', common.mustCall()); 58 req.on('close', common.mustCall(() => countdown.dec())); 59 req.on('error', common.expectsError({ 60 code: 'ERR_HTTP2_STREAM_ERROR', 61 name: 'Error', 62 message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR' 63 })); 64 } 65})); 66