Skip to content

Commit 7964997

Browse files
wood1986hiroppy
authored andcommitted
fix: add null check for connection.headers (#2200)
1 parent bcacacf commit 7964997

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/servers/SockJSServer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module.exports = class SockJSServer extends BaseServer {
6464
// f should be passed the resulting connection and the connection headers
6565
onConnection(f) {
6666
this.socket.on('connection', (connection) => {
67-
f(connection, connection.headers);
67+
f(connection, connection ? connection.headers : null);
6868
});
6969
}
7070

test/server/servers/SockJSServer.test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('SockJSServer', () => {
1010
let socketServer;
1111
let listeningApp;
1212

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

@@ -84,9 +84,20 @@ describe('SockJSServer', () => {
8484
done();
8585
}, 3000);
8686
});
87+
88+
it('should not throw an exception when connection is null', () => {
89+
const cb = jest.fn();
90+
91+
socketServer.onConnection(cb);
92+
93+
expect(() => {
94+
socketServer.socket.emit('connection', null);
95+
}).not.toThrow();
96+
expect(cb.mock.calls[0]).toEqual([null, null]);
97+
});
8798
});
8899

89-
afterAll((done) => {
100+
afterEach((done) => {
90101
listeningApp.close(done);
91102
});
92103
});

0 commit comments

Comments
 (0)