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 10// Check that pushStream handles method HEAD correctly 11// - stream should end immediately (no body) 12 13const server = http2.createServer(); 14server.on('stream', common.mustCall((stream, headers) => { 15 const port = server.address().port; 16 if (headers[':path'] === '/') { 17 stream.pushStream({ 18 ':scheme': 'http', 19 ':method': 'HEAD', 20 ':authority': `localhost:${port}`, 21 }, common.mustCall((err, push, headers) => { 22 assert.strictEqual(push._writableState.ended, true); 23 push.respond(); 24 // Cannot write to push() anymore 25 push.on('error', common.expectsError({ 26 name: 'Error', 27 code: 'ERR_STREAM_WRITE_AFTER_END', 28 message: 'write after end' 29 })); 30 assert(!push.write('test')); 31 stream.end('test'); 32 })); 33 } 34 stream.respond({ 35 'content-type': 'text/html', 36 ':status': 200 37 }); 38})); 39 40server.listen(0, common.mustCall(() => { 41 const port = server.address().port; 42 const client = http2.connect(`http://localhost:${port}`); 43 44 const countdown = new Countdown(2, () => { 45 server.close(); 46 client.close(); 47 }); 48 49 const req = client.request(); 50 req.setEncoding('utf8'); 51 52 client.on('stream', common.mustCall((stream, headers) => { 53 assert.strictEqual(headers[':method'], 'HEAD'); 54 assert.strictEqual(headers[':scheme'], 'http'); 55 assert.strictEqual(headers[':path'], '/'); 56 assert.strictEqual(headers[':authority'], `localhost:${port}`); 57 stream.on('push', common.mustCall(() => { 58 stream.on('data', common.mustNotCall()); 59 stream.on('end', common.mustCall()); 60 })); 61 stream.on('close', common.mustCall(() => countdown.dec())); 62 })); 63 64 let data = ''; 65 66 req.on('data', common.mustCall((d) => data += d)); 67 req.on('end', common.mustCall(() => { 68 assert.strictEqual(data, 'test'); 69 })); 70 req.on('close', common.mustCall(() => countdown.dec())); 71 req.end(); 72})); 73