import { describe, it } from 'bun:test'; import { expect } from 'chai'; import { FormUtil } from './form-util'; describe('FormUtil.parse', () => { // What qs.parse returned with the options body-parser gave it (extended: true). const cases: [string, unknown][] = [ ['a=1&b=2', { a: '1', b: '2' }], ['a=1&a=2', { a: ['1', '2'] }], ['a[b]=1&a[c]=2', { a: { b: '1', c: '2' } }], ['a[]=1&a[]=2', { a: ['1', '2'] }], ['a[0]=x&a[1]=y', { a: ['x', 'y'] }], ['a[1]=x', { a: ['x'] }], ['a[25]=x', { a: ['x'] }], ['a[200]=x', { a: { 200: 'x' } }], ['x=a+b%20c', { x: 'a b c' }], ['k', { k: '' }], ['a[b][c][d]=1', { a: { b: { c: { d: '1' } } } }], ['__proto__[x]=1&ok=1', { ok: '1' }], ['a=%E4%BD%A0', { a: '你' }], ['a[]=1&a[b]=2', { a: { 0: '1', b: '2' } }], ['=v&k=', { k: '' }], ['a=%ZZ', { a: '%ZZ' }], ['arr[]=a&arr[]=b&arr[]=c', { arr: ['a', 'b', 'c'] }], ['', {}] ]; for (const [text, expected] of cases) { it(`parses ${JSON.stringify(text)} as qs did`, () => { expect(FormUtil.parse(text)).to.deep.equal(expected); }); } it('does not touch Object.prototype', () => { FormUtil.parse('__proto__[polluted]=1&constructor[prototype][polluted]=1&a[__proto__][polluted]=1'); expect(({} as Record).polluted).to.equal(undefined); }); it('keeps levels past depth together', () => { expect(FormUtil.parse('a[b][c]=1', { depth: 1 })).to.deep.equal({ a: { b: { '[c]': '1' } } }); }); it('refuses too many parameters', () => { expect(() => FormUtil.parse('a=1&b=2&c=3', { parameterLimit: 2 })).to.throw(RangeError); }); });