Skip to content

Commit

Permalink
fix: fix parsing of relative URLs
Browse files Browse the repository at this point in the history
2.0.9 introduced a new URL parser based on WHATWG URL API. However, if
the request.url is relative (which is the case), parsing fails:
nodejs/node#12682
  • Loading branch information
nicokaiser committed Mar 3, 2021
1 parent 4090bae commit 740d59e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/url-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const Request = require('./request');
*/
function httpToFloraRequest(httpRequest) {
return new Promise((resolve, reject) => {
const parsedUrl = new URL(httpRequest.url);
const parsedUrl = new URL(httpRequest.url, 'http://localhost');
const matches = parsedUrl.pathname.match(/^\/(.+)\/([^/.]*)(?:\.([a-z]+))?$/);
if (!matches) {
resolve(null);
Expand Down
10 changes: 10 additions & 0 deletions test/url-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ describe('HTTP request parsing', () => {
.catch(done);
});

it('should parse relative urls', (done) => {
httpRequest.url = '/';
parseRequest(httpRequest)
.then((request) => {
expect(request).to.be.null;
done();
})
.catch(done);
});

describe('flat resources', () => {
it('should parse resource', (done) => {
httpRequest.url = 'http://api.example.com/user/';
Expand Down

0 comments on commit 740d59e

Please sign in to comment.