feat: Use WebSocket2023Channel identifier for WebSocket URL

This commit is contained in:
Joachim Van Herwegen
2023-04-25 14:58:04 +02:00
parent 26f24aa76c
commit 69af7c4e16
7 changed files with 44 additions and 90 deletions

View File

@@ -2,26 +2,32 @@ import type { IncomingMessage } from 'http';
import {
generateWebSocketUrl, parseWebSocketRequest,
} from '../../../../../src/server/notifications/WebSocketChannel2023/WebSocket2023Util';
import { BadRequestHttpError } from '../../../../../src/util/errors/BadRequestHttpError';
describe('WebSocket2023Util', (): void => {
describe('#generateWebSocketUrl', (): void => {
it('generates a WebSocket link with a query parameter.', async(): Promise<void> => {
expect(generateWebSocketUrl('http://example.com/', '123456')).toBe('ws://example.com/?auth=123456');
it('generates a WebSocket link.', async(): Promise<void> => {
expect(generateWebSocketUrl('http://example.com/123456')).toBe('ws://example.com/123456');
expect(generateWebSocketUrl('https://example.com/foo/bar', '123456'))
.toBe('wss://example.com/foo/bar?auth=123456');
expect(generateWebSocketUrl('https://example.com/foo/bar/123456'))
.toBe('wss://example.com/foo/bar/123456');
});
});
describe('#parseWebSocketRequest', (): void => {
it('parses the request.', async(): Promise<void> => {
const request: IncomingMessage = { url: '/foo/bar?auth=123%24456' } as any;
expect(parseWebSocketRequest(request)).toEqual({ path: '/foo/bar', id: '123$456' });
const request: IncomingMessage = { url: '/foo/bar/123%24456' } as any;
expect(parseWebSocketRequest('http://example.com/', request)).toBe('http://example.com/foo/bar/123%24456');
});
it('returns an empty path and no id if the url parameter is undefined.', async(): Promise<void> => {
it('throws an error if the url parameter is not defined.', async(): Promise<void> => {
const request: IncomingMessage = {} as any;
expect(parseWebSocketRequest(request)).toEqual({ path: '/' });
expect((): string => parseWebSocketRequest('http://example.com/', request)).toThrow(BadRequestHttpError);
});
it('can handle non-root base URLs.', async(): Promise<void> => {
const request: IncomingMessage = { url: '/foo/bar/123%24456' } as any;
expect(parseWebSocketRequest('http://example.com/foo/bar/', request)).toBe('http://example.com/foo/bar/123%24456');
});
});
});