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() })