Skip to content

Commit

Permalink
fix(interceptor): don't hang on the last response when using .times()
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Aug 25, 2021
1 parent 74e30de commit 97e6f85
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/interceptor.js
Expand Up @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions tests/test_reply_with_file.js
Expand Up @@ -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', {
Expand Down

0 comments on commit 97e6f85

Please sign in to comment.