Skip to content

Commit

Permalink
Destroy request object after successful response (#2187)
Browse files Browse the repository at this point in the history
  • Loading branch information
nduthoit authored Dec 7, 2022
1 parent 5e17bb7 commit 2d1497e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/as-promise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export default function asPromise<T>(normalizedOptions: NormalizedOptions): Canc
return;
}

request.destroy();
resolve(request.options.resolveBodyOnly ? response.body as T : response as unknown as T);
});

Expand Down
11 changes: 11 additions & 0 deletions test/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,14 @@ test('promise.json() does not fail when server returns an error and throwHttpErr
const promise = got('', {throwHttpErrors: false});
await t.notThrowsAsync(promise.json());
});

test('the request is destroyed once the promise has resolved', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 200;
response.end('null');
});

const {request} = await got('');

t.true(request.destroyed);
});

0 comments on commit 2d1497e

Please sign in to comment.