-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
98 additions
and
127 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
indexer/services/socks/__tests__/lib/invalid-message.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { InvalidMessageHandler } from '../../src/lib/invalid-message'; | ||
import { RateLimiter } from '../../src/lib/rate-limit'; | ||
import { sendMessage } from '../../src/helpers/wss'; | ||
import WebSocket from 'ws'; | ||
import { WS_CLOSE_CODE_POLICY_VIOLATION } from '../../src/lib/constants'; | ||
import { Connection, WebsocketEvents } from '../../src/types'; | ||
|
||
jest.mock('../../src/lib/rate-limit'); | ||
jest.mock('../../src/helpers/wss', () => ({ | ||
sendMessage: jest.fn(), | ||
})); | ||
|
||
describe('InvalidMessageHandler', () => { | ||
let invalidMessageHandler: InvalidMessageHandler; | ||
let mockConnection: Connection; | ||
const connectionId = 'testConnectionId'; | ||
const responseMessage = 'Test response message'; | ||
|
||
beforeEach(() => { | ||
(RateLimiter as jest.Mock).mockImplementation(() => ({ | ||
rateLimit: jest.fn().mockReturnValue(0), | ||
removeConnection: jest.fn(), | ||
})); | ||
|
||
mockConnection = { | ||
ws: { | ||
close: jest.fn(), | ||
removeAllListeners: jest.fn(), | ||
} as unknown as WebSocket, | ||
messageId: 1, | ||
}; | ||
}); | ||
|
||
test('should send normal response message if not rate-limited', () => { | ||
invalidMessageHandler = new InvalidMessageHandler(); | ||
invalidMessageHandler.handleInvalidMessage(responseMessage, mockConnection, connectionId); | ||
|
||
expect(sendMessage).toHaveBeenCalled(); | ||
expect(mockConnection.ws.close).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should rate limit, close connection, remove all event listeners for messages if over limit', () => { | ||
(RateLimiter as jest.Mock).mockImplementation(() => ({ | ||
rateLimit: jest.fn().mockReturnValue(1000), | ||
removeConnection: jest.fn(), | ||
})); | ||
invalidMessageHandler = new InvalidMessageHandler(); | ||
invalidMessageHandler.handleInvalidMessage(responseMessage, mockConnection, connectionId); | ||
|
||
expect(sendMessage).toHaveBeenCalled(); | ||
expect(mockConnection.ws.close).toHaveBeenCalledWith( | ||
WS_CLOSE_CODE_POLICY_VIOLATION, | ||
JSON.stringify({ message: 'Rate limited' }), | ||
); | ||
expect(mockConnection.ws.removeAllListeners).toHaveBeenCalledWith(WebsocketEvents.MESSAGE); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,4 +149,5 @@ export enum WebsocketEvents { | |
LISTENING = 'listening', | ||
MESSAGE = 'message', | ||
PONG = 'pong', | ||
PING = 'ping', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters