Skip to content
Permalink
Browse files
fix: send Buffer with length (#2232)
  • Loading branch information
Beretta1979 committed Oct 19, 2021
1 parent 83131b7 commit 8fcc607433590e1993d552a06a041e3061d1905b
Showing with 25 additions and 4 deletions.
  1. +1 −1 lib/intercepted_request_router.js
  2. +24 −3 tests/test_request_overrider.js
@@ -153,7 +153,7 @@ class InterceptedRequestRouter {
return false
}

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

@@ -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')
@@ -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()
@@ -112,8 +113,7 @@ describe('Request Overrider', () => {
}
)

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

@@ -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.