RestAssured Examples





/***
* Constructing a GET Request with Query Params
*
* @param queryParams
* @param requestURL
* @return
* @throws Exception
*/

public String getHttpRequestWithQueryParams(Map queryParams, String requestURL) throws Exception {
    response = given().queryParams(queryParams).when().get(requestURL).then().log().all().assertThat()
 .statusCode(200).extract().asString();
 return response;
}


/***
* Constructing a GET Request with Path Params
*
* @param pathParams
* @param requestURL
* @return
* @throws Exception
*/
public String getHttpRequestWithPathParams(Map pathParams, String requestURL) throws Exception {
response = given().pathParams(pathParams).when().get(requestURL).then().log().all().assertThat().statusCode(200)
.extract().asString();
return response;
}


/***
* Constructing a GET Request with Headers
*
* @param headerDetails
* @param requestURL
* @return
* @throws Exception
*/
public String getHttpRequestWithHeaders(Map headerDetails, String requestURL) throws Exception {
response = given().headers(headerDetails).when().get(requestURL).then().log().all().assertThat().statusCode(200)
.extract().asString();
return response;
}


/***
* Constructing a GET Request with Headers, Query Params,Path Params
*
* @param headerDetails
* @param pathParams
* @param queryParams
* @param requestURL
* @return
* @throws Exception
*/
public String getHttpRequestWithHeadersPathAndQueryParams(Map headerDetails,
Map pathParams, Map queryParams, String requestURL) throws Exception {
response = given().headers(headerDetails).pathParams(pathParams).queryParams(queryParams).when().get(requestURL)
.then().log().all().assertThat().statusCode(200).extract().asString();
return response;
}


// POST Request Type
/***
* Constructing a POST Request with Headers
*
* @param headerDetails
* @param requestBody
* @return
* @throws Exception
*/


public String postHttpRequest(Map headerDetails, String requestBody, String resourceURL)
throws Exception { // response =
//RestAssured.baseURI = "URL";
String response = given().log().all().headers(headerDetails).body(requestBody).when().post(resourceURL).then()
.log().all().assertThat().statusCode(200).extract().response().asString();
return response;
}


/***
* Constructing a POST Request with Headers and Path Params
*
* @param headerDetails
* @param requestBody
* @return
* @throws Exception
*/
public String postHttpRequest(RequestSpecification requestSpec, String resourceURL) throws Exception {
response = requestSpec.when().post(resourceURL)
.then().log().all().assertThat().statusCode(200).extract().response().asString();
return response;
}


/***
* Constructing a POST Request with Headers and Query Params
*
* @param headerDetails
* @param queryParams
* @param requestBody
* @param resourceURL
* @return
* @throws Exception
*/
public String postHttpRequestWithQueryParams(Map headerDetails, Map queryParams,
String requestBody, String resourceURL) throws Exception {
response = given().log().all().headers(headerDetails).queryParams(queryParams)
.body(new String(Files.readAllBytes(Paths.get(requestBody)))).when().post(resourceURL).then().log()
.all().assertThat().statusCode(200).extract().response().asString();
return response;
}
// PUT
public String putHttpRequest(Map headerDetails, String requestBody, String resourceURL)
throws Exception {
response = given().log().all().headers(headerDetails)
.body(new String(Files.readAllBytes(Paths.get(requestBody)))).when().put(resourceURL).then().log().all()
.assertThat().statusCode(200).extract().response().asString();
return response;

}
/***
* Constructing a PUT Request with Headers and Path Params
*
* @param headerDetails
* @param requestBody
* @return
* @throws Exception
*/
public String putHttpRequestWithPathParams(Map headerDetails, Map pathParams,
String requestBody, String resourceURL) throws Exception {
response = given().log().all().headers(headerDetails).pathParams(pathParams)
.body(new String(Files.readAllBytes(Paths.get(requestBody)))).when().put(resourceURL).then().log().all()
.assertThat().statusCode(200).extract().response().asString();
return response;
}


/***
* Constructing a PUT Request with Headers and Query Params
*
* @param headerDetails
* @param queryParams
* @param requestBody
* @param resourceURL
* @return
* @throws Exception
*/
public String putHttpRequestWithQueryParams(Map headerDetails, Map queryParams,
String requestBody, String resourceURL) throws Exception {
response = given().log().all().headers(headerDetails).queryParams(queryParams)
.body(new String(Files.readAllBytes(Paths.get(requestBody)))).when().put(resourceURL).then().log().all()
.assertThat().statusCode(200).extract().response().asString();
return response;
}


/***
* Constructing a PUT Request with Headers, Query and Path Params
*
* @param headerDetails
* @param pathParams
* @param queryParams
* @param requestBody
* @param resourceURL
* @return
* @throws Exception
*/
public String putHttpRequest(Map headerDetails, Map pathParams,
Map queryParams, String requestBody, String resourceURL) throws Exception {
response = given().log().all().headers(headerDetails).pathParams(pathParams).queryParams(queryParams)
.body(new String(Files.readAllBytes(Paths.get(requestBody)))).when().put(resourceURL).then().log().all()
.assertThat().statusCode(200).extract().response().asString();
return response;
}


// DELETE HTTP Request
/*****
* Constructing DELETE Request with Query Params
*
* @param queryParams
* @param requestURL
* @return
* @throws Exception
*/
public String deleteHttpRequestWithQueryParams(Map queryParams, String requestURL)
throws Exception {
response = given().queryParams(queryParams).when().delete(requestURL).then().log().all().assertThat()
.statusCode(200).extract().asString();
return response;
}


/****
* Constructing DELETE Request with Path Params
*
* @param pathParams
* @param requestURL
* @return
* @throws Exception
*/
public String deleteHttpRequestWithPathParams(Map pathParams, String requestURL) throws Exception {
response = given().pathParams(pathParams).when().delete(requestURL).then().log().all().assertThat()
.statusCode(200).extract().asString();
return response;
}
/****
* Constructing DELETE Request with Headers,Path and Query Params
*
* @param headerDetails
* @param pathParams
* @param queryParams
* @param requestURL
* @return
*/
public String deleteHttpRequestWithHeadersQueryAndPathParams(Map headerDetails,
Map pathParams, Map queryParams, String requestURL) {
response = given().headers(headerDetails).pathParams(pathParams).queryParams(queryParams).when()
.delete(requestURL).then().log().all().assertThat().statusCode(200).extract().asString();
return response;
}
/***
* Constructing DELETE Request with Input Payload
*
* @param headerDetails
* @param requestBody
* @param requestURL
* @return
* @throws IOException
*/
public String deleteHttpRequestWithBody(Map headerDetails, String requestBody, String requestURL)
throws IOException {
response = given().headers(headerDetails).body(new String(Files.readAllBytes(Paths.get(requestBody)))).when()
.delete(requestURL).then().log().all().assertThat().statusCode(200).extract().asString();
return response;
}


/**
* Constructing Multipart Request for adding attachments
*
* @param headerDetails
* @param fileLocation
* @param requestURL
* @return
*/
public String addingAttachments(Map headerDetails, String fileLocation, String requestURL) {
response = given().headers(headerDetails).multiPart("file", new File(fileLocation)).when().post(requestURL)
.then().log().all().assertThat().statusCode(200).extract().asString();
return response;
}
/***
* Returns the values of parsed JSON path
*
* @param response
* @param key
* @return
*/
public String getJsonPath(Response response, String key) {
String resp = response.asString();
JsonPath js = new JsonPath(resp);
return js.get(key).toString();
}




No comments:

Post a Comment