parse.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* eslint no-extend-native:0 */
  2. // Load modules
  3. var Code = require('code');
  4. var Lab = require('lab');
  5. var Qs = require('../');
  6. // Declare internals
  7. var internals = {};
  8. // Test shortcuts
  9. var lab = exports.lab = Lab.script();
  10. var expect = Code.expect;
  11. var describe = lab.experiment;
  12. var it = lab.test;
  13. describe('parse()', function () {
  14. it('parses a simple string', function (done) {
  15. expect(Qs.parse('0=foo')).to.deep.equal({ '0': 'foo' });
  16. expect(Qs.parse('foo=c++')).to.deep.equal({ foo: 'c ' });
  17. expect(Qs.parse('a[>=]=23')).to.deep.equal({ a: { '>=': '23' } });
  18. expect(Qs.parse('a[<=>]==23')).to.deep.equal({ a: { '<=>': '=23' } });
  19. expect(Qs.parse('a[==]=23')).to.deep.equal({ a: { '==': '23' } });
  20. expect(Qs.parse('foo', { strictNullHandling: true })).to.deep.equal({ foo: null });
  21. expect(Qs.parse('foo' )).to.deep.equal({ foo: '' });
  22. expect(Qs.parse('foo=')).to.deep.equal({ foo: '' });
  23. expect(Qs.parse('foo=bar')).to.deep.equal({ foo: 'bar' });
  24. expect(Qs.parse(' foo = bar = baz ')).to.deep.equal({ ' foo ': ' bar = baz ' });
  25. expect(Qs.parse('foo=bar=baz')).to.deep.equal({ foo: 'bar=baz' });
  26. expect(Qs.parse('foo=bar&bar=baz')).to.deep.equal({ foo: 'bar', bar: 'baz' });
  27. expect(Qs.parse('foo2=bar2&baz2=')).to.deep.equal({ foo2: 'bar2', baz2: '' });
  28. expect(Qs.parse('foo=bar&baz', { strictNullHandling: true })).to.deep.equal({ foo: 'bar', baz: null });
  29. expect(Qs.parse('foo=bar&baz')).to.deep.equal({ foo: 'bar', baz: '' });
  30. expect(Qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World')).to.deep.equal({
  31. cht: 'p3',
  32. chd: 't:60,40',
  33. chs: '250x100',
  34. chl: 'Hello|World'
  35. });
  36. done();
  37. });
  38. it('allows disabling dot notation', function (done) {
  39. expect(Qs.parse('a.b=c')).to.deep.equal({ a: { b: 'c' } });
  40. expect(Qs.parse('a.b=c', { allowDots: false })).to.deep.equal({ 'a.b': 'c' });
  41. done();
  42. });
  43. it('parses a single nested string', function (done) {
  44. expect(Qs.parse('a[b]=c')).to.deep.equal({ a: { b: 'c' } });
  45. done();
  46. });
  47. it('parses a double nested string', function (done) {
  48. expect(Qs.parse('a[b][c]=d')).to.deep.equal({ a: { b: { c: 'd' } } });
  49. done();
  50. });
  51. it('defaults to a depth of 5', function (done) {
  52. expect(Qs.parse('a[b][c][d][e][f][g][h]=i')).to.deep.equal({ a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } });
  53. done();
  54. });
  55. it('only parses one level when depth = 1', function (done) {
  56. expect(Qs.parse('a[b][c]=d', { depth: 1 })).to.deep.equal({ a: { b: { '[c]': 'd' } } });
  57. expect(Qs.parse('a[b][c][d]=e', { depth: 1 })).to.deep.equal({ a: { b: { '[c][d]': 'e' } } });
  58. done();
  59. });
  60. it('parses a simple array', function (done) {
  61. expect(Qs.parse('a=b&a=c')).to.deep.equal({ a: ['b', 'c'] });
  62. done();
  63. });
  64. it('parses an explicit array', function (done) {
  65. expect(Qs.parse('a[]=b')).to.deep.equal({ a: ['b'] });
  66. expect(Qs.parse('a[]=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] });
  67. expect(Qs.parse('a[]=b&a[]=c&a[]=d')).to.deep.equal({ a: ['b', 'c', 'd'] });
  68. done();
  69. });
  70. it('parses a mix of simple and explicit arrays', function (done) {
  71. expect(Qs.parse('a=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] });
  72. expect(Qs.parse('a[]=b&a=c')).to.deep.equal({ a: ['b', 'c'] });
  73. expect(Qs.parse('a[0]=b&a=c')).to.deep.equal({ a: ['b', 'c'] });
  74. expect(Qs.parse('a=b&a[0]=c')).to.deep.equal({ a: ['b', 'c'] });
  75. expect(Qs.parse('a[1]=b&a=c')).to.deep.equal({ a: ['b', 'c'] });
  76. expect(Qs.parse('a=b&a[1]=c')).to.deep.equal({ a: ['b', 'c'] });
  77. done();
  78. });
  79. it('parses a nested array', function (done) {
  80. expect(Qs.parse('a[b][]=c&a[b][]=d')).to.deep.equal({ a: { b: ['c', 'd'] } });
  81. expect(Qs.parse('a[>=]=25')).to.deep.equal({ a: { '>=': '25' } });
  82. done();
  83. });
  84. it('allows to specify array indices', function (done) {
  85. expect(Qs.parse('a[1]=c&a[0]=b&a[2]=d')).to.deep.equal({ a: ['b', 'c', 'd'] });
  86. expect(Qs.parse('a[1]=c&a[0]=b')).to.deep.equal({ a: ['b', 'c'] });
  87. expect(Qs.parse('a[1]=c')).to.deep.equal({ a: ['c'] });
  88. done();
  89. });
  90. it('limits specific array indices to 20', function (done) {
  91. expect(Qs.parse('a[20]=a')).to.deep.equal({ a: ['a'] });
  92. expect(Qs.parse('a[21]=a')).to.deep.equal({ a: { '21': 'a' } });
  93. done();
  94. });
  95. it('supports keys that begin with a number', function (done) {
  96. expect(Qs.parse('a[12b]=c')).to.deep.equal({ a: { '12b': 'c' } });
  97. done();
  98. });
  99. it('supports encoded = signs', function (done) {
  100. expect(Qs.parse('he%3Dllo=th%3Dere')).to.deep.equal({ 'he=llo': 'th=ere' });
  101. done();
  102. });
  103. it('is ok with url encoded strings', function (done) {
  104. expect(Qs.parse('a[b%20c]=d')).to.deep.equal({ a: { 'b c': 'd' } });
  105. expect(Qs.parse('a[b]=c%20d')).to.deep.equal({ a: { b: 'c d' } });
  106. done();
  107. });
  108. it('allows brackets in the value', function (done) {
  109. expect(Qs.parse('pets=["tobi"]')).to.deep.equal({ pets: '["tobi"]' });
  110. expect(Qs.parse('operators=[">=", "<="]')).to.deep.equal({ operators: '[">=", "<="]' });
  111. done();
  112. });
  113. it('allows empty values', function (done) {
  114. expect(Qs.parse('')).to.deep.equal({});
  115. expect(Qs.parse(null)).to.deep.equal({});
  116. expect(Qs.parse(undefined)).to.deep.equal({});
  117. done();
  118. });
  119. it('transforms arrays to objects', function (done) {
  120. expect(Qs.parse('foo[0]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } });
  121. expect(Qs.parse('foo[bad]=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } });
  122. expect(Qs.parse('foo[bad]=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } });
  123. expect(Qs.parse('foo[]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } });
  124. expect(Qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
  125. expect(Qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb')).to.deep.equal({ foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
  126. expect(Qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c')).to.deep.equal({ a: { '0': 'b', t: 'u', c: true } });
  127. expect(Qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y')).to.deep.equal({ a: { '0': 'b', '1': 'c', x: 'y' } });
  128. done();
  129. });
  130. it('transforms arrays to objects (dot notation)', function (done) {
  131. expect(Qs.parse('foo[0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
  132. expect(Qs.parse('foo[0].baz=bar&fool.bad.boo=baz')).to.deep.equal({ foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
  133. expect(Qs.parse('foo[0][0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
  134. expect(Qs.parse('foo[0].baz[0]=15&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15'], bar: '2' }] });
  135. expect(Qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15', '16'], bar: '2' }] });
  136. expect(Qs.parse('foo.bad=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } });
  137. expect(Qs.parse('foo.bad=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } });
  138. expect(Qs.parse('foo[]=bar&foo.bad=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } });
  139. expect(Qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
  140. expect(Qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb')).to.deep.equal({ foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
  141. done();
  142. });
  143. it('can add keys to objects', function (done) {
  144. expect(Qs.parse('a[b]=c&a=d')).to.deep.equal({ a: { b: 'c', d: true } });
  145. done();
  146. });
  147. it('correctly prunes undefined values when converting an array to an object', function (done) {
  148. expect(Qs.parse('a[2]=b&a[99999999]=c')).to.deep.equal({ a: { '2': 'b', '99999999': 'c' } });
  149. done();
  150. });
  151. it('supports malformed uri characters', function (done) {
  152. expect(Qs.parse('{%:%}', { strictNullHandling: true })).to.deep.equal({ '{%:%}': null });
  153. expect(Qs.parse('{%:%}=')).to.deep.equal({ '{%:%}': '' });
  154. expect(Qs.parse('foo=%:%}')).to.deep.equal({ foo: '%:%}' });
  155. done();
  156. });
  157. it('doesn\'t produce empty keys', function (done) {
  158. expect(Qs.parse('_r=1&')).to.deep.equal({ '_r': '1' });
  159. done();
  160. });
  161. it('cannot access Object prototype', function (done) {
  162. Qs.parse('constructor[prototype][bad]=bad');
  163. Qs.parse('bad[constructor][prototype][bad]=bad');
  164. expect(typeof Object.prototype.bad).to.equal('undefined');
  165. done();
  166. });
  167. it('parses arrays of objects', function (done) {
  168. expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] });
  169. expect(Qs.parse('a[0][b]=c')).to.deep.equal({ a: [{ b: 'c' }] });
  170. done();
  171. });
  172. it('allows for empty strings in arrays', function (done) {
  173. expect(Qs.parse('a[]=b&a[]=&a[]=c')).to.deep.equal({ a: ['b', '', 'c'] });
  174. expect(Qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true })).to.deep.equal({ a: ['b', null, 'c', ''] });
  175. expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true })).to.deep.equal({ a: ['b', '', 'c', null] });
  176. expect(Qs.parse('a[]=&a[]=b&a[]=c')).to.deep.equal({ a: ['', 'b', 'c'] });
  177. done();
  178. });
  179. it('compacts sparse arrays', function (done) {
  180. expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] });
  181. done();
  182. });
  183. it('parses semi-parsed strings', function (done) {
  184. expect(Qs.parse({ 'a[b]': 'c' })).to.deep.equal({ a: { b: 'c' } });
  185. expect(Qs.parse({ 'a[b]': 'c', 'a[d]': 'e' })).to.deep.equal({ a: { b: 'c', d: 'e' } });
  186. done();
  187. });
  188. it('parses buffers correctly', function (done) {
  189. var b = new Buffer('test');
  190. expect(Qs.parse({ a: b })).to.deep.equal({ a: b });
  191. done();
  192. });
  193. it('continues parsing when no parent is found', function (done) {
  194. expect(Qs.parse('[]=&a=b')).to.deep.equal({ '0': '', a: 'b' });
  195. expect(Qs.parse('[]&a=b', { strictNullHandling: true })).to.deep.equal({ '0': null, a: 'b' });
  196. expect(Qs.parse('[foo]=bar')).to.deep.equal({ foo: 'bar' });
  197. done();
  198. });
  199. it('does not error when parsing a very long array', function (done) {
  200. var str = 'a[]=a';
  201. while (Buffer.byteLength(str) < 128 * 1024) {
  202. str += '&' + str;
  203. }
  204. expect(function () {
  205. Qs.parse(str);
  206. }).to.not.throw();
  207. done();
  208. });
  209. it('should not throw when a native prototype has an enumerable property', { parallel: false }, function (done) {
  210. Object.prototype.crash = '';
  211. Array.prototype.crash = '';
  212. expect(Qs.parse.bind(null, 'a=b')).to.not.throw();
  213. expect(Qs.parse('a=b')).to.deep.equal({ a: 'b' });
  214. expect(Qs.parse.bind(null, 'a[][b]=c')).to.not.throw();
  215. expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] });
  216. delete Object.prototype.crash;
  217. delete Array.prototype.crash;
  218. done();
  219. });
  220. it('parses a string with an alternative string delimiter', function (done) {
  221. expect(Qs.parse('a=b;c=d', { delimiter: ';' })).to.deep.equal({ a: 'b', c: 'd' });
  222. done();
  223. });
  224. it('parses a string with an alternative RegExp delimiter', function (done) {
  225. expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' });
  226. done();
  227. });
  228. it('does not use non-splittable objects as delimiters', function (done) {
  229. expect(Qs.parse('a=b&c=d', { delimiter: true })).to.deep.equal({ a: 'b', c: 'd' });
  230. done();
  231. });
  232. it('allows overriding parameter limit', function (done) {
  233. expect(Qs.parse('a=b&c=d', { parameterLimit: 1 })).to.deep.equal({ a: 'b' });
  234. done();
  235. });
  236. it('allows setting the parameter limit to Infinity', function (done) {
  237. expect(Qs.parse('a=b&c=d', { parameterLimit: Infinity })).to.deep.equal({ a: 'b', c: 'd' });
  238. done();
  239. });
  240. it('allows overriding array limit', function (done) {
  241. expect(Qs.parse('a[0]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '0': 'b' } });
  242. expect(Qs.parse('a[-1]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '-1': 'b' } });
  243. expect(Qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 })).to.deep.equal({ a: { '0': 'b', '1': 'c' } });
  244. done();
  245. });
  246. it('allows disabling array parsing', function (done) {
  247. expect(Qs.parse('a[0]=b&a[1]=c', { parseArrays: false })).to.deep.equal({ a: { '0': 'b', '1': 'c' } });
  248. done();
  249. });
  250. it('parses an object', function (done) {
  251. var input = {
  252. 'user[name]': { 'pop[bob]': 3 },
  253. 'user[email]': null
  254. };
  255. var expected = {
  256. 'user': {
  257. 'name': { 'pop[bob]': 3 },
  258. 'email': null
  259. }
  260. };
  261. var result = Qs.parse(input);
  262. expect(result).to.deep.equal(expected);
  263. done();
  264. });
  265. it('parses an object in dot notation', function (done) {
  266. var input = {
  267. 'user.name': { 'pop[bob]': 3 },
  268. 'user.email.': null
  269. };
  270. var expected = {
  271. 'user': {
  272. 'name': { 'pop[bob]': 3 },
  273. 'email': null
  274. }
  275. };
  276. var result = Qs.parse(input);
  277. expect(result).to.deep.equal(expected);
  278. done();
  279. });
  280. it('parses an object and not child values', function (done) {
  281. var input = {
  282. 'user[name]': { 'pop[bob]': { 'test': 3 } },
  283. 'user[email]': null
  284. };
  285. var expected = {
  286. 'user': {
  287. 'name': { 'pop[bob]': { 'test': 3 } },
  288. 'email': null
  289. }
  290. };
  291. var result = Qs.parse(input);
  292. expect(result).to.deep.equal(expected);
  293. done();
  294. });
  295. it('does not blow up when Buffer global is missing', function (done) {
  296. var tempBuffer = global.Buffer;
  297. delete global.Buffer;
  298. var result = Qs.parse('a=b&c=d');
  299. global.Buffer = tempBuffer;
  300. expect(result).to.deep.equal({ a: 'b', c: 'd' });
  301. done();
  302. });
  303. it('does not crash when parsing circular references', function (done) {
  304. var a = {};
  305. a.b = a;
  306. var parsed;
  307. expect(function () {
  308. parsed = Qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
  309. }).to.not.throw();
  310. expect(parsed).to.contain('foo');
  311. expect(parsed.foo).to.contain('bar', 'baz');
  312. expect(parsed.foo.bar).to.equal('baz');
  313. expect(parsed.foo.baz).to.deep.equal(a);
  314. done();
  315. });
  316. it('parses plain objects correctly', function (done) {
  317. var a = Object.create(null);
  318. a.b = 'c';
  319. expect(Qs.parse(a)).to.deep.equal({ b: 'c' });
  320. var result = Qs.parse({ a: a });
  321. expect(result).to.contain('a');
  322. expect(result.a).to.deep.equal(a);
  323. done();
  324. });
  325. it('parses dates correctly', function (done) {
  326. var now = new Date();
  327. expect(Qs.parse({ a: now })).to.deep.equal({ a: now });
  328. done();
  329. });
  330. it('parses regular expressions correctly', function (done) {
  331. var re = /^test$/;
  332. expect(Qs.parse({ a: re })).to.deep.equal({ a: re });
  333. done();
  334. });
  335. it('can allow overwriting prototype properties', function (done) {
  336. expect(Qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true })).to.deep.equal({ a: { hasOwnProperty: 'b' } }, { prototype: false });
  337. expect(Qs.parse('hasOwnProperty=b', { allowPrototypes: true })).to.deep.equal({ hasOwnProperty: 'b' }, { prototype: false });
  338. done();
  339. });
  340. it('can return plain objects', function (done) {
  341. var expected = Object.create(null);
  342. expected.a = Object.create(null);
  343. expected.a.b = 'c';
  344. expected.a.hasOwnProperty = 'd';
  345. expect(Qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true })).to.deep.equal(expected);
  346. expect(Qs.parse(null, { plainObjects: true })).to.deep.equal(Object.create(null));
  347. var expectedArray = Object.create(null);
  348. expectedArray.a = Object.create(null);
  349. expectedArray.a['0'] = 'b';
  350. expectedArray.a.c = 'd';
  351. expect(Qs.parse('a[]=b&a[c]=d', { plainObjects: true })).to.deep.equal(expectedArray);
  352. done();
  353. });
  354. });