Repository: sindresorhus/got (ts)

Issue:
Problem with default `Accept-Encoding` header
Why Got adds a default `Accept-Encoding` header ([this line](https://github.com/sindresorhus/got/blob/master/index.js#L475)) even with disabled `decompress` option?

**My scenario:** In my current project I wanted to forward some client request headers from one server to another and header values must be equal in both servers, but when `Accept-Encoding` header is not set, Got adds this header with a default value and that is a problem in this scenario.

I can't find a way to send a request with no `accept-encoding` header,  I had to set an empty string in `accept-encoding` to get around this:

```js
app.use(ctx => {
  const headers = ctx.headers; // client request headers

  got(url, {
    headers: {
      ...headers,
      // Prevent got from adding default accept-encoding header
      'accept-encoding': headers['accept-encoding'] || ''
    }
  })
})
```
`null` and `undefined` not working because in v8.0.0 it will be deleted and then default header will be added.

**My suggestion:** Only set default `accept-encoding` when `decompress` option is enabled, then the following code will work for my scenario:

```js
got(url, {
  decompress: false,
  headers
})
```

I can send a PR, but first I wanted to ask that is there any specific reason to set default `accept-encoding` other than `decompress` option?

Pull request description:
Don't set `accept-encoding` if decompress is false
Fixes #419.

Let me know if you want to move this somewhere else or use a different style 👍 . I originally wanted to put this inline under `user-agent` but then if disabled the headers object would still have an `accept-encoding` instead of none at all.

Patch:
diff --git a/index.js b/index.js
index 037fb7a..0d9eb0b 100644
--- a/index.js
+++ b/index.js
@@ -485,10 +485,13 @@ function normalizeArguments(url, opts) {
 	}
 
 	opts.headers = Object.assign({
-		'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`,
-		'accept-encoding': 'gzip,deflate'
+		'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`
 	}, headers);
 
+	if (opts.decompress) {
+		opts.headers['accept-encoding'] = 'gzip,deflate';
+	}
+
 	const query = opts.query;
 
 	if (query) {
diff --git a/package.json b/package.json
index 5975886..31d1ac0 100644
--- a/package.json
+++ b/package.json
@@ -70,7 +70,7 @@
 		"url-to-options": "^1.0.1"
 	},
 	"devDependencies": {
-		"ava": "^0.24.0",
+		"ava": "^0.25.0",
 		"coveralls": "^3.0.0",
 		"form-data": "^2.1.1",
 		"get-port": "^3.0.0",
@@ -82,7 +82,7 @@
 		"tempfile": "^2.0.0",
 		"tempy": "^0.2.1",
 		"universal-url": "1.0.0-alpha",
-		"xo": "^0.18.0"
+		"xo": "^0.20.0"
 	},
 	"ava": {
 		"concurrency": 4


Test patch:
diff --git a/test/headers.js b/test/headers.js
index 07fa774..ba07cf5 100644
--- a/test/headers.js
+++ b/test/headers.js
@@ -27,6 +27,12 @@ test('accept-encoding', async t => {
 	t.is(headers['accept-encoding'], 'gzip,deflate');
 });
 
+test('do not set accept-encoding header when decompress options is false', async t => {
+	const headers = (await got(s.url, {json: true, decompress: false})).body;
+	// TODO: Use `Reflect.has()` when we target Node.js 6
+	t.false(Object.prototype.hasOwnProperty.call(headers, 'accept-encoding'));
+});
+
 test('accept header with json option', async t => {
 	let headers = (await got(s.url, {json: true})).body;
 	t.is(headers.accept, 'application/json');
@@ -95,6 +101,7 @@ test('remove null value headers', async t => {
 			unicorns: null
 		}
 	})).body;
+	// TODO: Use `Reflect.has()` when we target Node.js 6
 	t.false(Object.prototype.hasOwnProperty.call(headers, 'unicorns'));
 });
 
@@ -104,6 +111,7 @@ test('remove undefined value headers', async t => {
 			unicorns: undefined
 		}
 	})).body;
+	// TODO: Use `Reflect.has()` when we target Node.js 6
 	t.false(Object.prototype.hasOwnProperty.call(headers, 'unicorns'));
 });