Skip to content

Commit

Permalink
test: send a Buffer with length 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Beretta1979 authored and gr2m committed Oct 19, 2021
1 parent 83131b7 commit ffaa218
Showing 1 changed file with 24 additions and 3 deletions.
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 ffaa218

Please sign in to comment.