-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from webitel/feature/ws-validator
23.12.31: ws validator [WTEL-3986]
- Loading branch information
Showing
5 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Validator for WebSocket URL string | ||
```javascript | ||
import websocketValidator from '@webitel/ui-sdk/src/validators/websocketValidator/websocketValidator'; | ||
|
||
websocketValidator('wss://example.com') // true | ||
websocketValidator('krokoziabra') // false | ||
``` |
28 changes: 28 additions & 0 deletions
28
src/validators/websocketValidator/__tests__/websocketValidator.spec.js
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,28 @@ | ||
import websocketValidator from '../websocketValidator'; | ||
|
||
describe('websocketValidator', () => { | ||
it('truthy case 1: localhost ws', () => { | ||
expect(websocketValidator('ws://localhost:8080')).toBe(true); | ||
}); | ||
it('truthy case 2: localhost wss', () => { | ||
expect(websocketValidator('wss://localhost:8080')).toBe(true); | ||
}); | ||
it('truthy case 3: ip ws', () => { | ||
expect(websocketValidator('ws://10.10.10.10:8080')).toBe(true); | ||
}); | ||
it('truthy case 4: dns name', () => { | ||
expect(websocketValidator('wss://example.com')).toBe(true); | ||
}); | ||
it('truthy case 5: dns name', () => { | ||
expect(websocketValidator('wss://example.com/ws')).toBe(true); | ||
}); | ||
it('truthy case 6: dns name', () => { | ||
expect(websocketValidator('wss://socket.socket.com/v3/channel_123?api_key=123')).toBe(true); | ||
}); | ||
it('falsy case 1: empty string', () => { | ||
expect(websocketValidator('')).toBe(false); | ||
}); | ||
it('falsy case 2: ws+ws', () => { | ||
expect(websocketValidator('wss://example.com/wswss://example.com/ws')).toBe(false); | ||
}); | ||
}); |
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,2 @@ | ||
// That's strange but i haven't found any npm package with websocket validator | ||
export default (value) => (/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[^\/]+)(\:[0-9]{1,5})?(.*)$/i).test(value); |
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