parse.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. 'use strict';
  2. var test = require('tape');
  3. var qs = require('../');
  4. var utils = require('../lib/utils');
  5. var iconv = require('iconv-lite');
  6. test('parse()', function (t) {
  7. t.test('parses a simple string', function (st) {
  8. st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
  9. st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
  10. st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
  11. st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
  12. st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
  13. st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
  14. st.deepEqual(qs.parse('foo'), { foo: '' });
  15. st.deepEqual(qs.parse('foo='), { foo: '' });
  16. st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
  17. st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
  18. st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
  19. st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
  20. st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
  21. st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
  22. st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
  23. st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
  24. cht: 'p3',
  25. chd: 't:60,40',
  26. chs: '250x100',
  27. chl: 'Hello|World'
  28. });
  29. st.end();
  30. });
  31. t.test('allows enabling dot notation', function (st) {
  32. st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
  33. st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
  34. st.end();
  35. });
  36. t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
  37. t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
  38. t.deepEqual(
  39. qs.parse('a[b][c][d][e][f][g][h]=i'),
  40. { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
  41. 'defaults to a depth of 5'
  42. );
  43. t.test('only parses one level when depth = 1', function (st) {
  44. st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
  45. st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
  46. st.end();
  47. });
  48. t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
  49. t.test('parses an explicit array', function (st) {
  50. st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
  51. st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
  52. st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
  53. st.end();
  54. });
  55. t.test('parses a mix of simple and explicit arrays', function (st) {
  56. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  57. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  58. st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
  59. st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
  60. st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  61. st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  62. st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
  63. st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
  64. st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
  65. st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
  66. st.end();
  67. });
  68. t.test('parses a nested array', function (st) {
  69. st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
  70. st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
  71. st.end();
  72. });
  73. t.test('allows to specify array indices', function (st) {
  74. st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
  75. st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
  76. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
  77. st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
  78. st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
  79. st.end();
  80. });
  81. t.test('limits specific array indices to arrayLimit', function (st) {
  82. st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
  83. st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
  84. st.end();
  85. });
  86. t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
  87. t.test('supports encoded = signs', function (st) {
  88. st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
  89. st.end();
  90. });
  91. t.test('is ok with url encoded strings', function (st) {
  92. st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
  93. st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
  94. st.end();
  95. });
  96. t.test('allows brackets in the value', function (st) {
  97. st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
  98. st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
  99. st.end();
  100. });
  101. t.test('allows empty values', function (st) {
  102. st.deepEqual(qs.parse(''), {});
  103. st.deepEqual(qs.parse(null), {});
  104. st.deepEqual(qs.parse(undefined), {});
  105. st.end();
  106. });
  107. t.test('transforms arrays to objects', function (st) {
  108. st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  109. st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  110. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
  111. st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
  112. st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  113. 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' }] });
  114. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
  115. st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
  116. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
  117. st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
  118. st.end();
  119. });
  120. t.test('transforms arrays to objects (dot notation)', function (st) {
  121. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
  122. st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
  123. st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
  124. st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
  125. 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' }] });
  126. st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  127. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
  128. st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
  129. st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
  130. 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' }] });
  131. st.end();
  132. });
  133. t.test('correctly prunes undefined values when converting an array to an object', function (st) {
  134. st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
  135. st.end();
  136. });
  137. t.test('supports malformed uri characters', function (st) {
  138. st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
  139. st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
  140. st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
  141. st.end();
  142. });
  143. t.test('doesn\'t produce empty keys', function (st) {
  144. st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
  145. st.end();
  146. });
  147. t.test('cannot access Object prototype', function (st) {
  148. qs.parse('constructor[prototype][bad]=bad');
  149. qs.parse('bad[constructor][prototype][bad]=bad');
  150. st.equal(typeof Object.prototype.bad, 'undefined');
  151. st.end();
  152. });
  153. t.test('parses arrays of objects', function (st) {
  154. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  155. st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
  156. st.end();
  157. });
  158. t.test('allows for empty strings in arrays', function (st) {
  159. st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
  160. st.deepEqual(
  161. qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
  162. { a: ['b', null, 'c', ''] },
  163. 'with arrayLimit 20 + array indices: null then empty string works'
  164. );
  165. st.deepEqual(
  166. qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
  167. { a: ['b', null, 'c', ''] },
  168. 'with arrayLimit 0 + array brackets: null then empty string works'
  169. );
  170. st.deepEqual(
  171. qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
  172. { a: ['b', '', 'c', null] },
  173. 'with arrayLimit 20 + array indices: empty string then null works'
  174. );
  175. st.deepEqual(
  176. qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
  177. { a: ['b', '', 'c', null] },
  178. 'with arrayLimit 0 + array brackets: empty string then null works'
  179. );
  180. st.deepEqual(
  181. qs.parse('a[]=&a[]=b&a[]=c'),
  182. { a: ['', 'b', 'c'] },
  183. 'array brackets: empty strings work'
  184. );
  185. st.end();
  186. });
  187. t.test('compacts sparse arrays', function (st) {
  188. st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
  189. st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
  190. st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
  191. st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
  192. st.end();
  193. });
  194. t.test('parses semi-parsed strings', function (st) {
  195. st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
  196. st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
  197. st.end();
  198. });
  199. t.test('parses buffers correctly', function (st) {
  200. var b = new Buffer('test');
  201. st.deepEqual(qs.parse({ a: b }), { a: b });
  202. st.end();
  203. });
  204. t.test('continues parsing when no parent is found', function (st) {
  205. st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
  206. st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
  207. st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
  208. st.end();
  209. });
  210. t.test('does not error when parsing a very long array', function (st) {
  211. var str = 'a[]=a';
  212. while (Buffer.byteLength(str) < 128 * 1024) {
  213. str = str + '&' + str;
  214. }
  215. st.doesNotThrow(function () {
  216. qs.parse(str);
  217. });
  218. st.end();
  219. });
  220. t.test('should not throw when a native prototype has an enumerable property', { parallel: false }, function (st) {
  221. Object.prototype.crash = '';
  222. Array.prototype.crash = '';
  223. st.doesNotThrow(qs.parse.bind(null, 'a=b'));
  224. st.deepEqual(qs.parse('a=b'), { a: 'b' });
  225. st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
  226. st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
  227. delete Object.prototype.crash;
  228. delete Array.prototype.crash;
  229. st.end();
  230. });
  231. t.test('parses a string with an alternative string delimiter', function (st) {
  232. st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
  233. st.end();
  234. });
  235. t.test('parses a string with an alternative RegExp delimiter', function (st) {
  236. st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
  237. st.end();
  238. });
  239. t.test('does not use non-splittable objects as delimiters', function (st) {
  240. st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
  241. st.end();
  242. });
  243. t.test('allows overriding parameter limit', function (st) {
  244. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
  245. st.end();
  246. });
  247. t.test('allows setting the parameter limit to Infinity', function (st) {
  248. st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
  249. st.end();
  250. });
  251. t.test('allows overriding array limit', function (st) {
  252. st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
  253. st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
  254. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
  255. st.end();
  256. });
  257. t.test('allows disabling array parsing', function (st) {
  258. st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { 0: 'b', 1: 'c' } });
  259. st.end();
  260. });
  261. t.test('allows for query string prefix', function (st) {
  262. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  263. st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
  264. st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
  265. st.end();
  266. });
  267. t.test('parses an object', function (st) {
  268. var input = {
  269. 'user[name]': { 'pop[bob]': 3 },
  270. 'user[email]': null
  271. };
  272. var expected = {
  273. user: {
  274. name: { 'pop[bob]': 3 },
  275. email: null
  276. }
  277. };
  278. var result = qs.parse(input);
  279. st.deepEqual(result, expected);
  280. st.end();
  281. });
  282. t.test('parses an object in dot notation', function (st) {
  283. var input = {
  284. 'user.name': { 'pop[bob]': 3 },
  285. 'user.email.': null
  286. };
  287. var expected = {
  288. user: {
  289. name: { 'pop[bob]': 3 },
  290. email: null
  291. }
  292. };
  293. var result = qs.parse(input, { allowDots: true });
  294. st.deepEqual(result, expected);
  295. st.end();
  296. });
  297. t.test('parses an object and not child values', function (st) {
  298. var input = {
  299. 'user[name]': { 'pop[bob]': { test: 3 } },
  300. 'user[email]': null
  301. };
  302. var expected = {
  303. user: {
  304. name: { 'pop[bob]': { test: 3 } },
  305. email: null
  306. }
  307. };
  308. var result = qs.parse(input);
  309. st.deepEqual(result, expected);
  310. st.end();
  311. });
  312. t.test('does not blow up when Buffer global is missing', function (st) {
  313. var tempBuffer = global.Buffer;
  314. delete global.Buffer;
  315. var result = qs.parse('a=b&c=d');
  316. global.Buffer = tempBuffer;
  317. st.deepEqual(result, { a: 'b', c: 'd' });
  318. st.end();
  319. });
  320. t.test('does not crash when parsing circular references', function (st) {
  321. var a = {};
  322. a.b = a;
  323. var parsed;
  324. st.doesNotThrow(function () {
  325. parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  326. });
  327. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  328. st.equal('bar' in parsed.foo, true);
  329. st.equal('baz' in parsed.foo, true);
  330. st.equal(parsed.foo.bar, 'baz');
  331. st.deepEqual(parsed.foo.baz, a);
  332. st.end();
  333. });
  334. t.test('does not crash when parsing deep objects', function (st) {
  335. var parsed;
  336. var str = 'foo';
  337. for (var i = 0; i < 5000; i++) {
  338. str += '[p]';
  339. }
  340. str += '=bar';
  341. st.doesNotThrow(function () {
  342. parsed = qs.parse(str, { depth: 5000 });
  343. });
  344. st.equal('foo' in parsed, true, 'parsed has "foo" property');
  345. var depth = 0;
  346. var ref = parsed.foo;
  347. while ((ref = ref.p)) {
  348. depth += 1;
  349. }
  350. st.equal(depth, 5000, 'parsed is 5000 properties deep');
  351. st.end();
  352. });
  353. t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
  354. var a = Object.create(null);
  355. a.b = 'c';
  356. st.deepEqual(qs.parse(a), { b: 'c' });
  357. var result = qs.parse({ a: a });
  358. st.equal('a' in result, true, 'result has "a" property');
  359. st.deepEqual(result.a, a);
  360. st.end();
  361. });
  362. t.test('parses dates correctly', function (st) {
  363. var now = new Date();
  364. st.deepEqual(qs.parse({ a: now }), { a: now });
  365. st.end();
  366. });
  367. t.test('parses regular expressions correctly', function (st) {
  368. var re = /^test$/;
  369. st.deepEqual(qs.parse({ a: re }), { a: re });
  370. st.end();
  371. });
  372. t.test('does not allow overwriting prototype properties', function (st) {
  373. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
  374. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
  375. st.deepEqual(
  376. qs.parse('toString', { allowPrototypes: false }),
  377. {},
  378. 'bare "toString" results in {}'
  379. );
  380. st.end();
  381. });
  382. t.test('can allow overwriting prototype properties', function (st) {
  383. st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
  384. st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
  385. st.deepEqual(
  386. qs.parse('toString', { allowPrototypes: true }),
  387. { toString: '' },
  388. 'bare "toString" results in { toString: "" }'
  389. );
  390. st.end();
  391. });
  392. t.test('params starting with a closing bracket', function (st) {
  393. st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
  394. st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
  395. st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
  396. st.end();
  397. });
  398. t.test('params starting with a starting bracket', function (st) {
  399. st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
  400. st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
  401. st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
  402. st.end();
  403. });
  404. t.test('add keys to objects', function (st) {
  405. st.deepEqual(
  406. qs.parse('a[b]=c&a=d'),
  407. { a: { b: 'c', d: true } },
  408. 'can add keys to objects'
  409. );
  410. st.deepEqual(
  411. qs.parse('a[b]=c&a=toString'),
  412. { a: { b: 'c' } },
  413. 'can not overwrite prototype'
  414. );
  415. st.deepEqual(
  416. qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
  417. { a: { b: 'c', toString: true } },
  418. 'can overwrite prototype with allowPrototypes true'
  419. );
  420. st.deepEqual(
  421. qs.parse('a[b]=c&a=toString', { plainObjects: true }),
  422. { a: { b: 'c', toString: true } },
  423. 'can overwrite prototype with plainObjects true'
  424. );
  425. st.end();
  426. });
  427. t.test('can return null objects', { skip: !Object.create }, function (st) {
  428. var expected = Object.create(null);
  429. expected.a = Object.create(null);
  430. expected.a.b = 'c';
  431. expected.a.hasOwnProperty = 'd';
  432. st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
  433. st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
  434. var expectedArray = Object.create(null);
  435. expectedArray.a = Object.create(null);
  436. expectedArray.a[0] = 'b';
  437. expectedArray.a.c = 'd';
  438. st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
  439. st.end();
  440. });
  441. t.test('can parse with custom encoding', function (st) {
  442. st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
  443. decoder: function (str) {
  444. var reg = /%([0-9A-F]{2})/ig;
  445. var result = [];
  446. var parts = reg.exec(str);
  447. while (parts) {
  448. result.push(parseInt(parts[1], 16));
  449. parts = reg.exec(str);
  450. }
  451. return iconv.decode(new Buffer(result), 'shift_jis').toString();
  452. }
  453. }), { 県: '大阪府' });
  454. st.end();
  455. });
  456. t.test('receives the default decoder as a second argument', function (st) {
  457. st.plan(1);
  458. qs.parse('a', {
  459. decoder: function (str, defaultDecoder) {
  460. st.equal(defaultDecoder, utils.decode);
  461. }
  462. });
  463. st.end();
  464. });
  465. t.test('throws error with wrong decoder', function (st) {
  466. st['throws'](function () {
  467. qs.parse({}, { decoder: 'string' });
  468. }, new TypeError('Decoder has to be a function.'));
  469. st.end();
  470. });
  471. t.test('does not mutate the options argument', function (st) {
  472. var options = {};
  473. qs.parse('a[b]=true', options);
  474. st.deepEqual(options, {});
  475. st.end();
  476. });
  477. t.end();
  478. });