Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pastes old source over tests
  • Loading branch information
dfadler committed Apr 28, 2014
commit d4d252528cedbdd60b8c7b701dea512002fd27b7
44 changes: 20 additions & 24 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ describe('qs.parse()', function(){
, chs: '250x100'
, chl: 'Hello|World'
});
});
})

it('should support encoded = signs', function(){
expect(qs.parse('he%3Dllo=th%3Dere'))
.to.eql({ 'he=llo': 'th=ere' });
});
})

it('should support nesting', function(){
expect(qs.parse('ops[>=]=25'))
Expand All @@ -67,7 +67,7 @@ describe('qs.parse()', function(){

expect(qs.parse('user[name][first]=tj&user[name][last]=holowaychuk'))
.to.eql({ user: { name: { first: 'tj', last: 'holowaychuk' }}});
});
})

it('should support array notation', function(){
expect(qs.parse('images[]'))
Expand All @@ -94,26 +94,22 @@ describe('qs.parse()', function(){
expect(qs.parse('user[name][first]=tj&user[name][first]=TJ'))
.to.eql({ user: { name: { first: ['tj', 'TJ'] }}});

var o = qs.parse('existing[fcbaebfecc][name][last]=tj');
expect(o).to.eql({ existing: { 'fcbaebfecc': { name: { last: 'tj' }}}});

if(Array.isArray) {
expect(Array.isArray(o.existing)).to.equal(false);
}

});
var o = qs.parse('existing[fcbaebfecc][name][last]=tj')
expect(o).to.eql({ existing: { 'fcbaebfecc': { name: { last: 'tj' }}}})
expect(Array.isArray(o.existing)).to.equal(false);
})

it('should support arrays with indexes', function(){
expect(qs.parse('foo[0]=bar&foo[1]=baz')).to.eql({ foo: ['bar', 'baz'] });
expect(qs.parse('foo[1]=bar&foo[0]=baz')).to.eql({ foo: ['baz', 'bar'] });
expect(qs.parse('foo[base64]=RAWR')).to.eql({ foo: { base64: 'RAWR' }});
expect(qs.parse('foo[64base]=RAWR')).to.eql({ foo: { '64base': 'RAWR' }});
});
})

it('should expand to an array when dupliate keys are present', function(){
expect(qs.parse('items=bar&items=baz&items=raz'))
.to.eql({ items: ['bar', 'baz', 'raz'] });
});
})

it('should support right-hand side brackets', function(){
expect(qs.parse('pets=["tobi"]'))
Expand All @@ -127,49 +123,49 @@ describe('qs.parse()', function(){

expect(qs.parse('op[>=]=[1,2,3]&op[=]=[[[[1]]]]'))
.to.eql({ op: { '>=': '[1,2,3]', '=': '[[[[1]]]]' }});
});
})

it('should support empty values', function(){
expect(qs.parse('')).to.eql({});
expect(qs.parse(undefined)).to.eql({});
expect(qs.parse(null)).to.eql({});
});
})

it('should transform arrays to objects', function(){
expect(qs.parse('foo[0]=bar&foo[bad]=baz')).to.eql({ foo: { 0: "bar", bad: "baz" }});
expect(qs.parse('foo[bad]=baz&foo[0]=bar')).to.eql({ foo: { 0: "bar", bad: "baz" }});
});
})

it('should support malformed uri chars', function(){
expect(qs.parse('{%:%}')).to.eql({ '{%:%}': '' });
expect(qs.parse('foo=%:%}')).to.eql({ 'foo': '%:%}' });
});
})

it('should support semi-parsed strings', function(){
expect(qs.parse({ 'user[name]': 'tobi' }))
.to.eql({ user: { name: 'tobi' }});

expect(qs.parse({ 'user[name]': 'tobi', 'user[email][main]': 'tobi@lb.com' }))
.to.eql({ user: { name: 'tobi', email: { main: 'tobi@lb.com' } }});
});
})

it('should not produce empty keys', function(){
expect(qs.parse('_r=1&'))
.to.eql({ _r: '1' });
});
.to.eql({ _r: '1' })
})

it('should not create big arrays of null objects', function(){
var q = qs.parse('a[999999999]=1&a[2]=2');
expect(q['a'].length).to.eql(2);
expect(q).to.eql({ a: ['2', '1'] });
});
})

it('should not be able to override prototypes', function(){
var obj = qs.parse('toString=bad&bad[toString]=bad&constructor=bad');
expect(obj.toString).to.be.a(Function);
expect(obj.bad.toString).to.be.a(Function);
expect(obj.constructor).to.be.a(Function);
});
})

it('should not be possible to access Object prototype', function() {
qs.parse('constructor[prototype][bad]=bad');
Expand All @@ -180,5 +176,5 @@ describe('qs.parse()', function(){
it('should not throw when a native prototype has an enumerable property', function() {
Object.prototype.crash = '';
expect(qs.parse.bind(null, 'test')).to.not.throwException();
});
});
})
})
5 changes: 2 additions & 3 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,9 @@ describe('qs.stringify', function(){
describe('others', function(){
it('should work with a date', function() {
var date = new Date(0);
var str = 'at=' + encodeURIComponent(date);
var str = 'at=' + encodeURIComponent(date)
var obj = { at: date };
expect(qs.stringify(obj)).to.eql(str);
});
});
});

});