From d64298cae7556ec1a3501f72ff51190802867c35 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Wed, 1 Apr 2020 17:57:55 +0200 Subject: [PATCH] Fix #1129 --- source/core/index.ts | 4 ++-- test/stream.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/source/core/index.ts b/source/core/index.ts index c6b48042a..ccf8954e1 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1102,6 +1102,8 @@ export default class Request extends Duplex implements RequestEvents { this.push(null); }); + this.emit('response', response); + for (const destination of this[kServerResponsesPiped]) { if (destination.headersSent) { continue; @@ -1119,8 +1121,6 @@ export default class Request extends Duplex implements RequestEvents { destination.statusCode = statusCode; } - - this.emit('response', response); } _onRequest(request: ClientRequest): void { diff --git a/test/stream.ts b/test/stream.ts index a0921408b..74a70a566 100644 --- a/test/stream.ts +++ b/test/stream.ts @@ -352,6 +352,21 @@ test('errors have body', withServer, async (t, server, got) => { }))); t.is(error.message, 'snap'); - console.log(error.response); t.is(error.response?.body, 'yay'); }); + +test('pipe can send modified headers', withServer, async (t, server, got) => { + server.get('/foobar', (_request, response) => { + response.setHeader('foo', 'bar'); + response.end(); + }); + + server.get('/', (_request, response) => { + got.stream('foobar').on('response', response => { + response.headers.foo = 'boo'; + }).pipe(response); + }); + + const {headers} = await got(''); + t.is(headers.foo, 'boo'); +});