From 79649971c299f648fdd0ba3d4513a8cf7fc881d2 Mon Sep 17 00:00:00 2001 From: WooD Fung <5212215+wood1986@users.noreply.github.com> Date: Tue, 20 Aug 2019 06:59:09 -0700 Subject: [PATCH] fix: add null check for connection.headers (#2200) --- lib/servers/SockJSServer.js | 2 +- test/server/servers/SockJSServer.test.js | 15 +++++++++++++-- 2 files changed, 14 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..48c535e98e 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,20 @@ describe('SockJSServer', () => { done(); }, 3000); }); + + it('should not throw an exception when connection is null', () => { + const cb = jest.fn(); + + socketServer.onConnection(cb); + + expect(() => { + socketServer.socket.emit('connection', null); + }).not.toThrow(); + expect(cb.mock.calls[0]).toEqual([null, null]); + }); }); - afterAll((done) => { + afterEach((done) => { listeningApp.close(done); }); });