|
| 1 | +import { |
| 2 | + sanitizeQueryParamValue, |
| 3 | + sanitizeQueryParamValueInPath, |
| 4 | +} from './sanitizeQueryParam'; |
| 5 | + |
| 6 | +describe('sanitizeQueryParamValue', () => { |
| 7 | + it('encodes reserved characters that break parsing (#, &, +, space)', () => { |
| 8 | + expect(sanitizeQueryParamValue('Widget #2')).toBe('Widget%20%232'); |
| 9 | + expect(sanitizeQueryParamValue('AT&T')).toBe('AT%26T'); |
| 10 | + expect(sanitizeQueryParamValue('plus+sign')).toBe('plus%2Bsign'); |
| 11 | + expect(sanitizeQueryParamValue('has space')).toBe('has%20space'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('preserves valid percent-escapes but encodes stray %', () => { |
| 15 | + expect(sanitizeQueryParamValue('already%23encoded')).toBe( |
| 16 | + 'already%23encoded' |
| 17 | + ); |
| 18 | + expect(sanitizeQueryParamValue('100% legit')).toBe('100%25%20legit'); |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('sanitizeQueryParamValueInPath', () => { |
| 23 | + const origin = 'https://example.com'; |
| 24 | + |
| 25 | + it('keeps full product_name when it contains #', () => { |
| 26 | + const safePath = sanitizeQueryParamValueInPath( |
| 27 | + '/p?product_name=Widget #2', |
| 28 | + 'product_name' |
| 29 | + ); |
| 30 | + const url = new URL(safePath, origin); |
| 31 | + |
| 32 | + expect(url.searchParams.get('product_name')).toBe('Widget #2'); |
| 33 | + expect(url.hash).toBe(''); |
| 34 | + }); |
| 35 | + |
| 36 | + it('keeps full product_name when it contains &', () => { |
| 37 | + const safePath = sanitizeQueryParamValueInPath( |
| 38 | + '/p?product_name=AT&T', |
| 39 | + 'product_name' |
| 40 | + ); |
| 41 | + const url = new URL(safePath, origin); |
| 42 | + |
| 43 | + expect(url.searchParams.get('product_name')).toBe('AT&T'); |
| 44 | + // Ensure it didn't accidentally create an extra param |
| 45 | + expect(Array.from(url.searchParams.keys())).toEqual(['product_name']); |
| 46 | + }); |
| 47 | + |
| 48 | + it('keeps plus sign as literal + (not space)', () => { |
| 49 | + const safePath = sanitizeQueryParamValueInPath( |
| 50 | + '/p?product_name=plus+sign', |
| 51 | + 'product_name' |
| 52 | + ); |
| 53 | + const url = new URL(safePath, origin); |
| 54 | + |
| 55 | + expect(url.searchParams.get('product_name')).toBe('plus+sign'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not modify other params while sanitizing the target param', () => { |
| 59 | + const safePath = sanitizeQueryParamValueInPath( |
| 60 | + '/p?exchangeToken=abc&product_name=Widget #2', |
| 61 | + 'product_name' |
| 62 | + ); |
| 63 | + const url = new URL(safePath, origin); |
| 64 | + |
| 65 | + expect(url.searchParams.get('exchangeToken')).toBe('abc'); |
| 66 | + expect(url.searchParams.get('product_name')).toBe('Widget #2'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns the original path if param is missing', () => { |
| 70 | + expect(sanitizeQueryParamValueInPath('/p?foo=bar', 'product_name')).toBe( |
| 71 | + '/p?foo=bar' |
| 72 | + ); |
| 73 | + }); |
| 74 | +}); |
0 commit comments