From eb742c20367e7b149ed63d4ffd14f8676222fdc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kol=C3=A1rik?= Date: Wed, 25 Aug 2021 03:19:10 +0200 Subject: [PATCH] fix(interceptor): don't drop unprocessed streams --- lib/interceptor.js | 6 +++++- tests/test_reply_with_file.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index 879801e29..1b273fd6b 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -442,7 +442,11 @@ module.exports = class Interceptor { markConsumed() { this.interceptionCounter++ - if ((this.scope.shouldPersist() || this.counter > 0) && this.filePath) { + if ( + (this.scope.shouldPersist() || this.counter > 0) && + this.interceptionCounter > 1 && + this.filePath + ) { this.body = fs.createReadStream(this.filePath) this.body.pause() } diff --git a/tests/test_reply_with_file.js b/tests/test_reply_with_file.js index 004a15152..639abd688 100644 --- a/tests/test_reply_with_file.js +++ b/tests/test_reply_with_file.js @@ -2,7 +2,9 @@ // Tests for `.replyWithFile()`. +const fs = require('fs') const path = require('path') +const sinon = require('sinon') const { expect } = require('chai') const proxyquire = require('proxyquire').preserveCache() const nock = require('..') @@ -40,6 +42,8 @@ describe('`replyWithFile()`', () => { }) it('reply with file with repeated', async () => { + sinon.spy(fs) + const scope = nock('http://example.test') .get('/') .times(2) @@ -55,6 +59,8 @@ describe('`replyWithFile()`', () => { expect(response2.statusCode).to.equal(200) expect(response2.body).to.have.lengthOf(20) + expect(fs.createReadStream.callCount).to.equal(2) + scope.done() })