Skip to content

Commit

Permalink
fix(interceptor): don't drop unprocessed streams
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Aug 25, 2021
1 parent 97e6f85 commit eb742c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/interceptor.js
Expand Up @@ -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()
}
Expand Down
6 changes: 6 additions & 0 deletions tests/test_reply_with_file.js
Expand Up @@ -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('..')
Expand Down Expand Up @@ -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)
Expand All @@ -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()
})

Expand Down

0 comments on commit eb742c2

Please sign in to comment.