From f987bac7789e1094fe30ff750c60425493d7546a Mon Sep 17 00:00:00 2001 From: wood1986 <5212215+wood1986@users.noreply.github.com> Date: Fri, 16 Aug 2019 22:49:06 -0700 Subject: [PATCH] fix: add null check for connection.headers --- lib/servers/SockJSServer.js | 2 +- test/server/servers/SockJSServer.test.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/servers/SockJSServer.js b/lib/servers/SockJSServer.js index 782b325a48..c12913a5a7 100644 --- a/lib/servers/SockJSServer.js +++ b/lib/servers/SockJSServer.js @@ -64,7 +64,7 @@ module.exports = class SockJSServer extends BaseServer { // f should be passed the resulting connection and the connection headers onConnection(f) { this.socket.on('connection', (connection) => { - f(connection, connection.headers); + f(connection, connection ? connection.headers : null); }); } diff --git a/test/server/servers/SockJSServer.test.js b/test/server/servers/SockJSServer.test.js index 9c61c67e14..46ec1afa5a 100644 --- a/test/server/servers/SockJSServer.test.js +++ b/test/server/servers/SockJSServer.test.js @@ -10,7 +10,7 @@ describe('SockJSServer', () => { let socketServer; let listeningApp; - beforeAll((done) => { + beforeEach((done) => { // eslint-disable-next-line new-cap const app = new express(); @@ -84,9 +84,17 @@ describe('SockJSServer', () => { done(); }, 3000); }); + + it('should not throw an exception when connection is null', (done) => { + expect(() => { + socketServer.onConnection(() => {}); + socketServer.socket.emit('connection', null); + setImmediate(done); + }).not.toThrow(); + }); }); - afterAll((done) => { + afterEach((done) => { listeningApp.close(done); }); });