Skip to content

Commit 62305d7

Browse files
committedApr 27, 2021
Throw an error when retrying with consumed body
Fixes #1694
1 parent 83575d5 commit 62305d7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎source/as-promise/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ export default function asPromise<T>(firstRequest: Request): CancelableRequest<T
149149
const newBody = request.options.body;
150150

151151
if (previousBody === newBody && is.nodeStream(newBody)) {
152+
error.message = 'Cannot retry with consumed body stream';
153+
152154
onError(error);
153155
return;
154156
}

‎test/post.ts

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import delay from 'delay';
77
import pEvent from 'p-event';
88
import {Handler} from 'express';
99
import getStream from 'get-stream';
10+
import FormData from 'form-data';
1011
import toReadableStream from 'to-readable-stream';
1112
import got, {UploadError} from '../source/index.js';
1213
import withServer from './helpers/with-server.js';
@@ -336,3 +337,31 @@ test('throws on upload error', withServer, async (t, server, got) => {
336337
message
337338
});
338339
});
340+
341+
test('formdata retry', withServer, async (t, server, got) => {
342+
server.post('/', echoHeaders);
343+
344+
const instance = got.extend({
345+
hooks: {
346+
afterResponse: [
347+
async (_response, retryWithMergedOptions) => {
348+
return retryWithMergedOptions({
349+
headers: {
350+
foo: 'bar'
351+
}
352+
});
353+
}
354+
]
355+
}
356+
});
357+
358+
const form = new FormData();
359+
form.append('hello', 'world');
360+
361+
await t.throwsAsync(instance.post({
362+
body: form,
363+
headers: form.getHeaders()
364+
}).json<{foo?: string}>(), {
365+
message: 'Cannot retry with consumed body stream'
366+
});
367+
});

0 commit comments

Comments
 (0)
Please sign in to comment.