Skip to content

Commit

Permalink
[Fix] fix for an impossible situation: when the formatter is called w…
Browse files Browse the repository at this point in the history
…ith a non-string value

Note that all these tests passed already. Since the only time a
formatter is called is in a context where it is concatenated with
another string using `+`, this is a redundant step. However, for
pedantic correctness and documentation, the contract for formatters is
to always return a string.
  • Loading branch information
ljharb committed Mar 29, 2019
1 parent f814a7f commit 45f6759
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
return replace.call(value, percentTwenties, '+');
},
RFC3986: function (value) {
return value;
return String(value);
}
},
RFC1738: 'RFC1738',
Expand Down
9 changes: 9 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ test('stringify()', function (t) {
return String.fromCharCode(buffer.readUInt8(0) + 97);
}
}), 'a=b');

st.equal(qs.stringify({ a: SaferBuffer.from('a b') }, {
encoder: function (buffer) {
return buffer;
}
}), 'a=a b');
st.end();
});

Expand Down Expand Up @@ -546,17 +552,20 @@ test('stringify()', function (t) {
t.test('RFC 1738 spaces serialization', function (st) {
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC1738 }), 'a+b=a+b');
st.end();
});

t.test('RFC 3986 spaces serialization', function (st) {
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC3986 }), 'a%20b=a%20b');
st.end();
});

t.test('Backward compatibility to RFC 3986', function (st) {
st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }), 'a%20b=a%20b');
st.end();
});

Expand Down

0 comments on commit 45f6759

Please sign in to comment.