From 32475290df5b8b6c98421bbfb1479d9a1aadef27 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Tue, 14 Apr 2020 20:28:23 +0200 Subject: [PATCH] Throw when the server aborts the pending request Fixes #1096 --- source/core/index.ts | 7 +++++++ test/http.ts | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/source/core/index.ts b/source/core/index.ts index 885e06006..f348269b6 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -968,6 +968,13 @@ export default class Request extends Duplex implements RequestEvents { this._beforeError(new ReadError(error, options, response as Response)); }); + response.once('aborted', () => { + this._beforeError(new ReadError({ + name: 'Error', + message: 'The server aborted the pending request' + }, options, response as Response)); + }); + this.emit('downloadProgress', this.downloadProgress); const rawCookies = response.headers['set-cookie']; diff --git a/test/http.ts b/test/http.ts index 728fdc443..fb90a9683 100644 --- a/test/http.ts +++ b/test/http.ts @@ -183,3 +183,24 @@ test('the response contains timings property', withServer, async (t, server, got t.truthy(timings); t.true(timings.phases.total! >= 0); }); + +test('throws an error if the server aborted the request', withServer, async (t, server, got) => { + server.get('/', (_request, response) => { + response.writeHead(200, { + 'content-type': 'text/plain' + }); + response.write('chunk 1'); + + setImmediate(() => { + response.write('chunk 2'); + + setImmediate(() => { + response.destroy(); + }); + }); + }); + + await t.throwsAsync(got(''), { + message: 'The server aborted the pending request' + }); +});