|
| 1 | +import { HARNESS_BRIDGE_PATH } from '@react-native-harness/bridge'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { getWSServer } from './getWSServer.js'; |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => ({ |
| 6 | + getDevServerUrl: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock('../utils/dev-server.js', () => ({ |
| 10 | + getDevServerUrl: mocks.getDevServerUrl, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock('react-native-url-polyfill', () => ({ |
| 14 | + URL, |
| 15 | +})); |
| 16 | + |
| 17 | +describe('getWSServer', () => { |
| 18 | + beforeEach(() => { |
| 19 | + mocks.getDevServerUrl.mockReset(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('builds a websocket bridge URL from an http dev server URL', () => { |
| 23 | + mocks.getDevServerUrl.mockReturnValue( |
| 24 | + 'http://localhost:8081/index.bundle?platform=ios&dev=true#main', |
| 25 | + ); |
| 26 | + |
| 27 | + expect(getWSServer()).toBe(`ws://localhost:8081${HARNESS_BRIDGE_PATH}`); |
| 28 | + }); |
| 29 | + |
| 30 | + it('builds a secure websocket bridge URL from an https dev server URL', () => { |
| 31 | + mocks.getDevServerUrl.mockReturnValue('HTTPS://Example.COM:19000/'); |
| 32 | + |
| 33 | + expect(getWSServer()).toBe(`wss://example.com:19000${HARNESS_BRIDGE_PATH}`); |
| 34 | + }); |
| 35 | + |
| 36 | + it('preserves the explicit port for hostnames', () => { |
| 37 | + mocks.getDevServerUrl.mockReturnValue('http://example.com:31337/status'); |
| 38 | + |
| 39 | + expect(getWSServer()).toBe(`ws://example.com:31337${HARNESS_BRIDGE_PATH}`); |
| 40 | + }); |
| 41 | + |
| 42 | + it('drops user info while preserving the host for ipv6 URLs', () => { |
| 43 | + mocks.getDevServerUrl.mockReturnValue( |
| 44 | + 'http://user:secret@[::1]:8081/status', |
| 45 | + ); |
| 46 | + |
| 47 | + expect(getWSServer()).toBe(`ws://[::1]:8081${HARNESS_BRIDGE_PATH}`); |
| 48 | + }); |
| 49 | + |
| 50 | + it('preserves the port for ipv6 URLs without user info', () => { |
| 51 | + mocks.getDevServerUrl.mockReturnValue('http://[2001:db8::1]:19001/status'); |
| 52 | + |
| 53 | + expect(getWSServer()).toBe( |
| 54 | + `ws://[2001:db8::1]:19001${HARNESS_BRIDGE_PATH}`, |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('throws for non-absolute dev server URLs', () => { |
| 59 | + mocks.getDevServerUrl.mockReturnValue('localhost:8081'); |
| 60 | + |
| 61 | + expect(() => getWSServer()).toThrow( |
| 62 | + new TypeError('Invalid URL: localhost:8081'), |
| 63 | + ); |
| 64 | + }); |
| 65 | +}); |
0 commit comments