Skip to content

Commit

Permalink
Throw when the server aborts the pending request
Browse files Browse the repository at this point in the history
Fixes #1096
  • Loading branch information
szmarczak committed Apr 14, 2020
1 parent c15e020 commit 3247529
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,13 @@ export default class Request extends Duplex implements RequestEvents<Request> {
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'];
Expand Down
21 changes: 21 additions & 0 deletions test/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
});

0 comments on commit 3247529

Please sign in to comment.