You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
13 KiB

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