-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathutils.test.ts
More file actions
127 lines (104 loc) · 4.5 KB
/
utils.test.ts
File metadata and controls
127 lines (104 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { describe, expect, it } from 'vitest';
import { createRtcUrl, createValidateUrl } from './utils';
describe('createRtcUrl', () => {
it('should create a basic RTC URL', () => {
const url = 'wss://example.com';
const searchParams = new URLSearchParams();
const result = createRtcUrl(url, searchParams);
expect(result.toString()).toBe('wss://example.com/rtc/v1');
});
it('should create a basic RTC URL with http protocol', () => {
const url = 'http://example.com';
const searchParams = new URLSearchParams();
const result = createRtcUrl(url, searchParams);
expect(result.toString()).toBe('ws://example.com/rtc/v1');
});
it('should handle search parameters', () => {
const url = 'wss://example.com';
const searchParams = new URLSearchParams({
token: 'test-token',
room: 'test-room',
});
const result = createRtcUrl(url, searchParams);
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/rtc/v1');
expect(parsedResult.searchParams.get('token')).toBe('test-token');
expect(parsedResult.searchParams.get('room')).toBe('test-room');
});
it('should handle ws protocol', () => {
const url = 'ws://example.com';
const searchParams = new URLSearchParams();
const result = createRtcUrl(url, searchParams);
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/rtc/v1');
});
it('should handle sub paths', () => {
const url = 'wss://example.com/sub/path';
const searchParams = new URLSearchParams();
const result = createRtcUrl(url, searchParams);
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/sub/path/rtc/v1');
});
it('should handle sub paths with trailing slashes', () => {
const url = 'wss://example.com/sub/path/';
const searchParams = new URLSearchParams();
const result = createRtcUrl(url, searchParams);
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/sub/path/rtc/v1');
});
it('should handle sub paths with url params', () => {
const url = 'wss://example.com/sub/path?param=value';
const searchParams = new URLSearchParams();
searchParams.set('token', 'test-token');
const result = createRtcUrl(url, searchParams);
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/sub/path/rtc/v1');
expect(parsedResult.searchParams.get('param')).toBe('value');
expect(parsedResult.searchParams.get('token')).toBe('test-token');
});
});
describe('createValidateUrl', () => {
it('should create a basic validate URL', () => {
const rtcUrl = createRtcUrl('wss://example.com', new URLSearchParams());
const result = createValidateUrl(rtcUrl.toString());
expect(result.toString()).toBe('https://example.com/rtc/v1/validate');
});
it('should handle search parameters', () => {
const rtcUrl = createRtcUrl(
'wss://example.com',
new URLSearchParams({
token: 'test-token',
room: 'test-room',
}),
);
const result = createValidateUrl(rtcUrl.toString());
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/rtc/v1/validate');
expect(parsedResult.searchParams.get('token')).toBe('test-token');
expect(parsedResult.searchParams.get('room')).toBe('test-room');
});
it('should handle ws protocol', () => {
const rtcUrl = createRtcUrl('ws://example.com', new URLSearchParams());
const result = createValidateUrl(rtcUrl.toString());
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/rtc/v1/validate');
});
it('should preserve the original path', () => {
const rtcUrl = createRtcUrl('wss://example.com/some/path', new URLSearchParams());
const result = createValidateUrl(rtcUrl.toString());
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/some/path/rtc/v1/validate');
});
it('should handle sub paths with trailing slashes', () => {
const rtcUrl = createRtcUrl('wss://example.com/sub/path/', new URLSearchParams());
const result = createValidateUrl(rtcUrl.toString());
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/sub/path/rtc/v1/validate');
});
it('should handle v0 paths', () => {
const rtcUrl = createRtcUrl('wss://example.com/sub/path/', new URLSearchParams(), true);
const result = createValidateUrl(rtcUrl.toString());
const parsedResult = new URL(result);
expect(parsedResult.pathname).toBe('/sub/path/rtc/validate');
});
});