How to handle 4xx status codes in auto-tests

31 Views Asked by At

I need to assert that any other order with the same data returns a 410 status code, but the problem is that it returns a 410 status code and the code automatically stops its execution, how can I ignore this error so the program doesn't fail when it gets 410 status code in a response?

Sometimes it passes, but I believe because of the asynchronous nature it fails sometimes

I tried to use different approaches like using try/catch or simply chain catch method to the axios.post

Also I tried to use validateStatus like here

Using the validateStatus config option, you can define HTTP code(s) that should throw an error.

axios.get('/user/12345', {
  validateStatus: function (status) {
    return status < 500; // Resolve only if the status code is less than 500
  }
})

Here is the piece of code that returns a 410 status code

const customValidateStatus = (status) => {
  return status < 500;
};

try {
      // send another createOrder api request with the same postData
      axios.post(`https://chaiz-api${urlPart}.azurewebsites.net/order/create`, postData, {
        headers: {
          "X-Idempotency-Key": uuidSecondRequest
        },
        validateStatus: customValidateStatus
      }).catch(error => {
        console.log('Error happened with the status code', error.response.status);

        // assert that it returns 410 status code
        browser.assert.equal(error.response.status, 410);
      });
    }
    catch (err) {
      console.log('Error happened');
    }

And my goal is to ignore the case where 410 status code stops execution of the program

0

There are 0 best solutions below