Skip to content

Commit

Permalink
beforeRetry allows stream body if different from original (#1501)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giotino committed Oct 20, 2020
1 parent b1afa2b commit 3dd2273
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion source/as-promise/index.ts
Expand Up @@ -152,8 +152,10 @@ export default function asPromise<T>(normalizedOptions: NormalizedOptions): Canc

request.once('error', onError);

const previousBody = request.options.body;

request.once('retry', (newRetryCount: number, error: RequestError) => {
if (is.nodeStream(error.request?.options.body)) {
if (previousBody === error.request?.options.body && is.nodeStream(error.request?.options.body)) {
onError(error);
return;
}
Expand Down
38 changes: 38 additions & 0 deletions test/hooks.ts
Expand Up @@ -3,6 +3,7 @@ import {Agent as HttpAgent} from 'http';
import test, {Constructor} from 'ava';
import nock = require('nock');
import getStream = require('get-stream');
import FormData = require('form-data');
import sinon = require('sinon');
import delay = require('delay');
import {Handler} from 'express';
Expand Down Expand Up @@ -451,6 +452,43 @@ test('beforeRetry allows modifications', withServer, async (t, server, got) => {
t.is(body.foo, 'bar');
});

test('beforeRetry allows stream body if different from original', withServer, async (t, server, got) => {
server.post('/retry', async (request, response) => {
if (request.headers.foo) {
response.send('test');
} else {
response.statusCode = 500;
}

response.end();
});

const generateBody = () => {
const form = new FormData();
form.append('A', 'B');
return form;
};

const {body} = await got.post('retry', {
body: generateBody(),
retry: {
methods: ['POST']
},
hooks: {
beforeRetry: [
options => {
const form = generateBody();
options.body = form;
options.headers['content-type'] = `multipart/form-data; boundary=${form.getBoundary()}`;
options.headers.foo = 'bar';
}
]
}
});

t.is(body, 'test');
});

test('afterResponse is called with response', withServer, async (t, server, got) => {
server.get('/', echoHeaders);

Expand Down

0 comments on commit 3dd2273

Please sign in to comment.