diff --git a/spec/ParseWebSocketServer.spec.js b/spec/ParseWebSocketServer.spec.js index 4eb73695d1..2c0a8437b4 100644 --- a/spec/ParseWebSocketServer.spec.js +++ b/spec/ParseWebSocketServer.spec.js @@ -76,6 +76,37 @@ describe('ParseWebSocketServer', function () { expect(wssError).toBe('Invalid Packet'); }); + it('can handle ping/pong', async () => { + const onConnectCallback = jasmine.createSpy('onConnectCallback'); + const http = require('http'); + const server = http.createServer(); + const parseWebSocketServer = new ParseWebSocketServer(server, onConnectCallback, { + websocketTimeout: 10, + }).server; + + const ws = new EventEmitter(); + ws.readyState = 0; + ws.OPEN = 0; + ws.ping = jasmine.createSpy('ping'); + ws.terminate = jasmine.createSpy('terminate'); + + parseWebSocketServer.onConnection(ws); + + // Make sure callback is called + expect(onConnectCallback).toHaveBeenCalled(); + // Check that `waitingForPong` toggles + expect(ws.waitingForPong).toBe(false); + await new Promise(resolve => setTimeout(resolve, 10)); + expect(ws.ping).toHaveBeenCalled(); + expect(ws.waitingForPong).toBe(true); + ws.emit('pong'); + expect(ws.waitingForPong).toBe(false); + await new Promise(resolve => setTimeout(resolve, 10)); + expect(ws.waitingForPong).toBe(true); + expect(ws.terminate).not.toHaveBeenCalled(); + server.close(); + }); + it('closes interrupted connection', async () => { const onConnectCallback = jasmine.createSpy('onConnectCallback'); const http = require('http'); @@ -93,8 +124,10 @@ describe('ParseWebSocketServer', function () { // Make sure callback is called expect(onConnectCallback).toHaveBeenCalled(); + expect(ws.waitingForPong).toBe(false); await new Promise(resolve => setTimeout(resolve, 10)); expect(ws.ping).toHaveBeenCalled(); + expect(ws.waitingForPong).toBe(true); await new Promise(resolve => setTimeout(resolve, 10)); expect(ws.terminate).toHaveBeenCalled(); server.close();