Skip to content

Commit

Permalink
Add headers to request details
Browse files Browse the repository at this point in the history
  • Loading branch information
davecardwell committed Apr 27, 2022
1 parent 9771127 commit f63ebbc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -63,16 +63,17 @@ const { http, https } = require('follow-redirects');

const options = url.parse('http://bit.ly/900913');
options.maxRedirects = 10;
options.beforeRedirect = (options, { headers, statusCode }, { method, url }) => {
options.beforeRedirect = (options, response, request) => {
// 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.)
// response.headers = the redirect response headers
// response.statusCode = the redirect response code (eg. 301, 307, etc.)

// method = the request method that resulted in a redirect
// url = the requested URL that resulted in a redirect
// request.url = the requested URL that resulted in a redirect
// request.headers = the headers in the request that resulted in a redirect
// request.method = the method of the request that resulted in a redirect
if (options.hostname === "example.com") {
options.auth = "user:password";
}
Expand Down
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -415,13 +415,19 @@ RedirectableRequest.prototype._processResponse = function (response) {

// Evaluate the beforeRedirect callback
if (typeof this._options.beforeRedirect === "function") {
const requestHeaders = response.req.getRawHeaderNames().reduce((headers, header) => {
headers[header] = response.req.getHeader(header);
return headers;
}, {});

var responseDetails = {
headers: response.headers,
statusCode: statusCode,
};
var requestDetails = {
method: method,
url: currentUrl,
headers: requestHeaders,
method: method,
};
try {
this._options.beforeRedirect.call(
Expand Down
50 changes: 49 additions & 1 deletion test/test.js
Expand Up @@ -1641,7 +1641,55 @@ describe("follow-redirects", function () {
});
});

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

const headerChain = [];

return server.start(app)
.then(asPromise(function (resolve, reject) {
var options = {
host: "localhost",
port: 3600,
path: "/a",
method: "POST",
headers: {
"X-Foo": "bar",
},
beforeRedirect: function (optionz, __, request) {
optionz.headers["X-Redirect"] = `${request.url} => ${optionz.href}`;

headerChain.push(request.headers);
},
};
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(headerChain, [
{
"Host": "localhost:3600",
"X-Foo": "bar",
},
{
"Host": "localhost:3600",
"X-Foo": "bar",
"X-Redirect": "http://localhost:3600/a => http://localhost:3600/b",
},
{
"Host": "localhost:3600",
"X-Foo": "bar",
"X-Redirect": "http://localhost:3600/b => http://localhost:3600/c",
},
]);
});
});

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

0 comments on commit f63ebbc

Please sign in to comment.