|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { HOME, normalizeUrl } from './navigation'; |
| 3 | + |
| 4 | +describe('normalizeUrl', () => { |
| 5 | + it('returns home for blank input', () => { |
| 6 | + expect(normalizeUrl(' ')).toBe(HOME); |
| 7 | + }); |
| 8 | + |
| 9 | + it('keeps valid HTTP(S) URLs', () => { |
| 10 | + expect(normalizeUrl('https://example.com/path?q=1')).toBe( |
| 11 | + 'https://example.com/path?q=1', |
| 12 | + ); |
| 13 | + expect(normalizeUrl('http://localhost:3000/health')).toBe( |
| 14 | + 'http://localhost:3000/health', |
| 15 | + ); |
| 16 | + }); |
| 17 | + |
| 18 | + it('promotes a complete domain to HTTPS', () => { |
| 19 | + expect(normalizeUrl('docs.example.com/path')).toBe( |
| 20 | + 'https://docs.example.com/path', |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it('searches ordinary text', () => { |
| 25 | + expect(normalizeUrl('privacy first browser')).toBe( |
| 26 | + 'https://duckduckgo.com/?q=privacy%20first%20browser', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('searches unsupported schemes instead of loading them', () => { |
| 31 | + expect(normalizeUrl('javascript:alert(1)')).toBe( |
| 32 | + 'https://duckduckgo.com/?q=javascript%3Aalert(1)', |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not treat domain-looking text with spaces as a URL', () => { |
| 37 | + expect(normalizeUrl('example.com malicious suffix')).toBe( |
| 38 | + 'https://duckduckgo.com/?q=example.com%20malicious%20suffix', |
| 39 | + ); |
| 40 | + }); |
| 41 | +}); |
0 commit comments