DEV Community

Cover image for Write API tests for HTTP DELETE method
Dilpreet Johal
Dilpreet Johal

Posted on

Write API tests for HTTP DELETE method

The HTTP DELETE method is pretty straightforward, it deletes an existing resource and if the resource is already deleted it will most likely throw a 'not found' error.

Let's take a look at an example -

  it('DELETE /users/:id', () => {
    return request
      .delete('users/2')
      .set('Authorization', `Bearer ${TOKEN}`)
      .then((res) => {
        expect(res.body.data).to.be.eq(null);
      });
  });
Enter fullscreen mode Exit fullscreen mode

This will likely return the body similar to this-

{ code: 204, meta: null, data: null }
Enter fullscreen mode Exit fullscreen mode

And, upon hitting the same request again, you will see a not-found error -

{ code: 404, meta: null, data: { message: 'Resource not found' } }
Enter fullscreen mode Exit fullscreen mode

Check out this video to see a detailed explanation on how to work with the HTTP DELETE method:

You can also clone the GitHub repo to access this code


To learn more about API testing, check out my free tutorial series here -

https://www.youtube.com/watch?v=ZSVw3TyZur4&list=PL6AdzyjjD5HDR2kNRU2dA1C8ydXRAaaBV&ab_channel=AutomationBro


I hope this post helped you out, let me know in the comments below!

Happy testing! 😄

...

Subscribe to my YouTube channel
Support my work - https://www.buymeacoffee.com/automationbro
Follow @automationbro on Twitter

Top comments (0)