Skip to content

Commit

Permalink
fix: add null check for connection.headers (#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
wood1986 authored and hiroppy committed Aug 20, 2019
1 parent bcacacf commit 7964997
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/servers/SockJSServer.js
Expand Up @@ -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);
});
}

Expand Down
15 changes: 13 additions & 2 deletions test/server/servers/SockJSServer.test.js
Expand Up @@ -10,7 +10,7 @@ describe('SockJSServer', () => {
let socketServer;
let listeningApp;

beforeAll((done) => {
beforeEach((done) => {
// eslint-disable-next-line new-cap
const app = new express();

Expand Down Expand Up @@ -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);
});
});

0 comments on commit 7964997

Please sign in to comment.