fix follow=0 scenario

This commit is contained in:
David Frank 2015-03-20 00:22:23 +08:00
parent 281037c795
commit a8bb5650ad
4 changed files with 11 additions and 3 deletions

View File

@ -7,6 +7,7 @@ Changelog
## v1.0.5 (master)
- Fix: when follow = 0, fetch should not follow redirect
- Enhance: update tests for better coverage
- Enhance: code formatting
- Enhance: clean up doc

View File

@ -140,7 +140,7 @@ default values are shown, note that only `method`, `headers` and `body` are allo
{
method: 'GET'
, headers: {} // request header, format {a:1} or {b:[1,2,3]}
, follow: 20 // maximum redirect count, 0 to disable, 1 to not follow redirect
, follow: 20 // maximum redirect count, 0 to not follow redirect
, timeout: 0 // request timeout in ms, 0 to disable, timeout reset on redirect
, compress: true // support gzip/deflate content encoding, false to disable
, size: 0 // maximum response body size in bytes, 0 to disable

View File

@ -70,7 +70,7 @@ function Fetch(url, opts) {
, auth: uri.auth
, method: opts.method || 'GET'
, headers: opts.headers || {}
, follow: opts.follow || 20
, follow: opts.follow !== undefined ? opts.follow : 20
, counter: opts.counter || 0
, timeout: opts.timeout || 0
, compress: opts.compress !== false

View File

@ -150,7 +150,6 @@ describe('node-fetch', function() {
});
});
it('should follow redirect code 302', function() {
url = base + '/redirect/302';
return fetch(url).then(function(res) {
@ -199,6 +198,14 @@ describe('node-fetch', function() {
return expect(fetch(url, opts)).to.eventually.be.rejectedWith(Error);
});
it('should allow not following redirect', function() {
url = base + '/redirect/301';
opts = {
follow: 0
}
return expect(fetch(url, opts)).to.eventually.be.rejectedWith(Error);
});
it('should reject broken redirect', function() {
url = base + '/error/redirect';
return expect(fetch(url)).to.eventually.be.rejectedWith(Error);