diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 72d87368..3f1efd3f 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -201,7 +201,8 @@ export class HttpClient extends EventEmitter { // url maybe url.parse(url) object in urllib2 requestUrl = new URL(urlFormat(url)); } else { - requestUrl = url; + // or even if not, we clone to avoid mutating it + requestUrl = new URL(url.toString()); } } diff --git a/test/options.data.test.ts b/test/options.data.test.ts index 71b13e6d..6e7e65ef 100644 --- a/test/options.data.test.ts +++ b/test/options.data.test.ts @@ -43,6 +43,15 @@ describe('options.data.test.ts', () => { assert.equal(url.searchParams.get('data'), '哈哈'); }); + it('should not mutate a passed URL object when setting query string', async () => { + const url = new URL(_url); + assert.equal(url.searchParams.get('param1'), null); + await urllib.request(url, { + data: { param1: 'val1' }, + }); + assert.equal(url.searchParams.get('param1'), null); + }); + it('should GET with data work on nestedQuerystring=true', async () => { const response = await urllib.request(_url, { method: 'GET',