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.

406 lines
13 KiB

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