From 4d92eb6bef37637c0d85986faae314e17e9cd9bc Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 2 Aug 2018 21:09:33 +0200 Subject: [PATCH] Ignore JSON option when using `got.stream()` (#541) --- readme.md | 6 +++--- source/as-stream.js | 4 ---- source/normalize-arguments.js | 4 ++++ test/arguments.js | 6 +++--- test/stream.js | 6 ++---- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/readme.md b/readme.md index fa38aefbd..1a7df572a 100644 --- a/readme.md +++ b/readme.md @@ -137,7 +137,7 @@ Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.st Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data) -*This is mutually exclusive with stream mode.* +*If you provide this option, `got.stream()` will be read-only.* Body that will be sent with a `POST` request. @@ -157,7 +157,7 @@ Default: `'utf8'` Type: `boolean`
Default: `false` -*This is mutually exclusive with stream mode.* +*If you provide this option, `got.stream()` will be read-only.* If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`. @@ -168,7 +168,7 @@ If set to `true` and `Content-Type` header is not set, it will be set to `applic Type: `boolean`
Default: `false` -*This is mutually exclusive with stream mode.* +*If you use `got.stream()`, this option will be ignored.* If set to `true` and `Content-Type` header is not set, it will be set to `application/json`. diff --git a/source/as-stream.js b/source/as-stream.js index e878b41c6..eb4404719 100644 --- a/source/as-stream.js +++ b/source/as-stream.js @@ -14,10 +14,6 @@ module.exports = options => { options.gotRetry.retries = () => 0; - if (options.json) { - throw new Error('Got can not be used as a stream when the `json` option is used'); - } - if (options.body) { proxy.write = () => { throw new Error('Got\'s stream is not writable when the `body` option is used'); diff --git a/source/normalize-arguments.js b/source/normalize-arguments.js index d22436649..36d4e5080 100644 --- a/source/normalize-arguments.js +++ b/source/normalize-arguments.js @@ -49,6 +49,10 @@ module.exports = (url, options, defaults) => { ...options }; + if (options.stream && options.json) { + options.json = false; + } + if (options.decompress && is.undefined(options.headers['accept-encoding'])) { options.headers['accept-encoding'] = 'gzip, deflate'; } diff --git a/test/arguments.js b/test/arguments.js index 21a4b9353..77d262307 100644 --- a/test/arguments.js +++ b/test/arguments.js @@ -109,9 +109,9 @@ test('should return streams when using stream option', async t => { t.is(data.toString(), 'ok'); }); -test('should not allow stream and JSON option at the same time', async t => { - const error = await t.throws(got(`${s.url}/stream`, {stream: true, json: true})); - t.is(error.message, 'Got can not be used as a stream when the `json` option is used'); +test('should ignore JSON option when using stream option', async t => { + const data = await pEvent(got(`${s.url}/stream`, {stream: true, json: true}), 'data'); + t.is(data.toString(), 'ok'); }); test('throws TypeError when `url` is passed as an option', async t => { diff --git a/test/stream.js b/test/stream.js index 5e1defaf6..12f734128 100644 --- a/test/stream.js +++ b/test/stream.js @@ -41,10 +41,8 @@ test.after('cleanup', async () => { await s.close(); }); -test('option.json can not be used', t => { - t.throws(() => { - got.stream(s.url, {json: true}); - }, 'Got can not be used as a stream when the `json` option is used'); +test('options.json is ignored', t => { + t.notThrows(() => got.stream(s.url, {json: true})); }); test('returns readable stream', async t => {