From 97e6f853c818afb1c6bf85f9381fc07a51de9993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kol=C3=A1rik?= Date: Wed, 25 Aug 2021 03:16:09 +0200 Subject: [PATCH] fix(interceptor): don't hang on the last response when using .times() --- lib/interceptor.js | 4 ++-- tests/test_reply_with_file.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index 8c79b2d80..879801e29 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -442,13 +442,13 @@ module.exports = class Interceptor { markConsumed() { this.interceptionCounter++ - remove(this) - if ((this.scope.shouldPersist() || this.counter > 0) && this.filePath) { this.body = fs.createReadStream(this.filePath) this.body.pause() } + remove(this) + if (!this.scope.shouldPersist() && this.counter < 1) { this.scope.remove(this._key, this) } diff --git a/tests/test_reply_with_file.js b/tests/test_reply_with_file.js index 4adbb8cde..004a15152 100644 --- a/tests/test_reply_with_file.js +++ b/tests/test_reply_with_file.js @@ -39,6 +39,25 @@ describe('`replyWithFile()`', () => { scope.done() }) + it('reply with file with repeated', async () => { + const scope = nock('http://example.test') + .get('/') + .times(2) + .replyWithFile(200, binaryFilePath, { + 'content-encoding': 'gzip', + }) + + const response1 = await got('http://example.test/') + expect(response1.statusCode).to.equal(200) + expect(response1.body).to.have.lengthOf(20) + + const response2 = await got('http://example.test/') + expect(response2.statusCode).to.equal(200) + expect(response2.body).to.have.lengthOf(20) + + scope.done() + }) + describe('with no fs', () => { const { Scope } = proxyquire('../lib/scope', { './interceptor': proxyquire('../lib/interceptor', {