|
| 1 | +import {describe, expect, it} from 'vitest'; |
| 2 | + |
| 3 | +import {sanitizeNext} from './utils'; |
| 4 | + |
| 5 | +describe('sanitizeNext', () => { |
| 6 | + it('should return an empty string for external URLs', () => { |
| 7 | + expect(sanitizeNext('http://example.com')).toBe(''); |
| 8 | + expect(sanitizeNext('https://example.com')).toBe(''); |
| 9 | + expect(sanitizeNext('//example.com')).toBe(''); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should prepend a slash if missing', () => { |
| 13 | + expect(sanitizeNext('path/to/resource')).toBe('/path/to/resource'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should not modify a valid internal path', () => { |
| 17 | + expect(sanitizeNext('/path/to/resource')).toBe('/path/to/resource'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should remove unsafe characters', () => { |
| 21 | + expect(sanitizeNext('/path/to/resource?query=1')).toBe('/path/to/resource'); |
| 22 | + expect(sanitizeNext('/path/to/resource#hash')).toBe('/path/to/resource'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should allow alphanumeric and hyphens', () => { |
| 26 | + expect(sanitizeNext('/path-to/resource123')).toBe('/path-to/resource123'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return an empty string for paths with colons', () => { |
| 30 | + expect(sanitizeNext('/path:to/resource')).toBe(''); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return an empty string for the root path', () => { |
| 34 | + expect(sanitizeNext('/')).toBe(''); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should decode URL encoded characters', () => { |
| 38 | + expect(sanitizeNext('/path%2Fwith%2Fslashes')).toBe('/path/with/slashes'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return an empty string for a malformed URI component', () => { |
| 42 | + const input = '%E0%A4%A'; // Malformed URI |
| 43 | + const expectedOutput = ''; |
| 44 | + expect(sanitizeNext(input)).toBe(expectedOutput); |
| 45 | + }); |
| 46 | +}); |
0 commit comments