From 0c3ecc704d4d427b95ad59cf533c88b900e0f9ce Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 16 Oct 2020 10:57:16 +0200 Subject: [PATCH 1/5] `beforeRetry` allows stream body if different from original --- source/as-promise/index.ts | 4 +++- test/hooks.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/source/as-promise/index.ts b/source/as-promise/index.ts index 35e8944a6..c332bdcae 100644 --- a/source/as-promise/index.ts +++ b/source/as-promise/index.ts @@ -152,8 +152,10 @@ export default function asPromise(normalizedOptions: NormalizedOptions): Canc request.once('error', onError); + const previusBody = request.options.body; + request.once('retry', (newRetryCount: number, error: RequestError) => { - if (is.nodeStream(error.request?.options.body)) { + if (previusBody === error.request?.options.body && is.nodeStream(error.request?.options.body)) { onError(error); return; } diff --git a/test/hooks.ts b/test/hooks.ts index c757afac7..dddefe08a 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -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'; @@ -451,6 +452,43 @@ test('beforeRetry allows modifications', withServer, async (t, server, got) => { t.is(body.foo, 'bar'); }); +test.only('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'); + response.end(); + } else { + response.statusCode = 500; + response.end(); + } + }); + + const genBody = () => { + const form = new FormData(); + form.append('A', 'B'); + return form; + }; + + const {body} = await got.post('retry', { + body: genBody(), + retry: { + methods: ['POST'] + }, + hooks: { + beforeRetry: [ + options => { + const form = genBody(); + 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); From 35fa40a8572de6546d5b11d99d29544f130022e3 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 16 Oct 2020 10:58:39 +0200 Subject: [PATCH 2/5] Removed `test.only` --- test/hooks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hooks.ts b/test/hooks.ts index dddefe08a..9d284a54e 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -452,7 +452,7 @@ test('beforeRetry allows modifications', withServer, async (t, server, got) => { t.is(body.foo, 'bar'); }); -test.only('beforeRetry allows stream body if different from original', withServer, async (t, server, got) => { +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'); From ec35e5c8e47fc4b314af4bbb594a24dc98fca509 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 16 Oct 2020 11:09:16 +0200 Subject: [PATCH 3/5] Small cleanup --- test/hooks.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/hooks.ts b/test/hooks.ts index 9d284a54e..8774031f6 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -456,11 +456,10 @@ test('beforeRetry allows stream body if different from original', withServer, as server.post('/retry', async (request, response) => { if (request.headers.foo) { response.send('test'); - response.end(); } else { response.statusCode = 500; - response.end(); } + response.end(); }); const genBody = () => { From 5d3a25962d00608068a5215f221d0d430ad1a2f1 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 16 Oct 2020 13:48:17 +0200 Subject: [PATCH 4/5] Linting --- test/hooks.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/hooks.ts b/test/hooks.ts index 8774031f6..78be276e9 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -459,6 +459,7 @@ test('beforeRetry allows stream body if different from original', withServer, as } else { response.statusCode = 500; } + response.end(); }); From e4b83fd3fcc2b18addfac9ef0240f744e589f37f Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Sun, 18 Oct 2020 11:51:39 +0200 Subject: [PATCH 5/5] Applied requested changes --- source/as-promise/index.ts | 4 ++-- test/hooks.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/as-promise/index.ts b/source/as-promise/index.ts index c332bdcae..efd94703c 100644 --- a/source/as-promise/index.ts +++ b/source/as-promise/index.ts @@ -152,10 +152,10 @@ export default function asPromise(normalizedOptions: NormalizedOptions): Canc request.once('error', onError); - const previusBody = request.options.body; + const previousBody = request.options.body; request.once('retry', (newRetryCount: number, error: RequestError) => { - if (previusBody === error.request?.options.body && is.nodeStream(error.request?.options.body)) { + if (previousBody === error.request?.options.body && is.nodeStream(error.request?.options.body)) { onError(error); return; } diff --git a/test/hooks.ts b/test/hooks.ts index 78be276e9..adb8d19bf 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -463,21 +463,21 @@ test('beforeRetry allows stream body if different from original', withServer, as response.end(); }); - const genBody = () => { + const generateBody = () => { const form = new FormData(); form.append('A', 'B'); return form; }; const {body} = await got.post('retry', { - body: genBody(), + body: generateBody(), retry: { methods: ['POST'] }, hooks: { beforeRetry: [ options => { - const form = genBody(); + const form = generateBody(); options.body = form; options.headers['content-type'] = `multipart/form-data; boundary=${form.getBoundary()}`; options.headers.foo = 'bar';