mia-platform/custom-plugin-lib

returnAs: 'BUFFER' and allowedStatusCodes as options in delete with 204 gives error

simone-paglino opened this issue · 0 comments

The description of the bug or the rationale of your proposal

Passing to the delete function of a service both returnAs: 'BUFFER' and allowedStatusCodes: [...] inside options object, gives an error of a JSON input end of file (if the response has a 204 HTTP status code with 'No Content').

The description of the bug or the rationale of your proposal

Check the line of code where there's the JSON.parse command which creates this problem and make a conditional statement to not use it if there's no response.

A snippet of code for replicating the issue or showing the proposal usage if applicable

To replicate the issue:

nock('http://my-service-name')
.delete('/foo')
.reply(404)

const service = serviceBuilder('my-service-name')

try {
  await service.delete(
    '/foo',
    undefined,
    undefined,
    { returnAs: 'BUFFER', allowedStatusCodes: [204] }
  )
} catch (error) {
  assert.equal(error.message, 'Invalid status code: 404. Allowed: 204.')
}

The expected result for your bug

The test should give an error of JSON input end of file

Your environment

node: 15.10.0

custom-plugin-lib: 4.2.0

os: --