From d9b8d4882dfcc7386fc9390419c34d5847aedb14 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Tue, 25 Oct 2016 10:13:42 +0200 Subject: [PATCH] Add followOriginalHttpMethod to redirect to original HTTP method closes #2118 - added test - updated readme --- README.md | 1 + lib/redirect.js | 6 +++++- tests/test-redirect.js | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6eaaa0547..a0b6c84d0 100644 --- a/README.md +++ b/README.md @@ -762,6 +762,7 @@ The first argument can be either a `url` or an `options` object. The only requir - `followRedirect` - follow HTTP 3xx responses as redirects (default: `true`). This property can also be implemented as function which gets `response` object as a single argument and should return `true` if redirects should continue or `false` otherwise. - `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects (default: `false`) +- `followOriginalHttpMethod` - by default we redirect to HTTP method GET. you can enable this property to redirect to the original HTTP method (default: `false`) - `maxRedirects` - the maximum number of redirects to follow (default: `10`) - `removeRefererHeader` - removes the referer header when a redirect happens (default: `false`). **Note:** if true, referer header set in the initial request is preserved during redirect chain. diff --git a/lib/redirect.js b/lib/redirect.js index 040dfe0e0..f8604491f 100644 --- a/lib/redirect.js +++ b/lib/redirect.js @@ -8,6 +8,7 @@ function Redirect (request) { this.followRedirect = true this.followRedirects = true this.followAllRedirects = false + this.followOriginalHttpMethod = false this.allowRedirect = function () {return true} this.maxRedirects = 10 this.redirects = [] @@ -36,6 +37,9 @@ Redirect.prototype.onRequest = function (options) { if (options.removeRefererHeader !== undefined) { self.removeRefererHeader = options.removeRefererHeader } + if (options.followOriginalHttpMethod !== undefined) { + self.followOriginalHttpMethod = options.followOriginalHttpMethod + } } Redirect.prototype.redirectTo = function (response) { @@ -115,7 +119,7 @@ Redirect.prototype.onResponse = function (response) { ) if (self.followAllRedirects && request.method !== 'HEAD' && response.statusCode !== 401 && response.statusCode !== 307) { - request.method = 'GET' + request.method = self.followOriginalHttpMethod ? request.method : 'GET' } // request.method = 'GET' // Force all redirects to use GET || commented out fixes #215 delete request.src diff --git a/tests/test-redirect.js b/tests/test-redirect.js index 1f49182fb..decfd2602 100644 --- a/tests/test-redirect.js +++ b/tests/test-redirect.js @@ -186,6 +186,24 @@ tape('should follow post redirects when followallredirects true', function(t) { }) }) +tape('should follow post redirects when followallredirects true and followOriginalHttpMethod is enabled', function(t) { + hits = {} + request.post({ + uri: s.url + '/temp', + followAllRedirects: true, + followOriginalHttpMethod: true, + jar: jar, + headers: { cookie: 'foo=bar' } + }, function(err, res, body) { + t.equal(err, null) + t.equal(res.statusCode, 200) + t.ok(hits.temp, 'Original request is to /temp') + t.ok(hits.temp_landing, 'Forward to temporary landing URL') + t.equal(body, 'POST temp_landing', 'Got temporary landing content') + t.end() + }) +}) + tape('should not follow post redirects when followallredirects false', function(t) { hits = {} request.post({