From 4cec245e4e1f7aac25c1a0a0aaa66ceaadf0b636 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 8 Sep 2023 00:14:06 -0700 Subject: [PATCH] [Fix] properly stringify a query object Fixes #65 --- package.json | 2 +- test/index.js | 10 ++++++++++ url.js | 6 +++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 25f874b..68ba8f1 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "defunctzombie", "dependencies": { "punycode": "^1.4.1", - "qs": "^6.11.0" + "qs": "^6.11.2" }, "main": "./url.js", "keywords": [ diff --git a/test/index.js b/test/index.js index 9eef8ed..5cd5725 100644 --- a/test/index.js +++ b/test/index.js @@ -25,6 +25,7 @@ var test = require('mocha').test; var assert = require('assert'); +var nodeURL = require('url'); var url = require('../url'); @@ -2027,6 +2028,15 @@ relativeTests.forEach(function (relativeTest) { }); }); +test('format with querystring', function () { + var obj = { protocol: 'https:', host: 'google.com', pathname: 'test', query: { message: ['value1', 'value2'] } }; + + var actual = url.format(obj); + var expected = nodeURL.format(obj); + + assert.equal(actual, expected, 'format(' + actual + ') == ' + expected + '\nactual:' + actual); +}); + /* * format: [to, from, result] * the test: ['.//g', 'f:/a', 'f://g'] is a fundamental problem diff --git a/url.js b/url.js index 44dd207..937dc2a 100644 --- a/url.js +++ b/url.js @@ -434,7 +434,11 @@ Url.prototype.format = function () { } if (this.query && typeof this.query === 'object' && Object.keys(this.query).length) { - query = querystring.stringify(this.query); + query = querystring.stringify(this.query, { + arrayFormat: 'repeat', + encodeValuesOnly: true, + addQueryPrefix: false + }); } var search = this.search || (query && ('?' + query)) || '';