Skip to content

Commit

Permalink
Handle zero-length OK deflate responses (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmaciej committed Jan 16, 2022
1 parent 1ef4b56 commit 838d971
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.js
Expand Up @@ -275,6 +275,13 @@ export default function fetch(url, opts) {
response = new Response(body, response_options);
resolve(response);
});
raw.on('end', () => {
// some old IIS servers return zero-length OK deflate responses, so 'data' is never emitted.
if (!response) {
response = new Response(body, response_options);
resolve(response);
}
})
return;
}

Expand Down
7 changes: 7 additions & 0 deletions test/server.js
Expand Up @@ -120,6 +120,13 @@ export default class TestServer {
});
}

if (p === '/empty/deflate') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'deflate');
res.end();
}

if (p === '/sdch') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Expand Up @@ -679,6 +679,17 @@ describe('node-fetch', () => {
});
});

it('should handle empty deflate response', function() {
const url = `${base}empty/deflate`;
return fetch(url).then(res => {
expect(res.headers.get('content-type')).to.equal('text/plain');
return res.text().then(result => {
expect(result).to.be.a('string');
expect(result).to.be.empty;
});
});
});

it('should decompress brotli response', function() {
if(typeof zlib.createBrotliDecompress !== 'function') this.skip();
const url = `${base}brotli`;
Expand Down

0 comments on commit 838d971

Please sign in to comment.