Skip to content

Commit 8d6a680

Browse files
committedJul 1, 2021
Set options.url even if some options are invalid
Fixes #1753
1 parent 891dcbe commit 8d6a680

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed
 

‎source/core/options.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -776,20 +776,25 @@ export default class Options {
776776

777777
try {
778778
if (is.plainObject(input)) {
779-
this.merge(input);
780-
this.merge(options);
781-
this.url = input.url;
779+
try {
780+
this.merge(input);
781+
this.merge(options);
782+
} finally {
783+
this.url = input.url;
784+
}
782785
} else {
783-
this.merge(options);
784-
785-
if (options?.url !== undefined) {
786-
if (input === undefined) {
787-
this.url = options.url;
788-
} else {
789-
throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
786+
try {
787+
this.merge(options);
788+
} finally {
789+
if (options?.url !== undefined) {
790+
if (input === undefined) {
791+
this.url = options.url;
792+
} else {
793+
throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
794+
}
795+
} else if (input !== undefined) {
796+
this.url = input;
790797
}
791-
} else if (input !== undefined) {
792-
this.url = input;
793798
}
794799
}
795800
} catch (error) {

‎test/arguments.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {parse, URL, URLSearchParams} from 'url';
22
import test from 'ava';
33
import {Handler} from 'express';
44
import pEvent from 'p-event';
5-
import got, {Options, StrictOptions} from '../source/index.js';
5+
import got, {Options, RequestError, StrictOptions} from '../source/index.js';
66
import withServer, {withBodyParsingServer} from './helpers/with-server.js';
77
import invalidUrl from './helpers/invalid-url.js';
88

@@ -600,3 +600,28 @@ test('throws on too large noise', t => {
600600

601601
/* eslint-enable no-new */
602602
});
603+
604+
test('options have url even if some are invalid', async t => {
605+
const error = await t.throwsAsync<RequestError>(got('https://example.com', {
606+
// @ts-expect-error
607+
invalid: true
608+
}));
609+
610+
t.is((error.options.url as URL).href, 'https://example.com/');
611+
});
612+
613+
test('options have url even if some are invalid - got.extend', async t => {
614+
const instance = got.extend({
615+
handlers: [
616+
(options, next) => {
617+
t.is((options.url as URL).href, 'https://example.com/');
618+
return next(options);
619+
}
620+
]
621+
});
622+
623+
await t.throwsAsync(instance('https://example.com', {
624+
// @ts-expect-error
625+
invalid: true
626+
}));
627+
});

0 commit comments

Comments
 (0)
Please sign in to comment.