Skip to content

Commit 3dd2273

Browse files
authoredOct 20, 2020
beforeRetry allows stream body if different from original (#1501)
1 parent b1afa2b commit 3dd2273

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
 

‎source/as-promise/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ export default function asPromise<T>(normalizedOptions: NormalizedOptions): Canc
152152

153153
request.once('error', onError);
154154

155+
const previousBody = request.options.body;
156+
155157
request.once('retry', (newRetryCount: number, error: RequestError) => {
156-
if (is.nodeStream(error.request?.options.body)) {
158+
if (previousBody === error.request?.options.body && is.nodeStream(error.request?.options.body)) {
157159
onError(error);
158160
return;
159161
}

‎test/hooks.ts

+38
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Agent as HttpAgent} from 'http';
33
import test, {Constructor} from 'ava';
44
import nock = require('nock');
55
import getStream = require('get-stream');
6+
import FormData = require('form-data');
67
import sinon = require('sinon');
78
import delay = require('delay');
89
import {Handler} from 'express';
@@ -451,6 +452,43 @@ test('beforeRetry allows modifications', withServer, async (t, server, got) => {
451452
t.is(body.foo, 'bar');
452453
});
453454

455+
test('beforeRetry allows stream body if different from original', withServer, async (t, server, got) => {
456+
server.post('/retry', async (request, response) => {
457+
if (request.headers.foo) {
458+
response.send('test');
459+
} else {
460+
response.statusCode = 500;
461+
}
462+
463+
response.end();
464+
});
465+
466+
const generateBody = () => {
467+
const form = new FormData();
468+
form.append('A', 'B');
469+
return form;
470+
};
471+
472+
const {body} = await got.post('retry', {
473+
body: generateBody(),
474+
retry: {
475+
methods: ['POST']
476+
},
477+
hooks: {
478+
beforeRetry: [
479+
options => {
480+
const form = generateBody();
481+
options.body = form;
482+
options.headers['content-type'] = `multipart/form-data; boundary=${form.getBoundary()}`;
483+
options.headers.foo = 'bar';
484+
}
485+
]
486+
}
487+
});
488+
489+
t.is(body, 'test');
490+
});
491+
454492
test('afterResponse is called with response', withServer, async (t, server, got) => {
455493
server.get('/', echoHeaders);
456494

0 commit comments

Comments
 (0)
Please sign in to comment.