Skip to content

Commit

Permalink
feat: pass the request method to beforeRedirect
Browse files Browse the repository at this point in the history
  • Loading branch information
davecardwell committed Apr 26, 2022
1 parent f584099 commit 1aa5e3c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -63,12 +63,14 @@ const { http, https } = require('follow-redirects');

const options = url.parse('http://bit.ly/900913');
options.maxRedirects = 10;
options.beforeRedirect = (options, { headers, statusCode, requestUrl }) => {
options.beforeRedirect = (options, { headers, statusCode, requestMethod, requestUrl }) => {
// 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
if (options.hostname === "example.com") {
options.auth = "user:password";
Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -366,6 +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;
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 @@ -417,6 +418,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
var responseDetails = {
headers: response.headers,
statusCode: statusCode,
requestMethod: originalRequestMethod,
requestUrl: currentUrl,
};
try {
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Expand Up @@ -1613,6 +1613,34 @@ describe("follow-redirects", function () {
});
});

it("passes the request method to beforeRedirect", function () {
app.post("/a", redirectsTo("/b", 308));
app.post("/b", redirectsTo("/c", 301));
app.get("/c", redirectsTo("/d", 301));
app.get("/d", sendsJson({ a: "b" }));

const requestMethods = [];

return server.start(app)
.then(asPromise(function (resolve, reject) {
var options = {
host: "localhost",
port: 3600,
path: "/a",
method: "POST",
beforeRedirect: function (_, response) {
requestMethods.push(response.requestMethod);
},
};
http.get(options, concatJson(resolve, reject)).on("error", reject);
}))
.then(function (res) {
assert.deepEqual(res.responseUrl, "http://localhost:3600/d");
assert.deepEqual(res.parsedJson, { a: "b" });
assert.deepEqual(requestMethods, ["POST", "POST", "GET"]);
});
});

it("passes the original request URL to beforeRedirect", function () {
app.get("/a", redirectsTo("/b"));
app.get("/b", redirectsTo("/c"));
Expand Down

0 comments on commit 1aa5e3c

Please sign in to comment.