Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle zero-length OK deflate responses #903

Merged
merged 1 commit into from Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/index.js
Expand Up @@ -254,6 +254,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 @@ -115,6 +115,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