Rest Assured GET Example

GET method is one of the most common methods of HTTP Protocol which is used to request data from the REST API. REST API sends the response back when you send the GET request.


Important points of GET requests:

  1. GET requests parameters to remain in the browser history as they are not sent as part of the URL
  2. GET requests can be cached
  3. GET requests are prone to get hacked easily
  4. HttpClient API provides a class named HttpGet which represents the get request method.
  5. GET requests are less secure therefore should be avoided when trying to retrieve data from a sensitive resource
  6. GET requests can only be used to retrieve data not to modify
  7. GET requests allows only ASCII characters
  8. GET requests parameter details limited because of length restrictions.

 

Method to be followed

We would be automating the API tests using the famous BDD Framework. It consists of GIVEN, WHEN and THEN statement.

The Given() statement enables us to pass the request headers, query and path param, cookies, etc. It focuses on: “What is the context of the system? What pre-condition must be true before the action is tested? What data is in the system?”

The When() statement enables to test the input data via the business rule when the particular action occurs. It focuses on: “What will be tested?”

Any validation to be performed can be written after Then(). The assertion conditions come at this place. It focuses on: “What is the observable outcome? What are the expected results? What is the post-condition (state) or output data observable by the user?”

 

Rest Assured GET Example

API /store/order/{orderId}
HTTP method Type GET
Comments {orderId} belongs to the Id of the Petstore order for which we are trying to retrieve the order information of the pet. For example Id of Petstore order “3”, resource URL would be https://petstore.swagger.io/v2/store/order/3
Response body {
  “id”: 3,
  “petId”: 2,
  “quantity”: 2,
  “shipDate”: “2020-08-02T00:01:15.106+0000”,
  “status”: “placed”,
  “complete”: false
}
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 GET request API Test using Rest Assured?

1. Set up a request with the specified base URI

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

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 Get request to it

RestAssured.given()

.when()

.get("/1")

Suppose we want to fetch the Petstore order whose 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/1” to which we are sending a GET 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 GET 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(status, "placed");

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 GetDetail() {


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

RestAssured.given()

.when()

.get("/1")

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

}