add query string

This commit is contained in:
Luke Pulverenti 2016-03-14 16:00:18 -04:00
parent ccf6b48bdc
commit aa35c3ef4c
13 changed files with 349 additions and 0 deletions

View file

@ -0,0 +1,17 @@
import test from 'ava';
import fn from '../';
test('should extract query string from url', t => {
t.is(fn.extract('http://foo.bar/?abc=def&hij=klm'), 'abc=def&hij=klm');
t.is(fn.extract('http://foo.bar/?'), '');
});
test('should handle strings not containing query string', t => {
t.is(fn.extract('http://foo.bar/'), '');
t.is(fn.extract(''), '');
});
test('should throw for invalid values', t => {
t.throws(fn.extract.bind(fn, null), TypeError);
t.throws(fn.extract.bind(fn, undefined), TypeError);
});

View file

@ -0,0 +1,51 @@
import test from 'ava';
import fn from '../';
test('query strings starting with a `?`', t => {
t.same(fn.parse('?foo=bar'), {foo: 'bar'});
});
test('query strings starting with a `#`', t => {
t.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']});
});
test('parse a query string', t => {
t.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'});
});
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});
});
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(' '), {});
});
test('handle `+` correctly', t => {
t.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']});
});
test('query strings params including embedded `=`', t => {
t.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'});
});

View file

@ -0,0 +1,33 @@
import test from 'ava';
import fn from '../';
test('stringify', t => {
t.same(fn.stringify({foo: 'bar'}), 'foo=bar');
t.same(fn.stringify({foo: 'bar', bar: 'baz'}), 'bar=baz&foo=bar');
});
test('different types', t => {
t.same(fn.stringify(), '');
t.same(fn.stringify(0), '');
});
test('URI encode', t => {
t.same(fn.stringify({'foo bar': 'baz faz'}), 'foo%20bar=baz%20faz');
t.same(fn.stringify({'foo bar': 'baz\'faz'}), 'foo%20bar=baz%27faz');
});
test('handle array value', t => {
t.same(fn.stringify({abc: 'abc', foo: ['bar', 'baz']}), 'abc=abc&foo=bar&foo=baz');
});
test('handle empty array value', t => {
t.same(fn.stringify({abc: 'abc', foo: []}), 'abc=abc');
});
test('should not encode undefined values', t => {
t.same(fn.stringify({abc: undefined, foo: 'baz'}), 'foo=baz');
});
test('should encode null values as just a key', t => {
t.same(fn.stringify({xyz: null, abc: null, foo: 'baz'}), 'abc&foo=baz&xyz');
});