- We need to pass booking id which you want to delete in URI. E.g. https://restful-booker.herokuapp.com/booking/1 where “1” is booking id.
- Authentication token need to pass as cookie. Cookie name is “token” and value is generated auth token. ‘Cookie: token=<generatedToken>’.
- You don’t required to pass body to DELETE request.
Let’s see existing details of a Booking ID 1 using Postman:-
Let’s generate token which I have already covered in Previous post. Perform all above steps in Postman tool as of now. We will see in upcoming posts to chain different requests in REST Assured.
Generate Token:-
Non-BDD Style:-
package RestfulBooker.deleteExamples;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
public class NonBDDDeleteRequest {
public static void main(String[] args) {
// Create a request specification
RequestSpecification request = RestAssured.given();
// Setting a cookie for authentication as per API documentation
request.cookie("token", "f4e70e7b9bbcd05");
// Adding URI
request.baseUri("https://restful-booker.herokuapp.com/booking/1");
// Calling PUT method on URI. After hitting we get Response
Response response = request.delete();
// Printing Response as string
System.out.println(response.asString());
// Get Validatable response to perform validation
ValidatableResponse validatableResponse = response.then();
// Validate status code as 201 as per API documentation
validatableResponse.statusCode(201);
// Validate if booking is actually deleted.
RequestSpecification getRequestSpec = RestAssured.given();
// Adding URI
getRequestSpec.baseUri("https://restful-booker.herokuapp.com/booking/1");
// Calling GET request
Response res = getRequestSpec.get();
// Get Validatable response to perform validation
ValidatableResponse valRes = res.then();
// It will check if status code is 404 as booking id should not be found
valRes.statusCode(404);
}
}
BDD Style:-
package RestfulBooker.deleteExamples;
import io.restassured.RestAssured;
public class BDDStyleDeleteRequest {
public static void main(String[] args) {
// Delete Booking
//GIVEN
RestAssured
.given()
.baseUri("https://restful-booker.herokuapp.com/booking/1")
.cookie("token", "f7dddb1093eab19")
// WHEN
.when()
.delete()
// THEN
.then()
.assertThat()
.statusCode(201);
// Verifying booking is deleted
// Given
RestAssured
.given()
.baseUri("https://restful-booker.herokuapp.com/booking/1")
// When
.when()
.get()
// Then
.then()
.statusCode(404);
}
}
Note:- You need to generate access token every time you hit the request otherwise you will get error code as 403.