Skip to content

Commit 4c036ce

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 180bfa5 commit 4c036ce

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
@@ -469,6 +469,12 @@ test('stringify()', function (t) {
469469
return String.fromCharCode(buffer.readUInt8(0) + 97);
470470
}
471471
}), 'a=b');
472+
473+
st.equal(qs.stringify({ a: SaferBuffer.from('a b') }, {
474+
encoder: function (buffer) {
475+
return buffer;
476+
}
477+
}), 'a=a b');
472478
st.end();
473479
});
474480

@@ -509,17 +515,20 @@ test('stringify()', function (t) {
509515
t.test('RFC 1738 spaces serialization', function (st) {
510516
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
511517
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
518+
st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC1738 }), 'a+b=a+b');
512519
st.end();
513520
});
514521

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

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

0 commit comments

Comments
 (0)