import { stripAttributes } from '../'; const appendAttributes = (id: string, attributes: Record) => `${id}${id.includes('?') ? '&' : '?'}attributes=${encodeURIComponent( JSON.stringify(attributes) )}`; test('leaves ids without attributes untouched', () => { expect(stripAttributes('a.js')).toBe('a.js'); expect(stripAttributes('/absolute/path/a.js')).toBe('/absolute/path/a.js'); expect(stripAttributes('')).toBe(''); }); test('removes a ?attributes= suffix', () => { expect(stripAttributes('a.js?attributes=%7B%22type%22%3A%22json%22%7D')).toBe('a.js'); expect(stripAttributes(appendAttributes('a.js', { type: 'json' }))).toBe('a.js'); }); test('removes an &attributes= suffix', () => { expect(stripAttributes('module?plugin&attributes=%7B%22type%22%3A%22json%22%7D')).toBe( 'module?plugin' ); }); test('preserves genuine query strings', () => { expect(stripAttributes('module?plugin')).toBe('module?plugin'); expect(stripAttributes('module?attributes')).toBe('module?attributes'); expect(stripAttributes('module?plugin=true')).toBe('module?plugin=true'); }); test('keeps the attributes when the suffix does not round-trip', () => { expect(stripAttributes('a.js?attributes=not-json')).toBe('a.js?attributes=not-json'); expect(stripAttributes('a.js?attributes=%7Bnot-json')).toBe('a.js?attributes=%7Bnot-json'); expect(stripAttributes('a.js?attributes=%7B%7D')).toBe('a.js?attributes=%7B%7D'); expect(stripAttributes('a.js?attributes=%7B%22b%22%3A%22%22%2C%22a%22%3A%22%22%7D')).toBe( 'a.js?attributes=%7B%22b%22%3A%22%22%2C%22a%22%3A%22%22%7D' ); }); test('handles ids containing NUL', () => { expect(stripAttributes('\0virtual')).toBe('\0virtual'); expect(stripAttributes(appendAttributes('\0virtual', { type: 'json' }))).toBe('\0virtual'); }); test('handles suffixes with a __proto__ attribute', () => { const attributes = JSON.parse('{"__proto__":"json"}'); expect(stripAttributes(appendAttributes('a.js', attributes))).toBe('a.js'); const unsorted = JSON.parse('{"type":"json","__proto__":"json"}'); expect(stripAttributes(appendAttributes('a.js', unsorted))).toBe( appendAttributes('a.js', unsorted) ); });