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 errCheck = common.expectsError({ 10 name: 'Error', 11 code: 'ERR_STREAM_WRITE_AFTER_END', 12 message: 'write after end' 13}, 2); 14 15const { 16 HTTP2_HEADER_PATH, 17 HTTP2_HEADER_METHOD, 18 HTTP2_HEADER_STATUS, 19 HTTP2_METHOD_HEAD, 20} = http2.constants; 21 22const server = http2.createServer(); 23server.on('stream', (stream, headers) => { 24 25 assert.strictEqual(headers[HTTP2_HEADER_METHOD], HTTP2_METHOD_HEAD); 26 27 stream.respond({ [HTTP2_HEADER_STATUS]: 200 }); 28 29 // Because this is a head request, the outbound stream is closed automatically 30 stream.on('error', errCheck); 31 stream.write('data'); 32}); 33 34 35server.listen(0, () => { 36 37 const client = http2.connect(`http://localhost:${server.address().port}`); 38 39 const req = client.request({ 40 [HTTP2_HEADER_METHOD]: HTTP2_METHOD_HEAD, 41 [HTTP2_HEADER_PATH]: '/' 42 }); 43 44 // Because it is a HEAD request, the payload is meaningless. The 45 // option.endStream flag is set automatically making the stream 46 // non-writable. 47 req.on('error', errCheck); 48 req.write('data'); 49 50 req.on('response', common.mustCall((headers, flags) => { 51 assert.strictEqual(headers[HTTP2_HEADER_STATUS], 200); 52 assert.strictEqual(flags, 5); // The end of stream flag is set 53 })); 54 req.on('data', common.mustNotCall()); 55 req.on('end', common.mustCall(() => { 56 server.close(); 57 client.close(); 58 })); 59}); 60