|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const h2 = require('http2'); |
| 6 | +const net = require('net'); |
| 7 | + |
| 8 | +function isEven(number) { |
| 9 | + return number % 2 === 0; |
| 10 | +} |
| 11 | + |
| 12 | +function isOdd(number) { |
| 13 | + return number % 2 === 1; |
| 14 | +} |
| 15 | + |
| 16 | +// Push a request & response |
| 17 | +function requestListener(request, response) { |
| 18 | + return new Promise(function(resolve) { |
| 19 | + assert.ok(isOdd(response.stream.id)); |
| 20 | + const headers = { |
| 21 | + ':path': '/pushed', |
| 22 | + ':method': 'GET', |
| 23 | + ':scheme': 'http', |
| 24 | + ':authority': 'localhost' |
| 25 | + }; |
| 26 | + const pushRequest = response.createPushRequest(headers); |
| 27 | + pushRequest.push(common.mustCall(function(error, pushResponse) { |
| 28 | + assert.strictEqual(error, null); |
| 29 | + assert.ok(isEven(pushResponse.stream.id)); |
| 30 | + pushResponse.end('This is a server-initiated response'); |
| 31 | + response.end('This is a client-initiated response'); |
| 32 | + resolve(); |
| 33 | + })); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +function clientRequest(client, socket) { |
| 38 | + const headers = { |
| 39 | + ':path': '/', |
| 40 | + ':method': 'GET', |
| 41 | + ':scheme': 'http', |
| 42 | + ':authority': 'localhost' |
| 43 | + }; |
| 44 | + return new Promise(function(resolve) { |
| 45 | + const requestStream = client.request(headers); |
| 46 | + |
| 47 | + // TODO seb: fix the missing promised stream id for the pushed stream |
| 48 | + // The PUSH_PROMISE frame is received in onHeaders. |
| 49 | + // Because the event payload lacks a Promised Stream ID the headers |
| 50 | + // are triggered as an event on the parent stream, |
| 51 | + // causing this test to fail. |
| 52 | + |
| 53 | + function onStream(pushStream, headers, flags) { |
| 54 | + assert.strictEqual(headers[':status'], '200'); |
| 55 | + assert.strictEqual(headers[':path'], '/pushed'); |
| 56 | + assert.strictEqual(headers[':method'], 'GET'); |
| 57 | + assert.strictEqual(headers[':scheme'], 'http'); |
| 58 | + assert.strictEqual(headers[':authority'], 'localhost'); |
| 59 | + assert.ok(headers['date']); |
| 60 | + assert.strictEqual(flags, h2.constants.NGHTTP2_FLAG_END_HEADERS); |
| 61 | + |
| 62 | + pushStream.on('data', common.mustCall(function(data) { |
| 63 | + assert.strictEqual(data.toString(), |
| 64 | + 'This is a server-initiated response'); |
| 65 | + })); |
| 66 | + |
| 67 | + pushStream.on('streamClosed', resolve); |
| 68 | + } |
| 69 | + requestStream.session.on('stream', common.mustCall(onStream)); |
| 70 | + |
| 71 | + requestStream.on('response', common.mustCall(function(headers, flags) { |
| 72 | + assert.strictEqual(headers[':status'], '200'); |
| 73 | + assert.strictEqual(headers[':path'], '/'); |
| 74 | + assert.strictEqual(headers[':method'], 'GET'); |
| 75 | + assert.strictEqual(headers[':scheme'], 'http'); |
| 76 | + assert.strictEqual(headers[':authority'], 'localhost'); |
| 77 | + assert.ok(headers['date']); |
| 78 | + assert.strictEqual(flags, h2.constants.NGHTTP2_FLAG_END_HEADERS); |
| 79 | + })); |
| 80 | + |
| 81 | + requestStream.on('data', common.mustCall(function(data) { |
| 82 | + assert.strictEqual(data.toString(), |
| 83 | + 'This is a client-initiated response'); |
| 84 | + })); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +let socket; |
| 89 | +const stages = []; |
| 90 | +const server = h2.createServer(); |
| 91 | +server.listen(0, common.mustCall(function() { |
| 92 | + console.log(server.address()); |
| 93 | + server.once('request', common.mustCall(function(request, response) { |
| 94 | + stages.push(requestListener(request, response)); |
| 95 | + Promise.all(stages) |
| 96 | + .then(function() { |
| 97 | + socket.destroy(); |
| 98 | + server.close(); |
| 99 | + }); |
| 100 | + })); |
| 101 | + socket = net.connect(server.address(), common.mustCall(function() { |
| 102 | + const client = h2.createClientSession(socket); |
| 103 | + stages.push(clientRequest(client, socket)); |
| 104 | + })); |
| 105 | +})); |
0 commit comments