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 { inspect } = require('util'); 9 10const server = h2.createServer(); 11server.on('stream', (stream) => { 12 stream.on('close', common.mustCall()); 13 stream.respond(); 14 stream.end('ok'); 15}); 16 17server.listen(0, common.mustCall(() => { 18 const client = h2.connect(`http://localhost:${server.address().port}`); 19 const req = client.request(); 20 const closeCode = 1; 21 22 assert.throws( 23 () => req.close(2 ** 32), 24 { 25 name: 'RangeError', 26 code: 'ERR_OUT_OF_RANGE', 27 message: 'The value of "code" is out of range. It must be ' + 28 '>= 0 && <= 4294967295. Received 4294967296' 29 } 30 ); 31 assert.strictEqual(req.closed, false); 32 33 [true, 1, {}, [], null, 'test'].forEach((notFunction) => { 34 assert.throws( 35 () => req.close(closeCode, notFunction), 36 { 37 name: 'TypeError', 38 code: 'ERR_INVALID_CALLBACK', 39 message: `Callback must be a function. Received ${inspect(notFunction)}` 40 } 41 ); 42 assert.strictEqual(req.closed, false); 43 }); 44 45 req.close(closeCode, common.mustCall()); 46 assert.strictEqual(req.closed, true); 47 48 // Make sure that destroy is called. 49 req._destroy = common.mustCall(req._destroy.bind(req)); 50 51 // Second call doesn't do anything. 52 req.close(closeCode + 1); 53 54 req.on('close', common.mustCall(() => { 55 assert.strictEqual(req.destroyed, true); 56 assert.strictEqual(req.rstCode, closeCode); 57 server.close(); 58 client.close(); 59 })); 60 61 req.on('error', common.expectsError({ 62 code: 'ERR_HTTP2_STREAM_ERROR', 63 name: 'Error', 64 message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR' 65 })); 66 67 // The `response` event should not fire as the server should receive the 68 // RST_STREAM frame before it ever has a chance to reply. 69 req.on('response', common.mustNotCall()); 70 71 // The `end` event should still fire as we close the readable stream by 72 // pushing a `null` chunk. 73 req.on('end', common.mustCall()); 74 75 req.resume(); 76 req.end(); 77})); 78