Skip to content

Commit

Permalink
Throw an error when retrying with consumed body
Browse files Browse the repository at this point in the history
Fixes #1694
  • Loading branch information
szmarczak committed Apr 27, 2021
1 parent 83575d5 commit 62305d7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/as-promise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export default function asPromise<T>(firstRequest: Request): CancelableRequest<T
const newBody = request.options.body;

if (previousBody === newBody && is.nodeStream(newBody)) {
error.message = 'Cannot retry with consumed body stream';

onError(error);
return;
}
Expand Down
29 changes: 29 additions & 0 deletions test/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import delay from 'delay';
import pEvent from 'p-event';
import {Handler} from 'express';
import getStream from 'get-stream';
import FormData from 'form-data';
import toReadableStream from 'to-readable-stream';
import got, {UploadError} from '../source/index.js';
import withServer from './helpers/with-server.js';
Expand Down Expand Up @@ -336,3 +337,31 @@ test('throws on upload error', withServer, async (t, server, got) => {
message
});
});

test('formdata retry', withServer, async (t, server, got) => {
server.post('/', echoHeaders);

const instance = got.extend({
hooks: {
afterResponse: [
async (_response, retryWithMergedOptions) => {
return retryWithMergedOptions({
headers: {
foo: 'bar'
}
});
}
]
}
});

const form = new FormData();
form.append('hello', 'world');

await t.throwsAsync(instance.post({
body: form,
headers: form.getHeaders()
}).json<{foo?: string}>(), {
message: 'Cannot retry with consumed body stream'
});
});

0 comments on commit 62305d7

Please sign in to comment.