|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | var test = require('tape'); |
4 | | -var qs = require('../'); |
5 | | -var utils = require('../lib/utils'); |
| 4 | +var hasPropertyDescriptors = require('has-property-descriptors')(); |
6 | 5 | var iconv = require('iconv-lite'); |
| 6 | +var mockProperty = require('mock-property'); |
| 7 | +var hasOverrideMistake = require('has-override-mistake')(); |
7 | 8 | var SaferBuffer = require('safer-buffer').Buffer; |
8 | 9 |
|
| 10 | +var qs = require('../'); |
| 11 | +var utils = require('../lib/utils'); |
| 12 | + |
9 | 13 | test('parse()', function (t) { |
10 | 14 | t.test('parses a simple string', function (st) { |
11 | 15 | st.deepEqual(qs.parse('0=foo'), { 0: 'foo' }); |
@@ -601,6 +605,34 @@ test('parse()', function (t) { |
601 | 605 | st.end(); |
602 | 606 | }); |
603 | 607 |
|
| 608 | + t.test('does not crash when the global Object prototype is frozen', { skip: !hasPropertyDescriptors || !hasOverrideMistake }, function (st) { |
| 609 | + // We can't actually freeze the global Object prototype as that will interfere with other tests, and once an object is frozen, it |
| 610 | + // can't be unfrozen. Instead, we add a new non-writable property to simulate this. |
| 611 | + st.teardown(mockProperty(Object.prototype, 'frozenProp', { value: 'foo', nonWritable: true, nonEnumerable: true })); |
| 612 | + |
| 613 | + st['throws']( |
| 614 | + function () { |
| 615 | + var obj = {}; |
| 616 | + obj.frozenProp = 'bar'; |
| 617 | + }, |
| 618 | + // node < 6 has a different error message |
| 619 | + /^TypeError: Cannot assign to read only property 'frozenProp' of (?:object '#<Object>'|#<Object>)/, |
| 620 | + 'regular assignment of an inherited non-writable property throws' |
| 621 | + ); |
| 622 | + |
| 623 | + var parsed; |
| 624 | + st.doesNotThrow( |
| 625 | + function () { |
| 626 | + parsed = qs.parse('frozenProp', { allowPrototypes: false }); |
| 627 | + }, |
| 628 | + 'parsing a nonwritable Object.prototype property does not throw' |
| 629 | + ); |
| 630 | + |
| 631 | + st.deepEqual(parsed, {}, 'bare "frozenProp" results in {}'); |
| 632 | + |
| 633 | + st.end(); |
| 634 | + }); |
| 635 | + |
604 | 636 | t.test('params starting with a closing bracket', function (st) { |
605 | 637 | st.deepEqual(qs.parse(']=toString'), { ']': 'toString' }); |
606 | 638 | st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' }); |
|
0 commit comments