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 #555 #901

Merged
merged 1 commit into from May 17, 2014
Merged
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
4 changes: 2 additions & 2 deletions request.js
Expand Up @@ -883,13 +883,13 @@ Request.prototype.onResponse = function (response) {
, redirectUri: redirectTo
}
)
if (self.followAllRedirects && response.statusCode != 401) self.method = 'GET'
if (self.followAllRedirects && response.statusCode != 401 && response.statusCode != 307) self.method = 'GET'
// self.method = 'GET' // Force all redirects to use GET || commented out fixes #215
delete self.src
delete self.req
delete self.agent
delete self._started
if (response.statusCode != 401) {
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 self.body
Expand Down
29 changes: 17 additions & 12 deletions tests/test-redirect.js
Expand Up @@ -21,6 +21,7 @@ s.listen(s.port, function () {
bouncer(301, 'temp')
bouncer(302, 'perm')
bouncer(302, 'nope')
bouncer(307, 'fwd')

function bouncer(code, label) {
var landing = label+'_landing';
Expand All @@ -35,18 +36,12 @@ s.listen(s.port, function () {
})

s.on('/'+landing, function (req, res) {
if (req.method !== 'GET') { // We should only accept GET redirects
console.error("Got a non-GET request to the redirect destination URL");
res.writeHead(400);
res.end();
return;
}
// 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)
res.end(landing)
res.end(req.method.toUpperCase() + ' ' + landing)
})
}

Expand All @@ -58,7 +53,7 @@ s.listen(s.port, function () {
if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
assert.ok(hits.perm, 'Original request is to /perm')
assert.ok(hits.perm_landing, 'Forward to permanent landing URL')
assert.equal(body, 'perm_landing', 'Got permanent landing content')
assert.equal(body, 'GET perm_landing', 'Got permanent landing content')
passed += 1
done()
})
Expand All @@ -69,7 +64,7 @@ s.listen(s.port, function () {
if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
assert.ok(hits.temp, 'Original request is to /temp')
assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
assert.equal(body, 'temp_landing', 'Got temporary landing content')
assert.equal(body, 'GET temp_landing', 'Got temporary landing content')
passed += 1
done()
})
Expand Down Expand Up @@ -102,7 +97,7 @@ s.listen(s.port, function () {
if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
assert.ok(hits.temp, 'Original request is to /temp')
assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
assert.equal(body, 'temp_landing', 'Got temporary landing content')
assert.equal(body, 'GET temp_landing', 'Got temporary landing content')
passed += 1
done()
})
Expand Down Expand Up @@ -145,15 +140,25 @@ s.listen(s.port, function () {
if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
assert.ok(hits.temp, 'Original request is to /temp')
assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
assert.equal(body, 'temp_landing', 'Got temporary landing content')
assert.equal(body, 'GET temp_landing', 'Got temporary landing content')
passed += 1
done()
})

request.del(server+'/fwd', {followAllRedirects:true, jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
if (er) throw er
if (res.statusCode !== 200) throw new Error('Status is not 200: '+res.statusCode)
assert.ok(hits.fwd, 'Original request is to /fwd')
assert.ok(hits.fwd_landing, 'Forward to temporary landing URL')
assert.equal(body, 'DELETE fwd_landing', 'Got temporary landing content')
passed += 1
done()
})

var reqs_done = 0;
function done() {
reqs_done += 1;
if(reqs_done == 9) {
if(reqs_done == 10) {
console.log(passed + ' tests passed.')
s.close()
}
Expand Down