|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { CrossLocalhostIssueType, detectCrossLocalhostUrls } from './detectCrossLocalhostUrls'; |
| 3 | + |
| 4 | +describe('detectCrossLocalhostUrls', () => { |
| 5 | + const chrome = 'Chrome'; |
| 6 | + const firefox = 'Firefox'; |
| 7 | + const safari = 'Safari'; |
| 8 | + |
| 9 | + it('returns null when operationsUrl is null', () => { |
| 10 | + expect(detectCrossLocalhostUrls(chrome, 'localhost', null)).toBe(null); |
| 11 | + }); |
| 12 | + |
| 13 | + it('returns null when operationsUrl is undefined', () => { |
| 14 | + expect(detectCrossLocalhostUrls(chrome, 'localhost', undefined)).toBe(null); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns null when operationsUrl is empty string', () => { |
| 18 | + expect(detectCrossLocalhostUrls(chrome, 'localhost', '')).toBe(null); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns null when browserHostname is missing', () => { |
| 22 | + expect(detectCrossLocalhostUrls(chrome, undefined, 'http://localhost:3000')).toBe(null); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns null when browserHostname is not localhost or 127.0.0.1', () => { |
| 26 | + expect(detectCrossLocalhostUrls(chrome, 'example.com', 'http://localhost:3000')).toBe(null); |
| 27 | + expect(detectCrossLocalhostUrls(chrome, 'my.dev', 'http://127.0.0.1:3000')).toBe(null); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns true when browser is localhost and operationsUrl is 127.0.0.1', () => { |
| 31 | + expect(detectCrossLocalhostUrls(chrome, 'localhost', 'http://127.0.0.1:3000')).toBe(CrossLocalhostIssueType.MixedLoopback); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns true when browser is 127.0.0.1 and operationsUrl is localhost', () => { |
| 35 | + expect(detectCrossLocalhostUrls(chrome, '127.0.0.1', 'http://localhost:8080')).toBe(CrossLocalhostIssueType.MixedLoopback); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns null when both hostnames match (localhost)', () => { |
| 39 | + expect(detectCrossLocalhostUrls(chrome, 'localhost', 'http://localhost:8080')).toBe(null); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns null when both hostnames match (127.0.0.1)', () => { |
| 43 | + expect(detectCrossLocalhostUrls(chrome, '127.0.0.1', 'http://127.0.0.1:8080')).toBe(null); |
| 44 | + }); |
| 45 | + |
| 46 | + it('ignores port differences and only compares hostname', () => { |
| 47 | + // Hostnames differ -> true |
| 48 | + expect(detectCrossLocalhostUrls(chrome, 'localhost', 'http://127.0.0.1:1234')).toBe(CrossLocalhostIssueType.MixedLoopback); |
| 49 | + // Hostnames same -> null even if ports different |
| 50 | + expect(detectCrossLocalhostUrls(chrome, 'localhost', 'http://localhost:1234')).toBe(null); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns true when connecting from the cloud to local outside Chrome and Firefox', () => { |
| 54 | + expect(detectCrossLocalhostUrls(chrome, 'prod.com', 'http://127.0.0.1:8080')).toBe(null); |
| 55 | + expect(detectCrossLocalhostUrls(firefox, 'prod.com', 'http://127.0.0.1:8080')).toBe(null); |
| 56 | + expect(detectCrossLocalhostUrls(safari, 'prod.com', 'http://127.0.0.1:8080')).toBe(CrossLocalhostIssueType.InsecureCookieOutsideChromeAndFirefox); |
| 57 | + }); |
| 58 | +}); |
0 commit comments