node/test/parallel/test-http2-create-client-session.js
James M Snell 060babd665 http2: add initial support for originSet
Add new properties to `Http2Session` to identify alpnProtocol,
and indicator about whether the session is TLS or not, and
initial support for origin set (preparinng for `ORIGIN` frame
support and the client-side `Pool` implementation.

The `originSet` is the set of origins for which an `Http2Session`
may be considered authoritative. Per the `ORIGIN` frame spec,
the originSet is only valid on TLS connections, so this is only
exposed when using a `TLSSocket`.

PR-URL: https://github.com/nodejs/node/pull/17935
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Sebastiaan Deckers <sebdeckers83@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
2018-01-03 11:29:43 -08:00

71 lines
1.9 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
const Countdown = require('../common/countdown');
const body =
'<html><head></head><body><h1>this is some data</h2></body></html>';
const server = h2.createServer();
const count = 100;
// we use the lower-level API here
server.on('stream', common.mustCall(onStream, count));
function onStream(stream, headers, flags) {
assert.strictEqual(headers[':scheme'], 'http');
assert.ok(headers[':authority']);
assert.strictEqual(headers[':method'], 'GET');
assert.strictEqual(flags, 5);
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.write(body.slice(0, 20));
stream.end(body.slice(20));
}
server.listen(0);
server.on('listening', common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
client.setMaxListeners(101);
client.on('goaway', console.log);
client.on('connect', common.mustCall(() => {
assert(!client.encrypted);
assert(!client.originSet);
assert.strictEqual(client.alpnProtocol, 'h2c');
}));
const countdown = new Countdown(count, () => {
client.close();
server.close();
});
for (let n = 0; n < count; n++) {
const req = client.request();
req.on('response', common.mustCall(function(headers) {
assert.strictEqual(headers[':status'], 200, 'status code is set');
assert.strictEqual(headers['content-type'], 'text/html',
'content type is set');
assert(headers['date'], 'there is a date');
}));
let data = '';
req.setEncoding('utf8');
req.on('data', (d) => data += d);
req.on('end', common.mustCall(() => {
assert.strictEqual(body, data);
}));
req.on('close', common.mustCall(() => countdown.dec()));
}
}));