loading...
Cover image for Write API tests for HTTP DELETE method

Write API tests for HTTP DELETE method

automationbro profile image Automation Bro ・1 min read

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);
      });
  });

This will likely return the body similar to this-

{ code: 204, meta: null, data: null }

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

{ code: 404, meta: null, data: { message: 'Resource not found' } }

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

Discussion

pic
Editor guide