Skip to content

Commit

Permalink
Ignore JSON option when using got.stream() (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Aug 2, 2018
1 parent 6ba9e68 commit 4d92eb6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -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.

Expand All @@ -157,7 +157,7 @@ Default: `'utf8'`
Type: `boolean`<br>
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`.

Expand All @@ -168,7 +168,7 @@ If set to `true` and `Content-Type` header is not set, it will be set to `applic
Type: `boolean`<br>
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`.

Expand Down
4 changes: 0 additions & 4 deletions source/as-stream.js
Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions source/normalize-arguments.js
Expand Up @@ -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';
}
Expand Down
6 changes: 3 additions & 3 deletions test/arguments.js
Expand Up @@ -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 => {
Expand Down
6 changes: 2 additions & 4 deletions test/stream.js
Expand Up @@ -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 => {
Expand Down

0 comments on commit 4d92eb6

Please sign in to comment.