Repository: sindresorhus/got (ts)

Issue:
How to mock "got"?
I'm wondering if there is an easy way to mock "got" dependency?
I managed to deal with it using `proxyquire` but looking for easier solution.

Pull request description:
Add documentation on how to test requests
Fixes #454.

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..c545fb2 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
 	"name": "got",
-	"version": "8.1.0",
+	"version": "8.2.0",
 	"description": "Simplified HTTP requests",
 	"license": "MIT",
 	"repository": "sindresorhus/got",
@@ -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
diff --git a/readme.md b/readme.md
index db36afa..4766481 100644
--- a/readme.md
+++ b/readme.md
@@ -569,6 +569,44 @@ request(`https://${config.host}/production/`, {
 ```
 
 
+## Testing
+
+You can test your requests by using the [`nock`](https://github.com/node-nock/nock) module to mock an endpoint:
+
+```js
+const got = require('got');
+const nock = require('nock');
+
+nock('https://sindresorhus.com')
+	.get('/')
+	.reply(200, 'Hello world!');
+
+(async () => {
+	const response = await got('sindresorhus.com');
+	console.log(response.body);
+	//=> 'Hello world!'
+})();
+```
+
+If you need real integration tests you can use [`create-test-server`](https://github.com/lukechilds/create-test-server):
+
+```js
+const got = require('got');
+const createTestServer = require('create-test-server');
+
+(async () => {
+	const server = await createTestServer();
+	server.get('/', 'Hello world!');
+
+	const response = await got(server.url);
+	console.log(response.body);
+	//=> 'Hello world!'
+
+	await server.close();
+})();
+```
+
+
 ## Tips
 
 ### User Agent


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'));
 });