diff --git a/lib/core/util.js b/lib/core/util.js index 259ba7b38a6..769811f57f7 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -58,31 +58,31 @@ function parseURL (url) { throw new InvalidArgumentError('Invalid URL: The URL argument must be a non-null object.') } - if (url.port != null && url.port !== '' && !Number.isFinite(parseInt(url.port))) { - throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.') + if (!/^https?:/.test(url.origin || url.protocol)) { + throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.') } - if (url.path != null && typeof url.path !== 'string') { - throw new InvalidArgumentError('Invalid URL path: the path must be a string or null/undefined.') - } + if (!(url instanceof URL)) { + if (url.port != null && url.port !== '' && !Number.isFinite(parseInt(url.port))) { + throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.') + } - if (url.pathname != null && typeof url.pathname !== 'string') { - throw new InvalidArgumentError('Invalid URL pathname: the pathname must be a string or null/undefined.') - } + if (url.path != null && typeof url.path !== 'string') { + throw new InvalidArgumentError('Invalid URL path: the path must be a string or null/undefined.') + } - if (url.hostname != null && typeof url.hostname !== 'string') { - throw new InvalidArgumentError('Invalid URL hostname: the hostname must be a string or null/undefined.') - } + if (url.pathname != null && typeof url.pathname !== 'string') { + throw new InvalidArgumentError('Invalid URL pathname: the pathname must be a string or null/undefined.') + } - if (url.origin != null && typeof url.origin !== 'string') { - throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.') - } + if (url.hostname != null && typeof url.hostname !== 'string') { + throw new InvalidArgumentError('Invalid URL hostname: the hostname must be a string or null/undefined.') + } - if (!/^https?:/.test(url.origin || url.protocol)) { - throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.') - } + if (url.origin != null && typeof url.origin !== 'string') { + throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.') + } - if (!(url instanceof URL)) { const port = url.port != null ? url.port : (url.protocol === 'https:' ? 443 : 80) diff --git a/lib/fetch/global.js b/lib/fetch/global.js index 42282acdfe2..1df6f1227bc 100644 --- a/lib/fetch/global.js +++ b/lib/fetch/global.js @@ -9,14 +9,6 @@ function getGlobalOrigin () { } function setGlobalOrigin (newOrigin) { - if ( - newOrigin !== undefined && - typeof newOrigin !== 'string' && - !(newOrigin instanceof URL) - ) { - throw new Error('Invalid base url') - } - if (newOrigin === undefined) { Object.defineProperty(globalThis, globalOrigin, { value: undefined, diff --git a/test/client-errors.js b/test/client-errors.js index 936841fbb15..cec7f37d62a 100644 --- a/test/client-errors.js +++ b/test/client-errors.js @@ -256,7 +256,7 @@ errorAndChunkedEncodingPipelining(consts.ASYNC_ITERATOR) test('invalid options throws', (t) => { try { - new Client({ port: 'foobar' }) // eslint-disable-line + new Client({ port: 'foobar', protocol: 'https:' }) // eslint-disable-line t.fail() } catch (err) { t.type(err, errors.InvalidArgumentError) @@ -374,7 +374,7 @@ test('invalid options throws', (t) => { t.fail() } catch (err) { t.type(err, errors.InvalidArgumentError) - t.equal(err.message, 'Invalid URL hostname: the hostname must be a string or null/undefined.') + t.equal(err.message, 'Invalid URL protocol: the URL must start with `http:` or `https:`.') } try {