|
| 1 | +import test from 'ava'; |
| 2 | +import queryString from './index.js'; |
| 3 | + |
| 4 | +test('parse() drops empty and whitespace-only keys', t => { |
| 5 | + // Original issue - encoded whitespace key |
| 6 | + t.deepEqual(queryString.parse('?%20&'), {}); |
| 7 | + |
| 8 | + // Various whitespace encodings |
| 9 | + t.deepEqual(queryString.parse('?%20'), {}); |
| 10 | + t.deepEqual(queryString.parse('?%09'), {}); // Tab |
| 11 | + t.deepEqual(queryString.parse('?+'), {}); // Plus as space |
| 12 | + |
| 13 | + // Empty keys |
| 14 | + t.deepEqual(queryString.parse('?&&'), {}); |
| 15 | + |
| 16 | + // Mixed valid and invalid keys |
| 17 | + t.deepEqual(queryString.parse('?valid=1&%20&another=2'), { |
| 18 | + valid: '1', |
| 19 | + another: '2', |
| 20 | + }); |
| 21 | + |
| 22 | + // Valid keys are preserved |
| 23 | + t.deepEqual(queryString.parse('?a'), {a: null}); |
| 24 | + t.deepEqual(queryString.parse('?a='), {a: ''}); |
| 25 | +}); |
| 26 | + |
| 27 | +test('stringify() ignores empty and whitespace keys', t => { |
| 28 | + // Empty and whitespace keys |
| 29 | + t.is(queryString.stringify({'': 'value'}), ''); |
| 30 | + t.is(queryString.stringify({' ': 'value'}), ''); |
| 31 | + t.is(queryString.stringify({'\t': 'value'}), ''); |
| 32 | + |
| 33 | + // Mixed valid and invalid |
| 34 | + t.is(queryString.stringify({valid: '1', '': 'ignored'}), 'valid=1'); |
| 35 | + |
| 36 | + // Valid keys work normally |
| 37 | + t.is(queryString.stringify({a: null}), 'a'); |
| 38 | + t.is(queryString.stringify({a: ''}), 'a='); |
| 39 | +}); |
| 40 | + |
| 41 | +test('symmetry: parse and stringify round-trip', t => { |
| 42 | + // Original issue case |
| 43 | + t.is(queryString.stringify(queryString.parse('?%20&')), ''); |
| 44 | + |
| 45 | + // Empty keys |
| 46 | + t.is(queryString.stringify(queryString.parse('?&&')), ''); |
| 47 | + |
| 48 | + // Mixed keys maintain valid ones |
| 49 | + t.is(queryString.stringify(queryString.parse('?valid=1&%20&')), 'valid=1'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('array formats handle empty keys correctly', t => { |
| 53 | + // Parse with different array formats |
| 54 | + t.deepEqual(queryString.parse('?%20[]=1', {arrayFormat: 'bracket'}), {}); |
| 55 | + t.deepEqual(queryString.parse('?%20[0]=1', {arrayFormat: 'index'}), {}); |
| 56 | + t.deepEqual(queryString.parse('?%20=1,2', {arrayFormat: 'comma'}), {}); |
| 57 | + |
| 58 | + // Stringify with different array formats |
| 59 | + t.is(queryString.stringify({'': ['1']}, {arrayFormat: 'bracket'}), ''); |
| 60 | + t.is(queryString.stringify({' ': ['1']}, {arrayFormat: 'index'}), ''); |
| 61 | + t.is(queryString.stringify({'': ['1', '2']}, {arrayFormat: 'comma'}), ''); |
| 62 | +}); |
0 commit comments