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(); 11server.on('stream', common.mustCall((stream, headers, flags) => { 12 if (headers[':path'] === '/') { 13 stream.pushStream({ ':path': '/foobar' }, (err, push, headers) => { 14 assert.ifError(err); 15 push.respond({ 16 'content-type': 'text/html', 17 'x-push-data': 'pushed by server', 18 }); 19 push.write('pushed by server '); 20 setImmediate(() => push.end('data')); 21 stream.end('st'); 22 }); 23 } 24 stream.respond({ 'content-type': 'text/html' }); 25 stream.write('te'); 26})); 27 28 29server.listen(0, common.mustCall(() => { 30 const port = server.address().port; 31 const client = http2.connect(`http://localhost:${port}`); 32 33 const req = client.request(); 34 35 const countdown = new Countdown(2, () => { 36 server.close(); 37 client.close(); 38 }); 39 40 req.on('response', common.mustCall((headers) => { 41 assert.strictEqual(headers[':status'], 200); 42 assert.strictEqual(headers['content-type'], 'text/html'); 43 })); 44 45 client.on('stream', common.mustCall((stream, headers, flags) => { 46 assert.strictEqual(headers[':scheme'], 'http'); 47 assert.strictEqual(headers[':path'], '/foobar'); 48 assert.strictEqual(headers[':authority'], `localhost:${port}`); 49 stream.on('push', common.mustCall((headers, flags) => { 50 assert.strictEqual(headers[':status'], 200); 51 assert.strictEqual(headers['content-type'], 'text/html'); 52 assert.strictEqual(headers['x-push-data'], 'pushed by server'); 53 })); 54 55 stream.setEncoding('utf8'); 56 let pushData = ''; 57 stream.on('data', (d) => pushData += d); 58 stream.on('end', common.mustCall(() => { 59 assert.strictEqual(pushData, 'pushed by server data'); 60 })); 61 stream.on('close', () => countdown.dec()); 62 })); 63 64 let data = ''; 65 66 req.setEncoding('utf8'); 67 req.on('data', common.mustCallAtLeast((d) => data += d)); 68 req.on('end', common.mustCall(() => { 69 assert.strictEqual(data, 'test'); 70 })); 71 req.on('close', () => countdown.dec()); 72})); 73