Skip to content

Commit 539521a

Browse files
authored
fix: Migrate to WHATWG URL API from node's core url (#465)
2 parents ae1df31 + 0ac2eb8 commit 539521a

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/wrappers/request.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
const http = require('http');
22
const https = require('https');
3-
const urlUtil = require('url');
3+
const ArgumentError = require('../errors/ArgumentError');
44

55
module.exports.default = (options) => {
66
if (options.fetcher) {
77
return options.fetcher(options.uri);
88
}
99

1010
return new Promise((resolve, reject) => {
11-
const {
12-
hostname,
13-
path,
14-
port,
15-
protocol
16-
} = urlUtil.parse(options.uri);
11+
let url;
12+
try {
13+
url = new URL(options.uri);
14+
} catch (err) {
15+
throw new ArgumentError('Invalid JWKS URI: The provided URI is not a valid URL.');
16+
}
17+
const { hostname, port, protocol, pathname, search } = url;
18+
const path = pathname + search;
1719

1820
const requestOptions = {
1921
hostname,

tests/request.tests.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Request wrapper tests', () => {
4848
.reply(500, function () { this.req.response.statusMessage = errorMsg; });
4949

5050
request({ uri })
51-
.then(() => done('Shoul dhave thrown error'))
51+
.then(() => done('Should have thrown error'))
5252
.catch((err) => {
5353
expect(err.errorMsg).to.eql(errorMsg);
5454
done();
@@ -131,4 +131,14 @@ describe('Request wrapper tests', () => {
131131
.catch(done);
132132
});
133133
});
134+
135+
it('should reject with the ArgumentError message for malformed URI', (done) => {
136+
const badUrl = 'not-a-valid-url';
137+
request({ uri: badUrl })
138+
.then(() => done('Should have thrown error'))
139+
.catch((err) => {
140+
expect(err.message).to.equal('Invalid JWKS URI: The provided URI is not a valid URL.');
141+
done();
142+
});
143+
});
134144
});

0 commit comments

Comments
 (0)