Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1'use strict';2 3var test = require('tape');4var hasPropertyDescriptors = require('has-property-descriptors')();5var iconv = require('iconv-lite');6var mockProperty = require('mock-property');7var hasOverrideMistake = require('has-override-mistake')();8var SaferBuffer = require('safer-buffer').Buffer;9var v = require('es-value-fixtures');10var inspect = require('object-inspect');11var emptyTestCases = require('./empty-keys-cases').emptyTestCases;12var hasProto = require('has-proto')();13 14var qs = require('../');15var utils = require('../lib/utils');16 17test('parse()', function (t) {18 t.test('parses a simple string', function (st) {19 st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });20 st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });21 st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });22 st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });23 st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });24 st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });25 st.deepEqual(qs.parse('foo'), { foo: '' });26 st.deepEqual(qs.parse('foo='), { foo: '' });27 st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });28 st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });29 st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });30 st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });31 st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });32 st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });33 st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });34 st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {35 cht: 'p3',36 chd: 't:60,40',37 chs: '250x100',38 chl: 'Hello|World'39 });40 st.end();41 });42 43 t.test('comma: false', function (st) {44 st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });45 st.deepEqual(qs.parse('a[0]=b&a[1]=c'), { a: ['b', 'c'] });46 st.deepEqual(qs.parse('a=b,c'), { a: 'b,c' });47 st.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] });48 st.end();49 });50 51 t.test('comma: true', function (st) {52 st.deepEqual(qs.parse('a[]=b&a[]=c', { comma: true }), { a: ['b', 'c'] });53 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { comma: true }), { a: ['b', 'c'] });54 st.deepEqual(qs.parse('a=b,c', { comma: true }), { a: ['b', 'c'] });55 st.deepEqual(qs.parse('a=b&a=c', { comma: true }), { a: ['b', 'c'] });56 st.end();57 });58 59 t.test('allows enabling dot notation', function (st) {60 st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });61 st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });62 63 st.end();64 });65 66 t.test('decode dot keys correctly', function (st) {67 st.deepEqual(68 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: false, decodeDotInKeys: false }),69 { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },70 'with allowDots false and decodeDotInKeys false'71 );72 st.deepEqual(73 qs.parse('name.obj.first=John&name.obj.last=Doe', { allowDots: true, decodeDotInKeys: false }),74 { name: { obj: { first: 'John', last: 'Doe' } } },75 'with allowDots false and decodeDotInKeys false'76 );77 st.deepEqual(78 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: false }),79 { 'name%2Eobj': { first: 'John', last: 'Doe' } },80 'with allowDots true and decodeDotInKeys false'81 );82 st.deepEqual(83 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: true }),84 { 'name.obj': { first: 'John', last: 'Doe' } },85 'with allowDots true and decodeDotInKeys true'86 );87 88 st.deepEqual(89 qs.parse(90 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',91 { allowDots: false, decodeDotInKeys: false }92 ),93 { 'name%2Eobj%2Esubobject.first%2Egodly%2Ename': 'John', 'name%2Eobj%2Esubobject.last': 'Doe' },94 'with allowDots false and decodeDotInKeys false'95 );96 st.deepEqual(97 qs.parse(98 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',99 { allowDots: true, decodeDotInKeys: false }100 ),101 { name: { obj: { subobject: { first: { godly: { name: 'John' } }, last: 'Doe' } } } },102 'with allowDots true and decodeDotInKeys false'103 );104 st.deepEqual(105 qs.parse(106 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',107 { allowDots: true, decodeDotInKeys: true }108 ),109 { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },110 'with allowDots true and decodeDotInKeys true'111 );112 st.deepEqual(113 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe'),114 { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },115 'with allowDots and decodeDotInKeys undefined'116 );117 118 st.end();119 });120 121 t.test('decodes dot in key of object, and allow enabling dot notation when decodeDotInKeys is set to true and allowDots is undefined', function (st) {122 st.deepEqual(123 qs.parse(124 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',125 { decodeDotInKeys: true }126 ),127 { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },128 'with allowDots undefined and decodeDotInKeys true'129 );130 131 st.end();132 });133 134 t.test('throws when decodeDotInKeys is not of type boolean', function (st) {135 st['throws'](136 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 'foobar' }); },137 TypeError138 );139 140 st['throws'](141 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 0 }); },142 TypeError143 );144 st['throws'](145 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: NaN }); },146 TypeError147 );148 149 st['throws'](150 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: null }); },151 TypeError152 );153 154 st.end();155 });156 157 t.test('allows empty arrays in obj values', function (st) {158 st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: true }), { foo: [], bar: 'baz' });159 st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: false }), { foo: [''], bar: 'baz' });160 161 st.end();162 });163 164 t.test('throws when allowEmptyArrays is not of type boolean', function (st) {165 st['throws'](166 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 'foobar' }); },167 TypeError168 );169 170 st['throws'](171 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 0 }); },172 TypeError173 );174 st['throws'](175 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: NaN }); },176 TypeError177 );178 179 st['throws'](180 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: null }); },181 TypeError182 );183 184 st.end();185 });186 187 t.test('allowEmptyArrays + strictNullHandling', function (st) {188 st.deepEqual(189 qs.parse('testEmptyArray[]', { strictNullHandling: true, allowEmptyArrays: true }),190 { testEmptyArray: [] }191 );192 193 st.end();194 });195 196 t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');197 t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');198 t.deepEqual(199 qs.parse('a[b][c][d][e][f][g][h]=i'),200 { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },201 'defaults to a depth of 5'202 );203 204 t.test('only parses one level when depth = 1', function (st) {205 st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });206 st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });207 st.end();208 });209 210 t.test('uses original key when depth = 0', function (st) {211 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });212 st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: 0 }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });213 st.deepEqual(qs.parse('a.b=c', { depth: 0, allowDots: true }), { 'a[b]': 'c' }, 'normalizes dots before applying depth-0 behavior');214 st.deepEqual(qs.parse('toString=foo', { depth: 0 }), {}, 'respects prototype guard at depth 0');215 st.deepEqual(qs.parse('toString=foo', { depth: 0, allowPrototypes: true }), { toString: 'foo' }, 'allows prototypes at depth 0 when enabled');216 st.end();217 });218 219 t.test('ignores prototype keys when depth = 0 and allowPrototypes is false', function (st) {220 st.deepEqual(qs.parse('toString=foo', { depth: 0 }), {});221 st.deepEqual(qs.parse('hasOwnProperty=bar', { depth: 0 }), {});222 st.deepEqual(qs.parse('toString=foo&a=b', { depth: 0 }), { a: 'b' });223 st.end();224 });225 226 t.test('allows prototype keys when depth = 0 and allowPrototypes is true', function (st) {227 st.deepEqual(qs.parse('toString=foo', { depth: 0, allowPrototypes: true }), { toString: 'foo' });228 st.end();229 });230 231 t.test('uses original key when depth = false', function (st) {232 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });233 st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: false }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });234 st.end();235 });236 237 t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');238 239 t.test('parses an explicit array', function (st) {240 st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });241 st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });242 st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });243 st.end();244 });245 246 t.test('parses a mix of simple and explicit arrays', function (st) {247 st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });248 st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });249 st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });250 st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });251 252 st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });253 st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });254 st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });255 256 st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });257 st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });258 st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });259 260 st.end();261 });262 263 t.test('parses a nested array', function (st) {264 st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });265 st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });266 st.end();267 });268 269 t.test('parses keys with literal [] inside a bracket group (#493)', function (st) {270 // A bracket pair inside a bracket group should be treated literally as part of the key271 st.deepEqual(272 qs.parse('search[withbracket[]]=foobar'),273 { search: { 'withbracket[]': 'foobar' } },274 'treats inner [] literally when inside a bracket group'275 );276 277 // Single-level variant278 st.deepEqual(279 qs.parse('a[b[]]=c'),280 { a: { 'b[]': 'c' } },281 'keeps "b[]" as a literal key'282 );283 284 // Nested with an array push on the outer level285 st.deepEqual(286 qs.parse('list[][x[]]=y'),287 { list: [{ 'x[]': 'y' }] },288 'preserves inner [] while still treating outer [] as array push'289 );290 291 // Multiple nested bracket pairs: inner [] remains literal as part of the key292 st.deepEqual(293 qs.parse('a[b[c[]]]=d'),294 { a: { 'b[c[]]': 'd' } },295 'treats "b[c[]]" as a literal key inside the bracket group'296 );297 298 // Depth limits with literal brackets: preserve inner [] while limiting bracket-group parsing299 st.deepEqual(300 qs.parse('a[b[c[]]][d]=e', { depth: 1 }),301 { a: { 'b[c[]]': { '[d]': 'e' } } },302 'respects depth: 1 and preserves literal inner [] in the parsed key'303 );304 305 // Unterminated inner bracket group is wrapped as a literal remainder segment306 st.deepEqual(307 qs.parse('a[[]b=c'),308 { a: { '[[]b': 'c' } },309 'handles unterminated inner bracket groups without throwing'310 );311 312 st.end();313 });314 315 t.test('allows to specify array indices', function (st) {316 st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });317 st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });318 st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });319 st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });320 st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });321 st.end();322 });323 324 t.test('limits specific array indices to arrayLimit', function (st) {325 st.deepEqual(qs.parse('a[19]=a', { arrayLimit: 20 }), { a: ['a'] });326 st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: { 20: 'a' } });327 328 st.deepEqual(qs.parse('a[19]=a'), { a: ['a'] });329 st.deepEqual(qs.parse('a[20]=a'), { a: { 20: 'a' } });330 st.end();331 });332 333 t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');334 335 t.test('supports encoded = signs', function (st) {336 st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });337 st.end();338 });339 340 t.test('is ok with url encoded strings', function (st) {341 st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });342 st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });343 st.end();344 });345 346 t.test('allows brackets in the value', function (st) {347 st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });348 st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });349 st.end();350 });351 352 t.test('allows empty values', function (st) {353 st.deepEqual(qs.parse(''), {});354 st.deepEqual(qs.parse(null), {});355 st.deepEqual(qs.parse(undefined), {});356 st.end();357 });358 359 t.test('transforms arrays to objects', function (st) {360 st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });361 st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });362 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });363 st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });364 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });365 st.deepEqual(qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb'), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });366 367 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });368 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });369 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });370 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });371 st.end();372 });373 374 t.test('transforms arrays to objects (dot notation)', function (st) {375 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });376 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });377 st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });378 st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });379 st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15', '16'], bar: '2' }] });380 st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });381 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });382 st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });383 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });384 st.deepEqual(qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb', { allowDots: true }), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });385 st.end();386 });387 388 t.test('correctly prunes undefined values when converting an array to an object', function (st) {389 st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });390 st.end();391 });392 393 t.test('supports malformed uri characters', function (st) {394 st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });395 st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });396 st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });397 st.end();398 });399 400 t.test('doesn\'t produce empty keys', function (st) {401 st.deepEqual(qs.parse('_r=1&'), { _r: '1' });402 st.end();403 });404 405 t.test('cannot access Object prototype', function (st) {406 qs.parse('constructor[prototype][bad]=bad');407 qs.parse('bad[constructor][prototype][bad]=bad');408 st.equal(typeof Object.prototype.bad, 'undefined');409 st.end();410 });411 412 t.test('parses arrays of objects', function (st) {413 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });414 st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });415 st.end();416 });417 418 t.test('allows for empty strings in arrays', function (st) {419 st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });420 421 st.deepEqual(422 qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),423 { a: ['b', null, 'c', ''] },424 'with arrayLimit 20 + array indices: null then empty string works'425 );426 st.deepEqual(427 qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),428 { a: { 0: 'b', 1: null, 2: 'c', 3: '' } },429 'with arrayLimit 0 + array brackets: null then empty string works'430 );431 432 st.deepEqual(433 qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),434 { a: ['b', '', 'c', null] },435 'with arrayLimit 20 + array indices: empty string then null works'436 );437 st.deepEqual(438 qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),439 { a: { 0: 'b', 1: '', 2: 'c', 3: null } },440 'with arrayLimit 0 + array brackets: empty string then null works'441 );442 443 st.deepEqual(444 qs.parse('a[]=&a[]=b&a[]=c'),445 { a: ['', 'b', 'c'] },446 'array brackets: empty strings work'447 );448 st.end();449 });450 451 t.test('compacts sparse arrays', function (st) {452 st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });453 st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });454 st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });455 st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });456 st.end();457 });458 459 t.test('parses sparse arrays', function (st) {460 /* eslint no-sparse-arrays: 0 */461 st.deepEqual(qs.parse('a[4]=1&a[1]=2', { allowSparse: true }), { a: [, '2', , , '1'] });462 st.deepEqual(qs.parse('a[1][b][2][c]=1', { allowSparse: true }), { a: [, { b: [, , { c: '1' }] }] });463 st.deepEqual(qs.parse('a[1][2][3][c]=1', { allowSparse: true }), { a: [, [, , [, , , { c: '1' }]]] });464 st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { allowSparse: true }), { a: [, [, , [, , , { c: [, '1'] }]]] });465 st.end();466 });467 468 t.test('parses semi-parsed strings', function (st) {469 st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });470 st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });471 st.end();472 });473 474 t.test('parses buffers correctly', function (st) {475 var b = SaferBuffer.from('test');476 st.deepEqual(qs.parse({ a: b }), { a: b });477 st.end();478 });479 480 t.test('parses jquery-param strings', function (st) {481 // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'482 var encoded = 'filter%5B0%5D%5B%5D=int1&filter%5B0%5D%5B%5D=%3D&filter%5B0%5D%5B%5D=77&filter%5B%5D=and&filter%5B2%5D%5B%5D=int2&filter%5B2%5D%5B%5D=%3D&filter%5B2%5D%5B%5D=8';483 var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };484 st.deepEqual(qs.parse(encoded), expected);485 st.end();486 });487 488 t.test('continues parsing when no parent is found', function (st) {489 st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });490 st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });491 st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });492 st.end();493 });494 495 t.test('does not error when parsing a very long array', function (st) {496 var str = 'a[]=a';497 while (Buffer.byteLength(str) < 128 * 1024) {498 str = str + '&' + str;499 }500 501 st.doesNotThrow(function () {502 qs.parse(str);503 });504 505 st.end();506 });507 508 t.test('does not throw when a native prototype has an enumerable property', function (st) {509 st.intercept(Object.prototype, 'crash', { value: '' });510 st.intercept(Array.prototype, 'crash', { value: '' });511 512 st.doesNotThrow(qs.parse.bind(null, 'a=b'));513 st.deepEqual(qs.parse('a=b'), { a: 'b' });514 st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));515 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });516 517 st.end();518 });519 520 t.test('parses a string with an alternative string delimiter', function (st) {521 st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });522 st.end();523 });524 525 t.test('parses a string with an alternative RegExp delimiter', function (st) {526 st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });527 st.end();528 });529 530 t.test('does not use non-splittable objects as delimiters', function (st) {531 st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });532 st.end();533 });534 535 t.test('allows overriding parameter limit', function (st) {536 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });537 st.end();538 });539 540 t.test('allows setting the parameter limit to Infinity', function (st) {541 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });542 st.end();543 });544 545 t.test('allows overriding array limit', function (st) {546 st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });547 st.deepEqual(qs.parse('a[0]=b', { arrayLimit: 0 }), { a: { 0: 'b' } });548 549 st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });550 st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: 0 }), { a: { '-1': 'b' } });551 552 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: -1 }), { a: { 0: 'b', 1: 'c' } });553 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });554 555 st.end();556 });557 558 t.test('allows disabling array parsing', function (st) {559 var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });560 st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });561 st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');562 563 var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });564 st.deepEqual(emptyBrackets, { a: { 0: 'b' } });565 st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');566 567 st.end();568 });569 570 t.test('allows for query string prefix', function (st) {571 st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });572 st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });573 st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });574 575 st.end();576 });577 578 t.test('parses an object', function (st) {579 var input = {580 'user[name]': { 'pop[bob]': 3 },581 'user[email]': null582 };583 584 var expected = {585 user: {586 name: { 'pop[bob]': 3 },587 email: null588 }589 };590 591 var result = qs.parse(input);592 593 st.deepEqual(result, expected);594 st.end();595 });596 597 t.test('parses string with comma as array divider', function (st) {598 st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });599 st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });600 st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });601 st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });602 st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });603 604 // test cases inversed from from stringify tests605 st.deepEqual(qs.parse('a[0]=c'), { a: ['c'] });606 st.deepEqual(qs.parse('a[]=c'), { a: ['c'] });607 st.deepEqual(qs.parse('a[]=c', { comma: true }), { a: ['c'] });608 609 st.deepEqual(qs.parse('a[0]=c&a[1]=d'), { a: ['c', 'd'] });610 st.deepEqual(qs.parse('a[]=c&a[]=d'), { a: ['c', 'd'] });611 st.deepEqual(qs.parse('a=c,d', { comma: true }), { a: ['c', 'd'] });612 613 st.end();614 });615 616 t.test('parses values with comma as array divider', function (st) {617 st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });618 st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });619 st.end();620 });621 622 t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {623 var decoder = function (str, defaultDecoder, charset, type) {624 if (!isNaN(Number(str))) {625 return parseFloat(str);626 }627 return defaultDecoder(str, defaultDecoder, charset, type);628 };629 630 st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });631 st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });632 633 st.end();634 });635 636 t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {637 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });638 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });639 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });640 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });641 642 st.end();643 });644 645 t.test('parses url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {646 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });647 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });648 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });649 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });650 651 st.end();652 });653 654 t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {655 st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });656 st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });657 st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });658 659 st.end();660 });661 662 t.test('parses an object in dot notation', function (st) {663 var input = {664 'user.name': { 'pop[bob]': 3 },665 'user.email.': null666 };667 668 var expected = {669 user: {670 name: { 'pop[bob]': 3 },671 email: null672 }673 };674 675 var result = qs.parse(input, { allowDots: true });676 677 st.deepEqual(result, expected);678 st.end();679 });680 681 t.test('parses an object and not child values', function (st) {682 var input = {683 'user[name]': { 'pop[bob]': { test: 3 } },684 'user[email]': null685 };686 687 var expected = {688 user: {689 name: { 'pop[bob]': { test: 3 } },690 email: null691 }692 };693 694 var result = qs.parse(input);695 696 st.deepEqual(result, expected);697 st.end();698 });699 700 t.test('does not blow up when Buffer global is missing', function (st) {701 var restore = mockProperty(global, 'Buffer', { 'delete': true });702 703 var result = qs.parse('a=b&c=d');704 705 restore();706 707 st.deepEqual(result, { a: 'b', c: 'd' });708 st.end();709 });710 711 t.test('does not crash when parsing circular references', function (st) {712 var a = {};713 a.b = a;714 715 var parsed;716 717 st.doesNotThrow(function () {718 parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });719 });720 721 st.equal('foo' in parsed, true, 'parsed has "foo" property');722 st.equal('bar' in parsed.foo, true);723 st.equal('baz' in parsed.foo, true);724 st.equal(parsed.foo.bar, 'baz');725 st.deepEqual(parsed.foo.baz, a);726 st.end();727 });728 729 t.test('does not crash when parsing deep objects', function (st) {730 var parsed;731 var str = 'foo';732 733 for (var i = 0; i < 5000; i++) {734 str += '[p]';735 }736 737 str += '=bar';738 739 st.doesNotThrow(function () {740 parsed = qs.parse(str, { depth: 5000 });741 });742 743 st.equal('foo' in parsed, true, 'parsed has "foo" property');744 745 var depth = 0;746 var ref = parsed.foo;747 while ((ref = ref.p)) {748 depth += 1;749 }750 751 st.equal(depth, 5000, 'parsed is 5000 properties deep');752 753 st.end();754 });755 756 t.test('parses null objects correctly', { skip: !hasProto }, function (st) {757 var a = { __proto__: null, b: 'c' };758 759 st.deepEqual(qs.parse(a), { b: 'c' });760 var result = qs.parse({ a: a });761 st.equal('a' in result, true, 'result has "a" property');762 st.deepEqual(result.a, a);763 st.end();764 });765 766 t.test('parses dates correctly', function (st) {767 var now = new Date();768 st.deepEqual(qs.parse({ a: now }), { a: now });769 st.end();770 });771 772 t.test('parses regular expressions correctly', function (st) {773 var re = /^test$/;774 st.deepEqual(qs.parse({ a: re }), { a: re });775 st.end();776 });777 778 t.test('does not allow overwriting prototype properties', function (st) {779 st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});780 st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});781 782 st.deepEqual(783 qs.parse('toString', { allowPrototypes: false }),784 {},785 'bare "toString" results in {}'786 );787 788 st.end();789 });790 791 t.test('can allow overwriting prototype properties', function (st) {792 st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });793 st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });794 795 st.deepEqual(796 qs.parse('toString', { allowPrototypes: true }),797 { toString: '' },798 'bare "toString" results in { toString: "" }'799 );800 801 st.end();802 });803 804 t.test('does not crash when the global Object prototype is frozen', { skip: !hasPropertyDescriptors || !hasOverrideMistake }, function (st) {805 // We can't actually freeze the global Object prototype as that will interfere with other tests, and once an object is frozen, it806 // can't be unfrozen. Instead, we add a new non-writable property to simulate this.807 st.teardown(mockProperty(Object.prototype, 'frozenProp', { value: 'foo', nonWritable: true, nonEnumerable: true }));808 809 st['throws'](810 function () {811 var obj = {};812 obj.frozenProp = 'bar';813 },814 // node < 6 has a different error message815 /^TypeError: Cannot assign to read only property 'frozenProp' of (?:object '#<Object>'|#<Object>)/,816 'regular assignment of an inherited non-writable property throws'817 );818 819 var parsed;820 st.doesNotThrow(821 function () {822 parsed = qs.parse('frozenProp', { allowPrototypes: false });823 },824 'parsing a nonwritable Object.prototype property does not throw'825 );826 827 st.deepEqual(parsed, {}, 'bare "frozenProp" results in {}');828 829 st.end();830 });831 832 t.test('params starting with a closing bracket', function (st) {833 st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });834 st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });835 st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });836 st.end();837 });838 839 t.test('params starting with a starting bracket', function (st) {840 st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });841 st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });842 st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });843 st.end();844 });845 846 t.test('add keys to objects', function (st) {847 st.deepEqual(848 qs.parse('a[b]=c&a=d', { strictMerge: false }),849 { a: { b: 'c', d: true } },850 'can add keys to objects'851 );852 853 st.deepEqual(854 qs.parse('a[b]=c&a=toString', { strictMerge: false }),855 { a: { b: 'c' } },856 'can not overwrite prototype'857 );858 859 st.deepEqual(860 qs.parse('a[b]=c&a=toString', { strictMerge: false, allowPrototypes: true }),861 { a: { b: 'c', toString: true } },862 'can overwrite prototype with allowPrototypes true'863 );864 865 st.deepEqual(866 qs.parse('a[b]=c&a=toString', { strictMerge: false, plainObjects: true }),867 { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },868 'can overwrite prototype with plainObjects true'869 );870 871 st.end();872 });873 874 t.test('strictMerge wraps object and primitive into an array', function (st) {875 st.deepEqual(876 qs.parse('a[b]=c&a=d'),877 { a: [{ b: 'c' }, 'd'] },878 'object then primitive produces array'879 );880 881 st.deepEqual(882 qs.parse('a=d&a[b]=c'),883 { a: ['d', { b: 'c' }] },884 'primitive then object produces array'885 );886 887 st.deepEqual(888 qs.parse('a[b]=c&a=toString'),889 { a: [{ b: 'c' }, 'toString'] },890 'prototype-colliding value is preserved in array'891 );892 893 st.deepEqual(894 qs.parse('a[b]=c&a=toString', { plainObjects: true }),895 { __proto__: null, a: [{ __proto__: null, b: 'c' }, 'toString'] },896 'plainObjects preserved in array wrapping'897 );898 899 st.end();900 });901 902 t.test('dunder proto is ignored', function (st) {903 var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';904 var result = qs.parse(payload, { allowPrototypes: true });905 906 st.deepEqual(907 result,908 {909 categories: {910 length: '42'911 }912 },913 'silent [[Prototype]] payload'914 );915 916 var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });917 918 st.deepEqual(919 plainResult,920 {921 __proto__: null,922 categories: {923 __proto__: null,924 length: '42'925 }926 },927 'silent [[Prototype]] payload: plain objects'928 );929 930 var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });931 932 st.notOk(Array.isArray(query.categories), 'is not an array');933 st.notOk(query.categories instanceof Array, 'is not instanceof an array');934 st.deepEqual(query.categories, { some: { json: 'toInject' } });935 st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');936 937 st.deepEqual(938 qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),939 {940 foo: {941 bar: 'stuffs'942 }943 },944 'hidden values'945 );946 947 st.deepEqual(948 qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),949 {950 __proto__: null,951 foo: {952 __proto__: null,953 bar: 'stuffs'954 }955 },956 'hidden values: plain objects'957 );958 959 st.end();960 });961 962 t.test('can return null objects', { skip: !hasProto }, function (st) {963 var expected = {964 __proto__: null,965 a: {966 __proto__: null,967 b: 'c',968 hasOwnProperty: 'd'969 }970 };971 st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);972 st.deepEqual(qs.parse(null, { plainObjects: true }), { __proto__: null });973 var expectedArray = {974 __proto__: null,975 a: {976 __proto__: null,977 0: 'b',978 c: 'd'979 }980 };981 st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);982 st.end();983 });984 985 t.test('can parse with custom encoding', function (st) {986 st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {987 decoder: function (str) {988 var reg = /%([0-9A-F]{2})/ig;989 var result = [];990 var parts = reg.exec(str);991 while (parts) {992 result.push(parseInt(parts[1], 16));993 parts = reg.exec(str);994 }995 return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));996 }997 }), { 県: '大阪府' });998 st.end();999 });1000 1001 t.test('receives the default decoder as a second argument', function (st) {1002 st.plan(1);1003 qs.parse('a', {1004 decoder: function (str, defaultDecoder) {1005 st.equal(defaultDecoder, utils.decode);1006 }1007 });1008 st.end();1009 });1010 1011 t.test('throws error with wrong decoder', function (st) {1012 st['throws'](function () {1013 qs.parse({}, { decoder: 'string' });1014 }, new TypeError('Decoder has to be a function.'));1015 st.end();1016 });1017 1018 t.test('does not mutate the options argument', function (st) {1019 var options = {};1020 qs.parse('a[b]=true', options);1021 st.deepEqual(options, {});1022 st.end();1023 });1024 1025 t.test('throws if an invalid charset is specified', function (st) {1026 st['throws'](function () {1027 qs.parse('a=b', { charset: 'foobar' });1028 }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));1029 st.end();1030 });1031 1032 t.test('parses an iso-8859-1 string if asked to', function (st) {1033 st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });1034 st.end();1035 });1036 1037 var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';1038 var urlEncodedOSlashInUtf8 = '%C3%B8';1039 var urlEncodedNumCheckmark = '%26%2310003%3B';1040 var urlEncodedNumSmiley = '%26%239786%3B';1041 1042 t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {1043 st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });1044 st.end();1045 });1046 1047 t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {1048 st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });1049 st.end();1050 });1051 1052 t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {1053 st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });1054 st.end();1055 });1056 1057 t.test('ignores an utf8 sentinel with an unknown value', function (st) {1058 st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });1059 st.end();1060 });1061 1062 t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {1063 st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });1064 st.end();1065 });1066 1067 t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {1068 st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });1069 st.end();1070 });1071 1072 t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {1073 st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });1074 st.end();1075 });1076 1077 t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {1078 st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {1079 charset: 'iso-8859-1',1080 decoder: function (str, defaultDecoder, charset) {1081 return str ? defaultDecoder(str, defaultDecoder, charset) : null;1082 },1083 interpretNumericEntities: true1084 }), { foo: null, bar: '☺' });1085 st.end();1086 });1087 1088 t.test('handles a custom decoder returning `null`, with a string key of `null`', function (st) {1089 st.deepEqual(1090 qs.parse('null=1&ToNull=2', {1091 decoder: function (str, defaultDecoder, charset) {1092 return str === 'ToNull' ? null : defaultDecoder(str, defaultDecoder, charset);1093 }1094 }),1095 { 'null': '1' },1096 '"null" key is not overridden by `null` decoder result'1097 );1098 1099 st.end();1100 });1101 1102 t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {1103 st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '☺' });1104 st.end();1105 });1106 1107 t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {1108 st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '☺' });1109 st.end();1110 });1111 1112 t.test('interpretNumericEntities with comma:true and iso charset does not crash', function (st) {1113 st.deepEqual(1114 qs.parse('b&a[]=1,' + urlEncodedNumSmiley, { comma: true, charset: 'iso-8859-1', interpretNumericEntities: true }),1115 { b: '', a: ['1,☺'] }1116 );1117 1118 st.end();1119 });1120 1121 t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {1122 st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });1123 st.end();1124 });1125 1126 t.test('allows for decoding keys and values differently', function (st) {1127 var decoder = function (str, defaultDecoder, charset, type) {1128 if (type === 'key') {1129 return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();1130 }1131 if (type === 'value') {1132 return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();1133 }1134 throw 'this should never happen! type: ' + type;1135 };1136 1137 st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });1138 1139 var noopDecoder = function () { return 'x'; };1140 noopDecoder();1141 st['throws'](1142 function () { decoder('x', noopDecoder, 'utf-8', 'unknown'); },1143 'this should never happen! type: unknown',1144 'decoder throws for unexpected type'1145 );1146 1147 st.end();1148 });1149 1150 t.test('parameter limit tests', function (st) {1151 st.test('does not throw error when within parameter limit', function (sst) {1152 var result = qs.parse('a=1&b=2&c=3', { parameterLimit: 5, throwOnLimitExceeded: true });1153 sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses without errors');1154 sst.end();1155 });1156 1157 st.test('throws error when throwOnLimitExceeded is present but not boolean', function (sst) {1158 sst['throws'](1159 function () {1160 qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: 'true' });1161 },1162 new TypeError('`throwOnLimitExceeded` option must be a boolean'),1163 'throws error when throwOnLimitExceeded is present and not boolean'1164 );1165 sst.end();1166 });1167 1168 st.test('throws error when parameter limit exceeded', function (sst) {1169 sst['throws'](1170 function () {1171 qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: true });1172 },1173 new RangeError('Parameter limit exceeded. Only 3 parameters allowed.'),1174 'throws error when parameter limit is exceeded'1175 );1176 1177 sst['throws'](1178 function () {1179 qs.parse('a=1&b=2', { parameterLimit: 1, throwOnLimitExceeded: true });1180 },1181 new RangeError('Parameter limit exceeded. Only 1 parameter allowed.'),1182 'throws error with singular "parameter" when parameterLimit is 1'1183 );1184 sst.end();1185 });1186 1187 st.test('silently truncates when throwOnLimitExceeded is not given', function (sst) {1188 var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3 });1189 sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');1190 sst.end();1191 });1192 1193 st.test('silently truncates when parameter limit exceeded without error', function (sst) {1194 var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3, throwOnLimitExceeded: false });1195 sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');1196 sst.end();1197 });1198 1199 st.test('allows unlimited parameters when parameterLimit set to Infinity', function (sst) {1200 var result = qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: Infinity });