Skip to content

Commit f4938f5

Browse files
committed
[Tests] parse: characterize current lenient handling of unbalanced bracket keys (#558)
Pin the current (lenient) parse output for every malformed/unbalanced bracket-key shape, so any future change to that behavior (e.g. the proposed "throw on unbalanced brackets" in #560) is explicit in the test diff rather than silent. Each case asserts both that parsing does not throw and the exact output, via a small `characterizeParse` helper, so a change that introduces a throw fails cleanly per case instead of aborting the run. Covers the cases #560 would make throw (`a[bc`, `a[`, `a[b][c`, `a[b]c[d`, the issue reproduction, stray-close variants), the ones it would leave as literals (inner-bracket and bracket-prefixed keys), the depth- and allowDots-dependent shapes, and the unaffected stray-close/trailing-text keys.
1 parent 5d5f723 commit f4938f5

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

‎test/parse.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ var hasProto = require('has-proto')();
1414
var qs = require('../');
1515
var utils = require('../lib/utils');
1616

17+
var characterizeParse = function characterizeParse(st, input, opts, expected, label) {
18+
var result;
19+
st.doesNotThrow(function () { result = qs.parse(input, opts); }, label + ': does not throw');
20+
st.deepEqual(result, expected, label + ': parses to the current lenient output');
21+
};
22+
1723
test('parse()', function (t) {
1824
t.test('parses a simple string', function (st) {
1925
st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
@@ -312,6 +318,48 @@ test('parse()', function (t) {
312318
st.end();
313319
});
314320

321+
t.test('currently parses unbalanced bracket keys after a parent leniently to literal segments (issue #558)', function (st) {
322+
characterizeParse(st, 'a[bc=v', undefined, { a: { '[bc': 'v' } }, 'unclosed group after a parent');
323+
characterizeParse(st, 'a[=v', undefined, { a: { '[': 'v' } }, 'bare unclosed bracket after a parent');
324+
characterizeParse(st, 'a[b][c=v', undefined, { a: { b: { '[c': 'v' } } }, 'unclosed group after a valid one');
325+
characterizeParse(st, 'a[b]c[d=v', undefined, { a: { b: { '[d': 'v' } } }, 'unclosed group after text following a valid one');
326+
characterizeParse(st, 'filters[customtags:Env: Prod=v', undefined, { filters: { '[customtags:Env: Prod': 'v' } }, 'the issue #558 reproduction');
327+
characterizeParse(st, '][a=v', undefined, { ']': { '[a': 'v' } }, 'stray close bracket before an unclosed group');
328+
characterizeParse(st, 'a][b=v', undefined, { 'a]': { '[b': 'v' } }, 'stray close bracket inside the parent');
329+
st.end();
330+
});
331+
332+
t.test('currently parses unbalanced bracket keys containing inner brackets leniently (issue #558)', function (st) {
333+
characterizeParse(st, 'a[b[c=v', undefined, { a: { '[b[c': 'v' } }, 'unclosed group containing an inner bracket');
334+
characterizeParse(st, 'a[b[c]=v', undefined, { a: { '[b[c]': 'v' } }, 'unbalanced group with an inner bracket and one close');
335+
characterizeParse(st, 'a[b][c[d=v', undefined, { a: { b: { '[c[d': 'v' } } }, 'unclosed inner-bracket group after a valid one');
336+
st.end();
337+
});
338+
339+
t.test('currently parses bracket-prefixed unbalanced keys leniently (issue #558)', function (st) {
340+
characterizeParse(st, '[abc=v', undefined, { '[abc': 'v' }, 'key starting with an unclosed bracket');
341+
characterizeParse(st, '[[]b=v', undefined, { '[[]b': 'v' }, 'key starting with an unbalanced bracket group');
342+
st.end();
343+
});
344+
345+
t.test('lenient unbalanced-bracket handling currently depends on the depth option (issue #558)', function (st) {
346+
characterizeParse(st, 'a[b]c[d]e[f=v', { depth: 5 }, { a: { b: { d: { '[f': 'v' } } } }, 'consumes groups up to the depth budget then keeps the unclosed remainder literal');
347+
characterizeParse(st, 'a[b]c[d]e[f=v', { depth: 1 }, { a: { b: { '[d]e[f': 'v' } } }, 'a lower depth keeps more of the unclosed remainder literal');
348+
characterizeParse(st, 'a[bc=v', { depth: 0 }, { 'a[bc': 'v' }, 'depth 0 keeps the entire key literal');
349+
st.end();
350+
});
351+
352+
t.test('currently parses an allowDots key with a trailing unclosed bracket leniently (issue #558)', function (st) {
353+
characterizeParse(st, 'a.b[c=v', { allowDots: true }, { a: { b: { '[c': 'v' } } }, 'allowDots expands the dot then keeps the unclosed bracket literal');
354+
st.end();
355+
});
356+
357+
t.test('valid and stray-close bracket keys are unaffected by unbalanced-bracket handling', function (st) {
358+
characterizeParse(st, 'a]b=v', undefined, { 'a]b': 'v' }, 'stray close bracket with no open bracket stays a flat key');
359+
characterizeParse(st, 'a[b]extra=v', undefined, { a: { b: 'v' } }, 'text after a balanced group is ignored');
360+
st.end();
361+
});
362+
315363
t.test('allows to specify array indices', function (st) {
316364
st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
317365
st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });

0 commit comments

Comments
 (0)