Skip to content

Commit

Permalink
compress flag should not overwrite accept encoding header
Browse files Browse the repository at this point in the history
  • Loading branch information
David Frank committed Nov 5, 2018
1 parent 91e1c0b commit 5494e19
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,7 +7,9 @@ Changelog

## master

- Fix: `compress` flag shouldn't overwrite existing `Accept-Encoding` header.
- Fix: multiple `import` rules, where `PassThrough` etc. doesn't have a named export when using node <10 and `--exerimental-modules` flag.
- Other: Better README.

## v2.2.0

Expand Down
3 changes: 2 additions & 1 deletion src/request.js
Expand Up @@ -192,9 +192,10 @@ export function getNodeRequestOptions(request) {
}

// HTTP-network-or-cache fetch step 2.15
if (request.compress) {
if (request.compress && !headers.has('Accept-Encoding')) {
headers.set('Accept-Encoding', 'gzip,deflate');
}

if (!headers.has('Connection') && !request.agent) {
headers.set('Connection', 'close');
}
Expand Down
19 changes: 16 additions & 3 deletions test/test.js
Expand Up @@ -731,6 +731,19 @@ describe('node-fetch', () => {
});
});

it('should not overwrite existing accept-encoding header when auto decompression is true', function() {
const url = `${base}inspect`;
const opts = {
compress: true,
headers: {
'Accept-Encoding': 'gzip'
}
};
return fetch(url, opts).then(res => res.json()).then(res => {
expect(res.headers['accept-encoding']).to.equal('gzip');
});
});

it('should allow custom timeout', function() {
this.timeout(500);
const url = `${base}timeout`;
Expand Down Expand Up @@ -782,7 +795,7 @@ describe('node-fetch', () => {

it('should set default User-Agent', function () {
const url = `${base}inspect`;
fetch(url).then(res => res.json()).then(res => {
return fetch(url).then(res => res.json()).then(res => {
expect(res.headers['user-agent']).to.startWith('node-fetch/');
});
});
Expand All @@ -794,7 +807,7 @@ describe('node-fetch', () => {
'user-agent': 'faked'
}
};
fetch(url, opts).then(res => res.json()).then(res => {
return fetch(url, opts).then(res => res.json()).then(res => {
expect(res.headers['user-agent']).to.equal('faked');
});
});
Expand All @@ -813,7 +826,7 @@ describe('node-fetch', () => {
'accept': 'application/json'
}
};
fetch(url, opts).then(res => res.json()).then(res => {
return fetch(url, opts).then(res => res.json()).then(res => {
expect(res.headers.accept).to.equal('application/json');
});
});
Expand Down

0 comments on commit 5494e19

Please sign in to comment.