Skip to content

Commit 45f6759

Browse files
committed
[Fix] fix for an impossible situation: when the formatter is called with 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.
1 parent f814a7f commit 45f6759

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/formats.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
return replace.call(value, percentTwenties, '+');
1111
},
1212
RFC3986: function (value) {
13-
return value;
13+
return String(value);
1414
}
1515
},
1616
RFC1738: 'RFC1738',

test/stringify.js

+9
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ test('stringify()', function (t) {
506506
return String.fromCharCode(buffer.readUInt8(0) + 97);
507507
}
508508
}), 'a=b');
509+
510+
st.equal(qs.stringify({ a: SaferBuffer.from('a b') }, {
511+
encoder: function (buffer) {
512+
return buffer;
513+
}
514+
}), 'a=a b');
509515
st.end();
510516
});
511517

@@ -546,17 +552,20 @@ test('stringify()', function (t) {
546552
t.test('RFC 1738 spaces serialization', function (st) {
547553
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
548554
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
555+
st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC1738 }), 'a+b=a+b');
549556
st.end();
550557
});
551558

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

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

0 commit comments

Comments
 (0)