Rest Assured DELETE Example

DELETE is also one of the methods of HTTP which is used to ‘Delete’ any resource specified by a URI. This HTTP request may or may not have a body, only the id parameter in the route path is used. The “204 – No Content” status code is often used with DELETE requests when no response body is sent.

The client cannot be guaranteed that the operation has been carried out, even if the status code returned from the origin server indicates that the action has been completed successfully. The server implementation determines if the actual content will be deleted or not. Additionally, it will check whether it can restore or not. 

When DELETE method is successfully applied, below are the response status codes are expected to return:

  • 202 (Accepted) status code – This will return if the action will likely succeed but has not yet been enacted.
  • 204 (No content) status code – This will return if the action has been enacted and no further information is to be supplied.
  • 200 (OK) status code – This will return if the action has been enacted and the response message includes a representation describing the status.

 

Important points of DELETE requests:

  1. DELETE method throws 404(NOt found) error if the specific resource to be deleted is not available. 
  2. 200’ (OK) status codes will be returned if the resource gets successfully deleted.
  3. DELETE method is idempotent which means that calling the same PUT request multiple times will always produce the same result.
  4. DELETE methods cannot be cached.
  5. DELETE requests are less secure.
  6. DELETE method can be applied to a single resource or a collection of resources.

 

Rest Assured DELETE Example

API /store/order/{orderId}
HTTP method Type DELETE
Comments {orderId} belongs to the Id of the pet for which we are trying to delete the order information. For example Id of pet 3  resource URL would be https://petstore.swagger.io/v2/store/order/3
Response body {
  “code”: 200,
  “type”: “unknown”,
  “message”: “successfully deleted the order “
}
Response header  access-control-allow-headers: Content-Typeapi_keyAuthorization
 access-control-allow-methods: GETPOSTDELETEPUT
 access-control-allow-origin: *
 content-type: application/json
 date: Sun02 Aug 2020 05:45:05 GMT
 server: Jetty(9.2.9.v20150224)
 status: 200 

 

How to write DELETE request API Test using Rest Assured ?

1. Set up a request with the specified base URI

 RestAssured.baseURI = "https://petstore.swagger.io/v2/user";

Base URI is the root address of the Resource. Here we are specifying to REST assured to use the above as the base URI of the service.

 

2. Specify the exact resource and perform DELETE request to it

RestAssured.given()

.when()

.delete("/user1")

Suppose we want to delete the Petstore order whose order ID equals “1”. The API to fetch the pet store order is /store/order/{orderId}, where we would be passing our order ID. Thus the complete URL will be “https://petstore.swagger.io/v2/store/order/1to which we are sending a DELETE request.

 

3. Response Validation

Here we usually perform below three types of validation:

1. Validation of Status code

  .then().assertThat().statusCode(200);

Since it is a DELETE request for which we are making a call, thus the expected status code would be 200. Here, we are validating the response based on the condition where if status code from the response matches with the value 200

2. Validation of Response header

  .then().assertThat().header(content-type, "application/json");

Here, we are verifying if the content type is matching “application/json” then the assert will return true else false. 

3. Validation of Response Body

 .then().assertThat().body(message, "successfully deleted the order");

Here we are comparing one of the variables of the body with it’s expected result in the assert statement. If the actual result matched with expected, then assert return true else false.

 

Complete Example Code

@Test

public void DeleteUser() {


RestAssured.baseURI = "https://petstore.swagger.io/v2/user";

RestAssured.given()

.when()

.delete("/user1")

.then().assertThat().statusCode(200);

}