Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for redirect not preserving body with followOriginalHttpMethod #2608 #2645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,16 @@ Redirect.prototype.onResponse = function (response) {
if (response.statusCode !== 401 && response.statusCode !== 307) {
// Remove parameters from the previous response, unless this is the second request
// for a server that requires digest authentication.
delete request.body
delete request._form
if (!self.followOriginalHttpMethod) {
delete request.body
delete request._form
}
if (request.headers) {
request.removeHeader('host')
request.removeHeader('content-type')
request.removeHeader('content-length')
if (!self.followOriginalHttpMethod) {
request.removeHeader('content-type')
request.removeHeader('content-length')
}
if (request.uri.hostname !== request.originalHost.split(':')[0]) {
// Remove authorization if changing hostnames (but not if just
// changing ports or protocols). This matches the behavior of curl:
Expand Down
26 changes: 20 additions & 6 deletions tests/test-redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@ function createRedirectEndpoint (code, label, landing) {

function createLandingEndpoint (landing) {
s.on('/' + landing, function (req, res) {
// Make sure the cookie doesn't get included twice, see #139:
// Make sure cookies are set properly after redirect
assert.equal(req.headers.cookie, 'foo=bar; quux=baz; ham=eggs')
hits[landing] = true
res.writeHead(200, {'x-response': req.method.toUpperCase() + ' ' + landing})
res.end(req.method.toUpperCase() + ' ' + landing)
var body = ''
req.on('data', function (chunk) { body += chunk })
req.on('end', function () {
// Make sure the cookie doesn't get included twice, see #139:
// Make sure cookies are set properly after redirect
assert.equal(req.headers.cookie, 'foo=bar; quux=baz; ham=eggs')
hits[landing] = true
res.writeHead(200, {
'x-response': req.method.toUpperCase() + ' ' + landing,
'x-body': req.method.toUpperCase() + ' ' + body,
'x-content-type': req.method.toUpperCase() + ' ' + req.headers['content-type'],
'x-content-length': req.method.toUpperCase() + ' ' + req.headers['content-length']
})
res.end(req.method.toUpperCase() + ' ' + landing)
})
})
}

Expand Down Expand Up @@ -192,6 +201,8 @@ tape('should follow post redirects when followallredirects true and followOrigin
uri: s.url + '/temp',
followAllRedirects: true,
followOriginalHttpMethod: true,
body: {'test': true},
json: true,
jar: jar,
headers: { cookie: 'foo=bar' }
}, function (err, res, body) {
Expand All @@ -200,6 +211,9 @@ tape('should follow post redirects when followallredirects true and followOrigin
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.equal(res.headers['x-body'], 'POST {"test":true}', 'Got body')
t.equal(res.headers['x-content-type'], 'POST application/json', 'Got content type application/json')
t.equal(res.headers['x-content-length'], 'POST 13', 'Got content length')
t.end()
})
})
Expand Down