vendredi 30 août 2019

Testing 400 status code axios throws error

I'm creating an api test framework for a project I am working on and I'm looking to validate required fields in JSON objects being sent to an endpoint.

I'm trying to send a JSON object with a missing field and expecting a 400 response from my application with a validation message. But when making this call with axios it (somewhat rightly) throws an error as it received the 400.

I want to be able to assert that 400 is expected and to assert on the validation message.

All the examples I've come across are all regarding dealing with the 400 response in the correct way you would if you are not expecting the 400 response, but i am expecting it.

I couldn't seem to find anyone else trying this.

async function createReminder(reminderObject) {
  const response = await axios.post(`${config.get('baseUrl')}/reminder`, {
    ...reminderObject
  });

  return response;
}

module.exports.createReminder = createReminder;

Here is my working code as it stands. This will make a valid 200 when being used with a valid call.

I need to have the 400 response / validation message be returned in teh response object to be handled in the function that calls this function.

Aucun commentaire:

Enregistrer un commentaire