1. Understanding REST & REST Assured API
REST stands for Representational State Transfer mainly uses the http protocol to exchange messages
between client and server using different methods such as GET,POST,PUT,DELETE.
Example :
COVID19 API Website Details
COVID19 API Details
Request Details::
- REST API EndPoint: https://api.covid19api.com/
- Method Type: GET
- Request Headers:
authority: api.covid19api.com - Query Params:
https://getAllEmployees&empID=1
Response Details::
- Response Code:200
- Response Headers:content-type: application/json; charset=UTF-8
- Response Body:{“allRoute”:{“Name”:”Get All Data”}}
2. Design Test-cases
3. Pre-requisites for Automation REST Services:
com.jayway is a older version
io.restassured is the latest version
Eclipse
Maven project
pom.xml
io.restassured dependency
groupId: io.rest-assured
artifactId: rest-assured
version: 4.3.0
TESTNG – validate the test results
4. Get Method example
RestAssured
- Response Code Validation
- Response Headers Validation
- Response Body Validation
Java Logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
package com.covidtracking.tests; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.testng.Assert; import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; import io.restassured.RestAssured; import io.restassured.http.Header; import io.restassured.http.Headers; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; public class CovidTrackingTests { @Test public void validateCovid19APIGetMethod() { //Build the request object RequestSpecification requestSpecificationObj = RestAssured.given(); //Add the request headers to the request object Map<String,Object> requestHeaders = new LinkedHashMap<String,Object>(); requestHeaders.put("authority", "api.covid19api.com"); requestHeaders.put("accept-encoding","gzip, deflate, br"); requestSpecificationObj.headers(requestHeaders); String restEndPointURL ="https://api.covid19api.com/"; Response response = requestSpecificationObj.when().get(restEndPointURL); //Validate the ResponseCode int actualResponceCode = response.getStatusCode(); int expectedResponseCode = 200; SoftAssert sa = new SoftAssert(); sa.assertEquals(actualResponceCode, expectedResponseCode); //Validate the Response Headers contain content-length: 1119 Headers responseHeaders = response.getHeaders(); List<Header> headersList = responseHeaders.asList(); int expectedContentLength=1119; String actualContentLength = null; for(int i=0;i<headersList.size();i++) { System.out.println(headersList.get(i).getName() + ":" + headersList.get(i).getValue()); if(headersList.get(i).getName().contains("Content-Length")) { actualContentLength= headersList.get(i).getValue(); } } sa.assertEquals(Integer.parseInt(actualContentLength), expectedContentLength); //Validate the content of the Response Body String responseBody = response.getBody().prettyPrint(); System.out.println(responseBody); responseBody = response.getBody().asString(); System.out.println(responseBody); JsonPath jsonPath = new JsonPath(response.getBody().asString()); String actualResponseBody = jsonPath.get("allRoute.Name"); String expectedResponseBoody = "Get All Data"; sa.assertEquals(actualResponseBody, expectedResponseBoody); sa.assertAll(); } } |
Conclusion:
Validated Response Code
Validated Response Headers
Validated Response Body