Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Apr 13, 2024
1 parent 4f1c408 commit a242ac6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
14 changes: 9 additions & 5 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ function parseURL (url) {
throw new InvalidArgumentError('Invalid URL: The URL argument must be a non-null object.')
}

if (!isHttpOrHttpsPrefixed(url.origin || url.protocol)) {
throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.')
}

if (!(url instanceof URL)) {
if (url.port != null && url.port !== '' && isValidPort(url.port) === false) {
throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.')
Expand All @@ -118,6 +114,10 @@ function parseURL (url) {
throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.')
}

if (!isHttpOrHttpsPrefixed(url.origin || url.protocol)) {
throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.')
}

const port = url.port != null
? url.port
: (url.protocol === 'https:' ? 443 : 80)
Expand All @@ -139,7 +139,11 @@ function parseURL (url) {
// From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL:
// If first parameter is a relative URL, second param is required, and will be used as the base URL.
// If first parameter is an absolute URL, a given second param will be ignored.
url = new URL(`${origin || ''}${path || ''}`)
return new URL(`${origin}${path}`)
}

if (!isHttpOrHttpsPrefixed(url.origin || url.protocol)) {
throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.')
}

return url
Expand Down
2 changes: 1 addition & 1 deletion test/node-test/client-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ test('invalid options throws', (t, done) => {
assert.ok(0)
} catch (err) {
assert.ok(err instanceof errors.InvalidArgumentError)
assert.strictEqual(err.message, 'Invalid URL protocol: the URL must start with `http:` or `https:`.')
assert.strictEqual(err.message, 'Invalid URL hostname: the hostname must be a string or null/undefined.')
}

try {
Expand Down
13 changes: 11 additions & 2 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,22 @@ describe('parseURL', () => {
parseURL({ protocol: 'http:', hostname: 'www.example.com', origin: undefined })
})
test('throws if origin is not as string', () => {
throws(() => parseURL({ protocol: 'http:', origin: 1 }), new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.'))
throws(() => parseURL({ protocol: 'http:', origin: 1 }), new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.'))
})
test('doesn\'t throw if origin is a string', () => {
parseURL({ protocol: 'http:', origin: 'https://www.example.com' })
})
test('removes trailing /', () => {
parseURL({ protocol: 'http:', origin: 'https://www.example.com/' })
strictEqual(parseURL({ protocol: 'http:', origin: 'https://www.example.com/' }).origin, 'https://www.example.com')
})
})

describe('protocol', () => {
test('throws if protocol is not http: or https: and no origin is defined', () => {
throws(() => parseURL({ protocol: 'wss:', hostname: 'www.example.com', path: '' }))
})
test('doesn\'t throw when origin is not provided', () => {
strictEqual(parseURL({ protocol: 'http:', hostname: 'www.example.com', path: '' }).origin, 'http://www.example.com')
})
})
})
Expand Down

0 comments on commit a242ac6

Please sign in to comment.