Skip to content

Commit

Permalink
Merge pull request #8 from zakkudo/makeCopyingUnsafeQueryStringStayUn…
Browse files Browse the repository at this point in the history
…safe

Make copying an unsafe query string stay unsafe
  • Loading branch information
zakkudo committed Sep 28, 2018
2 parents 4e6da70 + aa2b87d commit f73a1b4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ class QueryString {
* @param {String|Object|QueryString} [data] - Initial data. A url `String`
* will be parsed, and `Object`/`QueryString` instances will be copied.
* @param {Object} [options] - Modifiers for how the query string object is contructed
* @param {Boolean} [options.unsafe = false] - Disable url escaping of key/value pairs. Useful for servers that use unsafe characters as delimiters
* @param {Boolean} [options.unsafe = false] - Disable url escaping of
* key/value pairs. Useful for servers that use unsafe characters as delimiters
*/
constructor(data, options = {}) {
if (options.unsafe) {
if (options.unsafe || (data instanceof UnsafeQueryString && options.unsafe !== false)) {
return new UnsafeQueryString(data);
}

Expand Down
73 changes: 73 additions & 0 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,79 @@ describe('UnsafeQueryString', () => {
);
});

it('parsing a QueryString instance without explicitly setting unsafe', () => {
const original = new QueryString({
testString: 'test value',
testNumber: 1,
testBoolean: false,
testJSON: {'test': 'value'},
testUndefined: undefined,
testDuplicateKeys: ['test value 1', 'test value 2'],
testNull: null,
}, {unsafe: true});
const query = new QueryString(original);

original.monkeyPatch = 'test';

expect(String(query)).toEqual(
'?' +
'testString=test value&' +
'testNumber=1&' +
'testBoolean=false&' +
'testJSON={"test":"value"}&' +
'testDuplicateKeys=test value 1&' +
'testDuplicateKeys=test value 2&' +
'testNull=null'
);

expect(JSON.stringify(query)).toEqual(
'{' +
'"testString":"test value",' +
'"testNumber":1,' +
'"testBoolean":false,' +
'"testJSON":{"test":"value"},' +
'"testDuplicateKeys":["test value 1","test value 2"],' +
'"testNull":null' +
'}'
);
});

it('parsing a UnsafeQueryString can be coerced to safe', () => {
const original = new QueryString({
testString: 'test value',
testNumber: 1,
testBoolean: false,
testJSON: {'test': 'value'},
testUndefined: undefined,
testDuplicateKeys: ['test value 1', 'test value 2'],
testNull: null,
}, {unsafe: true});
const query = new QueryString(original, {unsafe: false});

original.monkeyPatch = 'test';

expect(String(query)).toEqual(
'?' +
'testString=test%20value&' +
'testNumber=1&' +
'testBoolean=false&' +
'testJSON=%7B%22test%22%3A%22value%22%7D&' +
'testDuplicateKeys=test%20value%201&' +
'testDuplicateKeys=test%20value%202&' +
'testNull=null'
);

expect(JSON.stringify(query)).toEqual(
'{' +
'"testString":"test value",' +
'"testNumber":1,' +
'"testBoolean":false,' +
'"testJSON":{"test":"value"},' +
'"testDuplicateKeys":["test value 1","test value 2"],' +
'"testNull":null' +
'}'
);
});

it('allows you to update the query string after construction', () => {
const query = new QueryString({
Expand Down

0 comments on commit f73a1b4

Please sign in to comment.