Skip to content

Commit

Permalink
Fix stream body check
Browse files Browse the repository at this point in the history
Fixes #950
  • Loading branch information
szmarczak committed Dec 1, 2019
1 parent f0c782e commit 2ec5c4d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions source/as-stream.ts
Expand Up @@ -19,10 +19,10 @@ export default function asStream<T>(options: NormalizedOptions): ProxyStream<T>

options.retry.calculateDelay = () => 0;

if (options.body) {
if (options.body || options.json || options.form) {
proxy.write = () => {
proxy.destroy();
throw new Error('Got\'s stream is not writable when the `body` option is used');
throw new Error('Got\'s stream is not writable when the `body`, `json` or `form` option is used');
};
} else if (options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH') {
options.body = input;
Expand Down
17 changes: 15 additions & 2 deletions test/stream.ts
Expand Up @@ -69,9 +69,22 @@ test('throws on write if body is specified', withServer, (t, server, got) => {
server.post('/', postHandler);

t.throws(() => {
// @ts-ignore Error tests
got.stream.post({body: 'wow'}).end('wow');
}, 'Got\'s stream is not writable when the `body` option is used');
}, 'Got\'s stream is not writable when the `body`, `json` or `form` option is used');

t.throws(() => {
got.stream.post({json: {}}).end('wow');
}, 'Got\'s stream is not writable when the `body`, `json` or `form` option is used');

t.throws(() => {
got.stream.post({form: {}}).end('wow');
}, 'Got\'s stream is not writable when the `body`, `json` or `form` option is used');
});

test('does not throw if using stream and passing a json option', withServer, async (t, server, got) => {
server.post('/', postHandler);

await t.notThrowsAsync(getStream(got.stream.post({json: {}})));
});

test('throws on write if no payload method is present', withServer, (t, server, got) => {
Expand Down

0 comments on commit 2ec5c4d

Please sign in to comment.