node-append-query
node-append-query copied to clipboard
encodes incorrectly when query string has a `+` symbol
This is probably because we're passing in an un-encoded string as an input to querystring.parse()
function in line 13
Looks like querystring.parse()
function takes an encoded string, and in line 55 of the library, +
symbol is replaced with %20
. This makes sense because when encoding a space, it is sometimes encoded as +
or %20
.
However, in our case +
is the raw value and we wan't it to be treated as a +
and not a space
Expected behavior No matter how you pass the query parameters (object or string), it should always produce the same result
Actual behavior
Query parameters which includes +
symbol if passed in as string, gets encoded as %20
instead of %2B
test('should encode a url if encodeComponents is truthy when passed query as either object or string', function (t) {
t.plan(2)
var token = '0_h5E0i+IAmI8aZXOPEbgAWMiTA='
, expected = 'http://example.com/?token=' + encodeURIComponent(token)
// query object includes `+` symbol
var result1 = appendQuery('http://example.com/', { token: token } , { encodeComponents: true })
t.equal(result1, expected, 'should be equal')
// query string includes `+` symbol
var result2 = appendQuery('http://example.com/', 'token=' + token, { encodeComponents: true })
t.equal(result2, expected, 'should be equal')
})
Thanks for submitting this issue. I don't have time to come up with a fix right now, but if you send a PR I'd be happy to take a look and merge.