fix manual redirect mode

This commit is contained in:
David Frank 2016-04-13 02:58:04 +08:00
parent 1cd0a5c570
commit 311a8d5951
3 changed files with 30 additions and 2 deletions

View File

@ -5,6 +5,10 @@ Changelog
# 1.x release
## v1.5.1 (master)
- Fix: redirect mode `manual` should work even when there is no redirection or broken redirection
## v1.5.0 (master)
- Enhance: rejected promise now use custom `Error` (thx to @pekeler)

View File

@ -175,7 +175,7 @@ function Fetch(url, opts) {
}
// normalize location header for manual redirect mode
if (options.redirect === 'manual') {
if (options.redirect === 'manual' && headers.has('location')) {
headers.set('location', resolve_url(options.url, headers.get('location')));
}

View File

@ -304,7 +304,7 @@ describe('node-fetch', function() {
redirect: 'manual'
};
return fetch(url, opts).then(function(res) {
expect(res.url).to.equal(base + '/redirect/301');
expect(res.url).to.equal(url);
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal(base + '/inspect');
});
@ -320,6 +320,18 @@ describe('node-fetch', function() {
.and.have.property('type', 'no-redirect');
});
it('should support redirect mode, manual flag when there is no redirect', function() {
url = base + '/hello';
opts = {
redirect: 'manual'
};
return fetch(url, opts).then(function(res) {
expect(res.url).to.equal(url);
expect(res.status).to.equal(200);
expect(res.headers.get('location')).to.be.null;
});
});
it('should follow redirect code 301 and keep existing headers', function() {
url = base + '/redirect/301';
opts = {
@ -340,6 +352,18 @@ describe('node-fetch', function() {
.and.have.property('type', 'invalid-redirect');
});
it('should not reject broken redirect under manual redirect', function() {
url = base + '/error/redirect';
opts = {
redirect: 'manual'
};
return fetch(url, opts).then(function(res) {
expect(res.url).to.equal(url);
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.be.null;
});
});
it('should handle client-error response', function() {
url = base + '/error/400';
return fetch(url).then(function(res) {