Skip to content

Commit

Permalink
fix: send Buffer with length (#2232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beretta1979 committed Oct 19, 2021
1 parent 83131b7 commit 8fcc607
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/intercepted_request_router.js
Expand Up @@ -153,7 +153,7 @@ class InterceptedRequestRouter {
return false
}

if (!buffer || buffer.length === 0) {
if (!buffer) {
return true
}

Expand Down
27 changes: 24 additions & 3 deletions tests/test_request_overrider.js
Expand Up @@ -16,6 +16,7 @@ const { URL } = require('url')
const { expect } = require('chai')
const sinon = require('sinon')
const nock = require('..')
const FormData = require('form-data')

const got = require('./got_client')
const servers = require('./servers')
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('Request Overrider', () => {
})
})

it('write callback is not called if the provided chunk is an empty buffer', done => {
it('write callback is not called if the provided chunk is undefined', done => {
const scope = nock('http://example.test').post('/').reply()

const reqWriteCallback = sinon.spy()
Expand All @@ -112,8 +113,7 @@ describe('Request Overrider', () => {
}
)

const buf = Buffer.from('')
req.write(buf, null, reqWriteCallback)
req.write(undefined, null, reqWriteCallback)
req.end()
})

Expand Down Expand Up @@ -805,4 +805,25 @@ describe('Request Overrider', () => {

req.abort()
})

// https://github.com/nock/nock/issues/2231
it('mocking a request which sends an empty buffer should finalize', async () => {
const prefixUrl = 'http://www.test.com'
const bufferEndpoint = 'upload/buffer/'

nock(prefixUrl).post(`/${bufferEndpoint}`).reply(200, 'BUFFER_SENT')

const formData = new FormData()

formData.append('fileData', Buffer.alloc(0), 'chunk')

const options = {
prefixUrl,
body: formData,
}

const { body: response } = await got.post(bufferEndpoint, options)

expect(response).to.equal('BUFFER_SENT')
})
})

0 comments on commit 8fcc607

Please sign in to comment.