Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: send Buffer with length #2232

Merged
merged 2 commits into from Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})
})