Skip to content

Commit

Permalink
Merge pull request #2435 from kirrg001/fix/add-option-to-follow-redir…
Browse files Browse the repository at this point in the history
…ect-with-http-method

Add followOriginalHttpMethod to redirect to original HTTP method
  • Loading branch information
simov committed Oct 29, 2016
2 parents 76d909f + d9b8d48 commit ec2c88d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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.

Expand Down
6 changes: 5 additions & 1 deletion lib/redirect.js
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/test-redirect.js
Expand Up @@ -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({
Expand Down

0 comments on commit ec2c88d

Please sign in to comment.