node/test/parallel/test-http2-server-socketerror.js
Anatoli Papirovski 73533a1932
http2: do not allow socket manipulation
Because of the specific serialization and processing requirements
of HTTP/2, sockets should not be directly manipulated. This
forbids any interactions with destroy, emit, end, pause, read,
resume and write methods of the socket. It also redirects
setTimeout to session instead of socket.

PR-URL: https://github.com/nodejs/node/pull/16330
Fixes: https://github.com/nodejs/node/issues/16252
Refs: https://github.com/nodejs/node/pull/16211
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-10-25 12:50:44 -04:00

57 lines
1.7 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const { kSocket } = require('internal/http2/util');
const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
stream.respond();
stream.end('ok');
}));
server.on('session', common.mustCall((session) => {
// First, test that the socketError event is forwarded to the session object
// and not the server object.
const handler = common.mustCall((error, socket) => {
common.expectsError({
type: Error,
message: 'test'
})(error);
assert.strictEqual(socket, session[kSocket]);
});
const isNotCalled = common.mustNotCall();
session.on('socketError', handler);
server.on('socketError', isNotCalled);
session[kSocket].emit('error', new Error('test'));
session.removeListener('socketError', handler);
server.removeListener('socketError', isNotCalled);
// Second, test that the socketError is forwarded to the server object when
// no socketError listener is registered for the session
server.on('socketError', common.mustCall((error, socket, session) => {
common.expectsError({
type: Error,
message: 'test'
})(error);
assert.strictEqual(socket, session[kSocket]);
assert.strictEqual(session, session);
}));
session[kSocket].emit('error', new Error('test'));
}));
server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);
const req = client.request();
req.resume();
req.on('end', common.mustCall());
req.on('streamClosed', common.mustCall(() => {
client.destroy();
server.close();
}));
}));