diff --git a/CHANGELOG.md b/CHANGELOG.md index 48d8569..c2a31c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index e665fac..3a8f6f1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index ca8485f..1c0b47f 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/test.js b/test/test.js index cab2147..9ba66f5 100644 --- a/test/test.js +++ b/test/test.js @@ -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);