Skip to content

Commit

Permalink
Refactor around third request-specific param
Browse files Browse the repository at this point in the history
  • Loading branch information
davecardwell committed Apr 27, 2022
1 parent 1aa5e3c commit 9771127
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -63,15 +63,16 @@ const { http, https } = require('follow-redirects');

const options = url.parse('http://bit.ly/900913');
options.maxRedirects = 10;
options.beforeRedirect = (options, { headers, statusCode, requestMethod, requestUrl }) => {
options.beforeRedirect = (options, { headers, statusCode }, { method, url }) => {
// Use this to adjust the request options upon redirecting,
// to inspect the latest response headers,
// or to cancel the request by throwing an error

// headers = the redirect response headers
// statusCode = the redirect response code (eg. 301, 307, etc.)
// requestMethod = the request method that resulted in a redirect
// requestUrl = the requested URL that resulted in a redirect

// method = the request method that resulted in a redirect
// url = the requested URL that resulted in a redirect
if (options.hostname === "example.com") {
options.auth = "user:password";
}
Expand Down
15 changes: 11 additions & 4 deletions index.js
Expand Up @@ -366,7 +366,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
// care for methods not known to be safe, […]
// RFC7231§6.4.2–3: For historical reasons, a user agent MAY change
// the request method from POST to GET for the subsequent request.
var originalRequestMethod = this._options.method;
var method = this._options.method;
if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" ||
// RFC7231§6.4.4: The 303 (See Other) status code indicates that
// the server is redirecting the user agent to a different resource […]
Expand Down Expand Up @@ -418,11 +418,18 @@ RedirectableRequest.prototype._processResponse = function (response) {
var responseDetails = {
headers: response.headers,
statusCode: statusCode,
requestMethod: originalRequestMethod,
requestUrl: currentUrl,
};
var requestDetails = {
method: method,
url: currentUrl,
};
try {
this._options.beforeRedirect.call(null, this._options, responseDetails);
this._options.beforeRedirect.call(
null,
this._options,
responseDetails,
requestDetails
);
}
catch (err) {
this.emit("error", err);
Expand Down
8 changes: 4 additions & 4 deletions test/test.js
Expand Up @@ -1628,8 +1628,8 @@ describe("follow-redirects", function () {
port: 3600,
path: "/a",
method: "POST",
beforeRedirect: function (_, response) {
requestMethods.push(response.requestMethod);
beforeRedirect: function (_, __, request) {
requestMethods.push(request.method);
},
};
http.get(options, concatJson(resolve, reject)).on("error", reject);
Expand All @@ -1655,8 +1655,8 @@ describe("follow-redirects", function () {
port: 3600,
path: "/a",
method: "GET",
beforeRedirect: function (_, response) {
urlChain.push(response.requestUrl);
beforeRedirect: function (_, __, request) {
urlChain.push(request.url);
},
};
http.get(options, concatJson(resolve, reject)).on("error", reject);
Expand Down

0 comments on commit 9771127

Please sign in to comment.