|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { filterUrlQuery } from '../../../../src/utils/data-collection/filterUrlQuery'; |
| 3 | + |
| 4 | +describe('filterUrlQuery', () => { |
| 5 | + describe('no query string', () => { |
| 6 | + it('returns the URL unchanged', () => { |
| 7 | + expect(filterUrlQuery('https://example.com/api/users', true)).toBe('https://example.com/api/users'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('returns a URL with only a fragment unchanged', () => { |
| 11 | + expect(filterUrlQuery('https://example.com/docs#section', true)).toBe('https://example.com/docs#section'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('leaves a trailing `?` with no params alone', () => { |
| 15 | + expect(filterUrlQuery('https://example.com/api?', true)).toBe('https://example.com/api'); |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('denyList mode (true)', () => { |
| 20 | + it('filters sensitive params and preserves the rest', () => { |
| 21 | + const result = filterUrlQuery('https://example.com/api/users?token=abc123&q=a%20b%26c&page=5', true); |
| 22 | + |
| 23 | + expect(result).toBe('https://example.com/api/users?token=[Filtered]&q=a%20b%26c&page=5'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('preserves the fragment', () => { |
| 27 | + const result = filterUrlQuery('https://example.com/api?token=abc&page=5#results', true); |
| 28 | + |
| 29 | + expect(result).toBe('https://example.com/api?token=[Filtered]&page=5#results'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('preserves userinfo, port and path', () => { |
| 33 | + const result = filterUrlQuery('https://user:pw@example.com:8443/a/b?secret=x&ok=1', true); |
| 34 | + |
| 35 | + expect(result).toBe('https://user:pw@example.com:8443/a/b?secret=[Filtered]&ok=1'); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('off mode (false)', () => { |
| 40 | + it('removes the query entirely', () => { |
| 41 | + expect(filterUrlQuery('https://example.com/api/users?token=abc&page=5', false)).toBe( |
| 42 | + 'https://example.com/api/users', |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('removes the query but keeps the fragment', () => { |
| 47 | + expect(filterUrlQuery('https://example.com/api?token=abc#results', false)).toBe( |
| 48 | + 'https://example.com/api#results', |
| 49 | + ); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('allow / deny behaviors', () => { |
| 54 | + it('supports allowList mode', () => { |
| 55 | + const result = filterUrlQuery('https://example.com/s?page=1&ref=x&sort=name', { allow: ['page', 'sort'] }); |
| 56 | + |
| 57 | + expect(result).toBe('https://example.com/s?page=1&ref=[Filtered]&sort=name'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('supports extra deny terms', () => { |
| 61 | + const result = filterUrlQuery('https://example.com/s?page=1&utm_source=email', { deny: ['utm'] }); |
| 62 | + |
| 63 | + expect(result).toBe('https://example.com/s?page=1&utm_source=[Filtered]'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('non-standard URLs', () => { |
| 68 | + it('handles relative URLs', () => { |
| 69 | + expect(filterUrlQuery('/api/users?token=abc&page=5', true)).toBe('/api/users?token=[Filtered]&page=5'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('preserves duplicate params and their order', () => { |
| 73 | + const result = filterUrlQuery('https://example.com/s?page=1&token=a&page=2', true); |
| 74 | + |
| 75 | + expect(result).toBe('https://example.com/s?page=1&token=[Filtered]&page=2'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not treat a `?` inside a fragment as a query', () => { |
| 79 | + const result = filterUrlQuery('https://example.com/docs#/route?token=abc', true); |
| 80 | + |
| 81 | + expect(result).toBe('https://example.com/docs#/route?token=abc'); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments