|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { createRtcUrl, createValidateUrl } from './utils'; |
| 3 | + |
| 4 | +describe('createRtcUrl', () => { |
| 5 | + it('should create a basic RTC URL', () => { |
| 6 | + const url = 'wss://example.com'; |
| 7 | + const searchParams = new URLSearchParams(); |
| 8 | + const result = createRtcUrl(url, searchParams); |
| 9 | + expect(result.toString()).toBe('wss://example.com/rtc'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should handle search parameters', () => { |
| 13 | + const url = 'wss://example.com'; |
| 14 | + const searchParams = new URLSearchParams({ |
| 15 | + token: 'test-token', |
| 16 | + room: 'test-room', |
| 17 | + }); |
| 18 | + const result = createRtcUrl(url, searchParams); |
| 19 | + |
| 20 | + const parsedResult = new URL(result); |
| 21 | + expect(parsedResult.pathname).toBe('/rtc'); |
| 22 | + expect(parsedResult.searchParams.get('token')).toBe('test-token'); |
| 23 | + expect(parsedResult.searchParams.get('room')).toBe('test-room'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should handle ws protocol', () => { |
| 27 | + const url = 'ws://example.com'; |
| 28 | + const searchParams = new URLSearchParams(); |
| 29 | + const result = createRtcUrl(url, searchParams); |
| 30 | + |
| 31 | + const parsedResult = new URL(result); |
| 32 | + expect(parsedResult.pathname).toBe('/rtc'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle sub paths', () => { |
| 36 | + const url = 'wss://example.com/sub/path'; |
| 37 | + const searchParams = new URLSearchParams(); |
| 38 | + const result = createRtcUrl(url, searchParams); |
| 39 | + |
| 40 | + const parsedResult = new URL(result); |
| 41 | + expect(parsedResult.pathname).toBe('/sub/path/rtc'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should handle sub paths with trailing slashes', () => { |
| 45 | + const url = 'wss://example.com/sub/path/'; |
| 46 | + const searchParams = new URLSearchParams(); |
| 47 | + const result = createRtcUrl(url, searchParams); |
| 48 | + |
| 49 | + const parsedResult = new URL(result); |
| 50 | + expect(parsedResult.pathname).toBe('/sub/path/rtc'); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('createValidateUrl', () => { |
| 55 | + it('should create a basic validate URL', () => { |
| 56 | + const rtcUrl = createRtcUrl('wss://example.com', new URLSearchParams()); |
| 57 | + const result = createValidateUrl(rtcUrl); |
| 58 | + expect(result.toString()).toBe('https://example.com/rtc/validate'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle search parameters', () => { |
| 62 | + const rtcUrl = createRtcUrl( |
| 63 | + 'wss://example.com', |
| 64 | + new URLSearchParams({ |
| 65 | + token: 'test-token', |
| 66 | + room: 'test-room', |
| 67 | + }), |
| 68 | + ); |
| 69 | + const result = createValidateUrl(rtcUrl); |
| 70 | + |
| 71 | + const parsedResult = new URL(result); |
| 72 | + expect(parsedResult.pathname).toBe('/rtc/validate'); |
| 73 | + expect(parsedResult.searchParams.get('token')).toBe('test-token'); |
| 74 | + expect(parsedResult.searchParams.get('room')).toBe('test-room'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle ws protocol', () => { |
| 78 | + const rtcUrl = createRtcUrl('ws://example.com', new URLSearchParams()); |
| 79 | + const result = createValidateUrl(rtcUrl); |
| 80 | + |
| 81 | + const parsedResult = new URL(result); |
| 82 | + expect(parsedResult.pathname).toBe('/rtc/validate'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should preserve the original path', () => { |
| 86 | + const rtcUrl = createRtcUrl('wss://example.com/some/path', new URLSearchParams()); |
| 87 | + const result = createValidateUrl(rtcUrl); |
| 88 | + |
| 89 | + const parsedResult = new URL(result); |
| 90 | + expect(parsedResult.pathname).toBe('/some/path/rtc/validate'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should handle sub paths with trailing slashes', () => { |
| 94 | + const rtcUrl = createRtcUrl('wss://example.com/sub/path/', new URLSearchParams()); |
| 95 | + const result = createValidateUrl(rtcUrl); |
| 96 | + |
| 97 | + const parsedResult = new URL(result); |
| 98 | + expect(parsedResult.pathname).toBe('/sub/path/rtc/validate'); |
| 99 | + }); |
| 100 | +}); |
0 commit comments