forked from sindresorhus/query-string
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…hus#47. Fixes sindresorhus#47, Fixes sindresorhus#50
- Loading branch information
1 parent
d72fcc1
commit 7bebd8e
Showing
3 changed files
with
47 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,70 @@ | ||
import test from 'ava'; | ||
import fn from '../'; | ||
|
||
// https://github.com/sindresorhus/query-string/pull/48#issuecomment-198242935 | ||
function tsame(t, actual, expected) { | ||
Object.keys(expected).forEach(key => { | ||
t.is(actual[key], expected[key]); | ||
}); | ||
} | ||
|
||
test.beforeEach(t => { | ||
t.context.same = tsame.bind(undefined, t); | ||
}); | ||
|
||
test('query strings starting with a `?`', t => { | ||
t.same(fn.parse('?foo=bar'), {foo: 'bar'}); | ||
t.context.same(fn.parse('?foo=bar'), {foo: 'bar'}); | ||
}); | ||
|
||
test('query strings starting with a `#`', t => { | ||
t.same(fn.parse('#foo=bar'), {foo: 'bar'}); | ||
t.context.same(fn.parse('#foo=bar'), {foo: 'bar'}); | ||
}); | ||
|
||
test('query strings starting with a `&`', t => { | ||
t.same(fn.parse('&foo=bar&foo=baz'), {foo: ['bar', 'baz']}); | ||
t.context.same(fn.parse('&foo=bar&foo=baz'), {foo: ['bar', 'baz']}); | ||
}); | ||
|
||
test('parse a query string', t => { | ||
t.same(fn.parse('foo=bar'), {foo: 'bar'}); | ||
t.context.same(fn.parse('foo=bar'), {foo: 'bar'}); | ||
}); | ||
|
||
test('parse multiple query string', t => { | ||
t.same(fn.parse('foo=bar&key=val'), {foo: 'bar', key: 'val'}); | ||
t.context.same(fn.parse('foo=bar&key=val'), {foo: 'bar', key: 'val'}); | ||
}); | ||
|
||
test('parse query string without a value', t => { | ||
t.same(fn.parse('foo'), {foo: null}); | ||
t.same(fn.parse('foo&key'), {foo: null, key: null}); | ||
t.same(fn.parse('foo=bar&key'), {foo: 'bar', key: null}); | ||
t.context.same(fn.parse('foo'), {foo: null}); | ||
t.context.same(fn.parse('foo&key'), {foo: null, key: null}); | ||
t.context.same(fn.parse('foo=bar&key'), {foo: 'bar', key: null}); | ||
t.context.same(fn.parse('a&a'), {a: [null, null]}); | ||
t.context.same(fn.parse('a=&a'), {a: ['', null]}); | ||
}); | ||
|
||
test('return empty object if no qss can be found', t => { | ||
t.same(fn.parse('?'), {}); | ||
t.same(fn.parse('&'), {}); | ||
t.same(fn.parse('#'), {}); | ||
t.same(fn.parse(' '), {}); | ||
t.context.same(fn.parse('?'), {}); | ||
t.context.same(fn.parse('&'), {}); | ||
t.context.same(fn.parse('#'), {}); | ||
t.context.same(fn.parse(' '), {}); | ||
}); | ||
|
||
test('handle `+` correctly', t => { | ||
t.same(fn.parse('foo+faz=bar+baz++'), {'foo faz': 'bar baz '}); | ||
t.context.same(fn.parse('foo+faz=bar+baz++'), {'foo faz': 'bar baz '}); | ||
}); | ||
|
||
test('handle multiple of the same key', t => { | ||
t.same(fn.parse('foo=bar&foo=baz'), {foo: ['bar', 'baz']}); | ||
t.context.same(fn.parse('foo=bar&foo=baz'), {foo: ['bar', 'baz']}); | ||
}); | ||
|
||
test('query strings params including embedded `=`', t => { | ||
t.same(fn.parse('?param=http%3A%2F%2Fsomeurl%3Fid%3D2837'), {param: 'http://someurl?id=2837'}); | ||
t.context.same(fn.parse('?param=http%3A%2F%2Fsomeurl%3Fid%3D2837'), {param: 'http://someurl?id=2837'}); | ||
}); | ||
|
||
test('query strings params including raw `=`', t => { | ||
t.same(fn.parse('?param=http://someurl?id=2837'), {param: 'http://someurl?id=2837'}); | ||
t.context.same(fn.parse('?param=http://someurl?id=2837'), {param: 'http://someurl?id=2837'}); | ||
}); | ||
|
||
test('object properties', t => { | ||
t.notOk(fn.parse().prototype); | ||
t.context.same(fn.parse('hasOwnProperty=foo'), {hasOwnProperty: 'fo'}); | ||
t.context.same(fn.parse('__proto__=bar'), {__proto__: 'bar'}); | ||
}); |