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 reusing Got options #1127

Closed
wants to merge 10 commits into from
Closed
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
9 changes: 9 additions & 0 deletions source/normalize-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,20 @@ export const normalizeArguments = (url: URLOrOptions, options?: Options, default
options = {};
}

const {url: optionsUrl} = options;

// @ts-ignore URL is not URL
options.url = url;

runInitHooks(defaults?.options.hooks.init, options);
runInitHooks(options.hooks?.init, options);

url = options.url as URLOrOptions;
if (optionsUrl) {
options.url = optionsUrl;
} else {
delete options.url;
}
} else if (Reflect.has(url as object, 'resolve')) {
throw new Error('The legacy `url.Url` is deprecated. Use `URL` instead.');
} else {
Expand Down
14 changes: 13 additions & 1 deletion test/normalize-arguments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import got from '../source';
import got, {Options} from '../source';
import {normalizeArguments} from '../source/normalize-arguments';

test('should merge options replacing responseType', t => {
const responseType = 'json';
Expand All @@ -9,3 +10,14 @@ test('should merge options replacing responseType', t => {

t.is(options.responseType, responseType);
});

test('should be able to reuse options', t => {
const options: Options = {};
normalizeArguments('http://localhost', options);
t.notThrows(() => normalizeArguments('http://localhost', options));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.notThrows(() => normalizeArguments('http://localhost', options));
t.notThrows(() => {
normalizeArguments('http://localhost', options);
});

});

test.failing('should handle frozen objects', t => {
const options: Options = Object.freeze({});
t.notThrows(() => normalizeArguments('http://localhost', options));
});