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