From f63ebbc65572368a05609c93884c84b778ea1ffa Mon Sep 17 00:00:00 2001 From: Dave Cardwell Date: Wed, 27 Apr 2022 18:33:12 +0000 Subject: [PATCH] Add headers to request details --- README.md | 11 ++++++----- index.js | 8 +++++++- test/test.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b0b9fa9..eb869a6 100644 --- a/README.md +++ b/README.md @@ -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"; } diff --git a/index.js b/index.js index 67ec9b5..b983ca9 100644 --- a/index.js +++ b/index.js @@ -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( diff --git a/test/test.js b/test/test.js index ede3e02..ce98a5e 100644 --- a/test/test.js +++ b/test/test.js @@ -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" }));