Skip to content

Instantly share code, notes, and snippets.

@shankybnl
Last active June 6, 2018 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankybnl/efda87252b127d7495e4fabf6f7c18dd to your computer and use it in GitHub Desktop.
Save shankybnl/efda87252b127d7495e4fabf6f7c18dd to your computer and use it in GitHub Desktop.
RestUtil.java
public class RestUtil {
Map<String, String> getRequestHeaders;
Map<String, String> getRequestParameters;
HashMap<String, String> postRequestHeaders;
HashMap<String, String> postRequestParameters;
RequestSpecification baseSpec;
public RestUtil() throws Exception {
this.getRequestHeaders = new HashMap();
this.getRequestParameters = new HashMap();
this.postRequestHeaders = new HashMap();
this.postRequestParameters = new HashMap();
}
public RestUtil(RequestSpecification baseSpec) {
this.baseSpec = baseSpec;
}
public Response getRequest(String getPath, Map<String, String> values, Map<String, String> headers) {
Response jsonResponse = null;
try {
if (this.baseSpec == null) {
jsonResponse = (Response)RestAssured.given().parameters(values).headers(headers).when().get(getPath, new Object[0]);
} else {
jsonResponse = (Response)RestAssured.given().spec(this.baseSpec).parameters(values).headers(headers).when().get(getPath, new Object[0]);
}
} catch (Exception var6) {
LogUtil.logError(this.getClass().getName(), "getRequest method", "Unable to send get request method");
LogUtil.info("Exception occured:" + var6.getMessage());
}
LogUtil.info("getRequest response: " + jsonResponse.asString());
return jsonResponse;
}
public Response postRequest(String getPath, Map<String, String> values, Map<String, String> headers) {
System.out.println(getPath);
Response jsonResponse = null;
try {
if (this.baseSpec == null) {
jsonResponse = (Response)RestAssured.given().parameters(values).headers(headers).when().post(getPath, new Object[0]);
} else {
jsonResponse = (Response)RestAssured.given().spec(this.baseSpec).parameters(values).headers(headers).when().post(getPath, new Object[0]);
}
} catch (Exception var6) {
LogUtil.logError(this.getClass().getName(), "postRequest method", "Unable to send post request");
LogUtil.info("Exception occured:" + var6.getMessage());
}
LogUtil.info("postResponse: " + jsonResponse.asString());
return jsonResponse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment