|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const http2 = require('http2'); |
| 6 | + |
| 7 | +const server = http2.createServer(); |
| 8 | +server.on('stream', common.mustCall((stream, headers, flags) => { |
| 9 | + const port = server.address().port; |
| 10 | + if (headers[':path'] === '/') { |
| 11 | + stream.pushStream({ |
| 12 | + ':scheme': 'http', |
| 13 | + ':path': '/foobar', |
| 14 | + ':authority': `localhost:${port}`, |
| 15 | + }, (stream, headers) => { |
| 16 | + stream.respond({ |
| 17 | + 'content-type': 'text/html', |
| 18 | + ':status': 200, |
| 19 | + 'x-push-data': 'pushed by server', |
| 20 | + }); |
| 21 | + stream.end('pushed by server data'); |
| 22 | + }); |
| 23 | + } |
| 24 | + stream.respond({ |
| 25 | + 'content-type': 'text/html', |
| 26 | + ':status': 200 |
| 27 | + }); |
| 28 | + stream.end('test'); |
| 29 | +})); |
| 30 | + |
| 31 | +server.listen(0, common.mustCall(() => { |
| 32 | + const port = server.address().port; |
| 33 | + const headers = { ':path': '/' }; |
| 34 | + const client = http2.connect(`http://localhost:${port}`); |
| 35 | + const req = client.request(headers); |
| 36 | + |
| 37 | + client.on('push', common.mustCall((stream, headers, flag) => { |
| 38 | + assert.strictEqual(headers[':status'], '200'); |
| 39 | + assert.strictEqual(headers['content-type'], 'text/html'); |
| 40 | + assert.strictEqual(headers['x-push-data'], 'pushed by server'); |
| 41 | + })); |
| 42 | + |
| 43 | + let data = ''; |
| 44 | + |
| 45 | + req.on('data', common.mustCall((d) => data += d)); |
| 46 | + req.on('end', common.mustCall(() => { |
| 47 | + assert.strictEqual(data, 'test'); |
| 48 | + server.close(); |
| 49 | + client.destroy(); |
| 50 | + })); |
| 51 | + req.end(); |
| 52 | +})); |
0 commit comments