Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't mutate passed URL object #470

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/options.data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down