Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stringify for bracket mode with arrays containing null #138

Merged
merged 1 commit into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function encoderForArrayFormat(options) {
};
case 'bracket':
return (key, value) => {
return value === null ? encode(key, options) : [
return value === null ? [encode(key, options), '[]'].join('') : [
encode(key, options),
'[]=',
encode(value, options)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
},
"devDependencies": {
"ava": "*",
"deep-equal": "^1.0.1",
"fast-check": "^0.0.13",
"xo": "*"
}
}
9 changes: 9 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ test('query strings having brackets arrays and format option as `bracket`', t =>
}), {foo: ['bar', 'baz']});
});

test('query strings having brackets arrays with null and format option as `bracket`', t => {
t.deepEqual(m.parse('bar[]&foo[]=a&foo[]&foo[]=', {
arrayFormat: 'bracket'
}), {
foo: ['a', null, ''],
bar: [null]
});
});

test('query strings having indexed arrays and format option as `index`', t => {
t.deepEqual(m.parse('foo[0]=bar&foo[1]=baz', {
arrayFormat: 'index'
Expand Down
33 changes: 33 additions & 0 deletions test/properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import deepEqual from 'deep-equal';
import * as fc from 'fast-check';
import test from 'ava';
import m from '..';

// Valid query parameters must follow:
// - key can be any unicode string (not empty)
// - value must be one of:
// --> any unicode string
// --> null
// --> array containing values defined above (at least two items)
const queryParamsArbitrary = fc.object({
key: fc.fullUnicodeString(1, 10),
values: [
fc.fullUnicodeString(),
fc.constant(null),
fc.array(fc.oneof(fc.fullUnicodeString(), fc.constant(null))).filter(a => a.length >= 2)
],
maxDepth: 0
});

const optionsArbitrary = fc.record({
arrayFormat: fc.constantFrom('bracket', 'index', 'none'),
strict: fc.boolean(),
encode: fc.boolean(),
sort: fc.boolean()
});

test('should read correctly from stringified query params', t => {
t.notThrows(() => fc.assert(
fc.property(queryParamsArbitrary, optionsArbitrary,
(obj, opts) => deepEqual(m.parse(m.stringify(obj, opts), opts), obj))));
});
9 changes: 9 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ test('array stringify representation with array brackets', t => {
}), 'bar[]=one&bar[]=two&foo');
});

test('array stringify representation with array brackets and null value', t => {
t.is(m.stringify({
foo: ['a', null, ''],
bar: [null]
}, {
arrayFormat: 'bracket'
}), 'bar[]&foo[]=a&foo[]&foo[]=');
});

test('array stringify representation with a bad array format', t => {
t.is(m.stringify({
foo: null,
Expand Down