From 9566d25019caae8c4f1a9097bf344238a583d014 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 29 Mar 2019 14:50:56 -0400 Subject: [PATCH] [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. --- lib/formats.js | 2 +- test/stringify.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/formats.js b/lib/formats.js index df459975..702da12e 100644 --- a/lib/formats.js +++ b/lib/formats.js @@ -10,7 +10,7 @@ module.exports = { return replace.call(value, percentTwenties, '+'); }, RFC3986: function (value) { - return value; + return String(value); } }, RFC1738: 'RFC1738', diff --git a/test/stringify.js b/test/stringify.js index 7076680a..ec4df056 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -469,6 +469,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(); }); @@ -509,17 +515,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(); });