Skip to content

Commit 5c657e7

Browse files
authored
Fix missing return after reject (node-fetch#1022)
The bad Location header is escaped by the URL module, so any "corrupted" headers become valid or otherwise cause errors earlier in processing
1 parent 1f4f85e commit 5c657e7

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,7 @@ export default async function fetch(url, options_) {
116116
case 'manual':
117117
// Node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
118118
if (locationURL !== null) {
119-
// Handle corrupted header
120-
try {
121-
headers.set('Location', locationURL);
122-
/* c8 ignore next 3 */
123-
} catch (error) {
124-
reject(error);
125-
}
119+
headers.set('Location', locationURL);
126120
}
127121

128122
break;

test/main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,18 @@ describe('node-fetch', () => {
456456
});
457457
});
458458

459+
it('should support redirect mode, manual flag, broken Location header', () => {
460+
const url = `${base}redirect/bad-location`;
461+
const options = {
462+
redirect: 'manual'
463+
};
464+
return fetch(url, options).then(res => {
465+
expect(res.url).to.equal(url);
466+
expect(res.status).to.equal(301);
467+
expect(res.headers.get('location')).to.equal(`${base}redirect/%C3%A2%C2%98%C2%83`);
468+
});
469+
});
470+
459471
it('should support redirect mode, error flag', () => {
460472
const url = `${base}redirect/301`;
461473
const options = {

test/utils/server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ export default class TestServer {
288288
res.end();
289289
}
290290

291+
if (p === '/redirect/bad-location') {
292+
res.socket.write('HTTP/1.1 301\r\nLocation: ☃\r\nContent-Length: 0\r\n');
293+
res.socket.end('\r\n');
294+
}
295+
291296
if (p === '/error/400') {
292297
res.statusCode = 400;
293298
res.setHeader('Content-Type', 'text/plain');

0 commit comments

Comments
 (0)