Skip to content

Commit

Permalink
Throw invalid URL error on relative URLs.
Browse files Browse the repository at this point in the history
Aligns with Node behavior.
  • Loading branch information
RubenVerborgh committed Sep 13, 2022
1 parent e30137c commit 449e895
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
39 changes: 23 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ events.forEach(function (event) {
};
});

var InvalidUrlError = createErrorType(
"ERR_INVALID_URL",
"Invalid URL",
TypeError
);
// Error types with codes
var RedirectionError = createErrorType(
"ERR_FR_REDIRECTION_FAILURE",
Expand Down Expand Up @@ -409,7 +414,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
redirectUrl = url.resolve(currentUrl, location);
}
catch (cause) {
this.emit("error", new RedirectionError(cause));
this.emit("error", new RedirectionError({ cause: cause }));
return;
}

Expand Down Expand Up @@ -454,7 +459,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
this._performRequest();
}
catch (cause) {
this.emit("error", new RedirectionError(cause));
this.emit("error", new RedirectionError({ cause: cause }));
}
};

Expand All @@ -477,14 +482,18 @@ function wrap(protocols) {
function request(input, options, callback) {
// Parse parameters
if (isString(input)) {
var urlStr = input;
var parsed;
try {
input = urlToOptions(new URL(urlStr));
parsed = urlToOptions(new URL(input));
}
catch (err) {
/* istanbul ignore next */
input = url.parse(urlStr);
parsed = url.parse(input);
}
if (!isString(parsed.protocol)) {
throw new InvalidUrlError({ input });
}
input = parsed;
}
else if (URL && (input instanceof URL)) {
input = urlToOptions(input);
Expand Down Expand Up @@ -562,21 +571,19 @@ function removeMatchingHeaders(regex, headers) {
undefined : String(lastValue).trim();
}

function createErrorType(code, defaultMessage) {
function CustomError(cause) {
function createErrorType(code, message, baseClass) {
// Create constructor
function CustomError(properties) {
Error.captureStackTrace(this, this.constructor);
if (!cause) {
this.message = defaultMessage;
}
else {
this.message = defaultMessage + ": " + cause.message;
this.cause = cause;
}
Object.assign(this, properties || {});
this.code = code;
this.message = this.cause ? message + ": " + this.cause.message : message;
}
CustomError.prototype = new Error();

// Attach constructor and set default properties
CustomError.prototype = new (baseClass || Error)();
CustomError.prototype.constructor = CustomError;
CustomError.prototype.name = "Error [" + code + "]";
CustomError.prototype.code = code;
return CustomError;
}

Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ describe("follow-redirects", function () {
});
});

it("http.get with relative URL path", function () {
var error = null;
try {
http.get("/relative");
}
catch (e) {
error = e;
}
assert(error instanceof Error);
assert(error instanceof TypeError);
assert.equal(error.code, "ERR_INVALID_URL");
assert.equal(error.input, "/relative");
});

it("should return with the original status code if the response does not contain a location header", function () {
app.get("/a", function (req, res) {
res.status(307).end();
Expand Down

0 comments on commit 449e895

Please sign in to comment.