Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeRetry allows stream body if different from original #1501

Merged
merged 5 commits into from Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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