Friday 26 August 2016

Test REST Web Services using REST Assured API

REST Assured(RA) is a framework built on Java developed to test the REST services. It supports any HTTP method but has explicit support for GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH and includes specifying & validating like Headers, Cookies etc.

RA has inbuilt support for multiple authentication like BASIC, OAuth, OAuth2, Form, Certificate, Digest, CSRF (Cross Site Request Forgery) etc.

RA out of the box has the support for BDD approach.

Follow the below steps to configure & test the REST web services.



STEPS:


STEP 1: Download the latest version of REST ASSURD binaries from the below location.

          REST Assured Binaries.


STEP 2: Create the java project in eclipse & add the 'rest-assured-*.*.*.jar' and other dependent jars into project build path.




STEP 3: Prepare the required Input parameters like 

String url = "www.testurl.com/testpath/id";

String requestType = "GET";

// All the header params including Auth details if any can be added into headermap.
HashMap headermap = new HashMap<>();
headermap.put("cache-control", "no-cache");
headermap.put("Content-Type", "application/json");
headermap.put......

//Assign the request body (xml / json) to the below variable which can be used based on the service requirement.
String reqBody = "";

//Path of the response node, which need to be validated with the expected value
String validationKey = "reponse.test.orderno";
String expOrderVal = "A04BH";
String actualOrderVal = "";

//Variable to capture response
String resp = "";

STEP 4: Pass the above details as input to the REST Assured APIs, which would execute the request & validate the response.

if(requestType == "GET")
resp = RestAssured.given().log().all().headers(headermap).get(url);
else if(requestType == "POST")
resp = RestAssured.given().log().all().headers(headermap).body(reqBody).post(url);
else if(requestType == "PUT")
resp = RestAssured.given().log().all().headers(headermap).body(reqBody).put(url);

..................
..........
.....
....
..

//Extract the required key value & compare with the expected value.

JsonPath path = new JsonPath(resp);
actualOrderVal = path.getString(validationKey); 
if(actualOrderVal == expOrderVal)
   System.out.println("Test PASS");
else
   System.out.println("Test FAIL");




References:
https://github.com/rest-assured/rest-assured/wiki/GettingStarted
https://github.com/rest-assured/rest-assured/wiki/Downloads

No comments:

Post a Comment