//Get Place ---> In get place, there no Json Body, everything will be in URL. and query parameter
public static void main(String[] args) {
given()
.queryParam("page", "2")
.auth().none()
.header("Content-type","application/json")
.contentType(ContentType.JSON)
.when()
.get("https://reqres.in/api/users")
.then()
.statusCode(200)
.body(
"page", equalTo(2),
"per_page", equalTo(6),
"total", equalTo(12),
"total_pages", equalTo(2)
);
System.out.println("successfully executed");
}
Method- 2
Response getResponse =
given().log().all()
.queryParam("page", "2").auth().none()
.header("Content-type","application/json")
.contentType(ContentType.JSON)
.when()
.get("https://reqres.in/api/users");
// getResponse.prettyPrint(); //print response
System.out.println("Response is: "+getResponse.getBody().asString());
System.out.println("Status code is: "+getResponse.statusCode());
System.out.println("Response time is: "+getResponse.getTime());
2. Write a code snipped GET to fetch the information from the server and validate the Request body - In different style
public void getWeatherDetailsWithCorrectCityNameTest(String users){
//1. define the base url
//https://reqres.in/api
RestAssured.baseURI = "//https://reqres.in/api";
//2. define the http request:
RequestSpecification httpRequest = RestAssured.given();
//3. make a request and execute the request:
Response response = httpRequest.request(Method.GET, "/"+users);
//4. get the response body:
String respBody = response.getBody().asString();
System.out.println("Response Body is: "+ respBody );
//validate city name or validate the key or value
Assert.assertEquals(respBody.contains(users), true);
//5. get the status code and validate it:
int statusCode = response.getStatusCode();
System.out.println("the status code is: "+ statusCode);
Assert.assertEquals(statusCode, 200);
System.out.println("the status line is: "+ response.getStatusLine());
//6. get the headers:
Headers headers = response.getHeaders();
System.out.println(headers);
String contentType = response.getHeader("Content-Type");
System.out.println("the value of content-type header is: "+ contentType);
String contentLength = response.getHeader("Content-Length");
System.out.println("the value of Content-Length header is: "+ contentLength);
3. Write a code snipped for POST request and validate the Request body
Method 1:
public class PostAPI {
static String requestPayload = "{\r\n"
+ " \"name\": \"morpheus\",\r\n"
+ " \"job\": \"leader\"\r\n"
+ "}";
public static void main(String[] args) {
given()
.auth().none()
.header("Content-type","application/json")
.contentType(ContentType.JSON)
.when()
.body(requestPayload)
.post("https://reqres.in/api/users")
.then()
.statusCode(201)
.body(
"name", equalTo("morpheus"),
"job", equalTo("leader")
);
}
}
Method 2
public class PostAPI {
static String requestPayload = "{\r\n"
+ " \"name\": \"morpheus\",\r\n"
+ " \"job\": \"leader\"\r\n"
+ "}";
public static void main(String[] args) {
Response getResponse =
given()
.auth().none()
.header("Content-type","application/json")
.contentType(ContentType.JSON)
.when()
.body(requestPayload)
.post("https://reqres.in/api/users");
System.out.println("Response is :"+getResponse.getBody().asString());
System.out.println("Status code is :"+getResponse.statusCode());
System.out.println("Response time is: "+getResponse.getTime());
}
}
4. Write a code snipped for POST request and passing Request body in the form of Map object
Pre-requisites - you need to update update jackson databind dependency
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("name", "morpheus");
map.put("job", "leader");
Response getResponse =
given()
.auth().none()
.header("Content-type","application/json")
.contentType(ContentType.JSON)
.when()
.body(map)
.post("https://reqres.in/api/users");
System.out.println("Response is :"+getResponse.getBody().asString());
System.out.println("Status code is :"+getResponse.statusCode());
System.out.println("Response time is: "+getResponse.getTime());
}
5. Write a code snipped for POST request and passing Request body from external file
public static void main(String[] args) {
Response getResponse =
given()
.auth().none()
.header("Content-type","application/json")
.contentType(ContentType.JSON)
.when()
.body(new File(./PayLoad.json)).log().all()
.post("https://reqres.in/api/users");
System.out.println("Response is :"+getResponse.getBody().asString());
System.out.println("Status code is :"+getResponse.statusCode());
System.out.println("Response time is: "+getResponse.getTime());
}
6. How to do Schema validation in Rest assured?
Steps:
Pre-requisite:
Update pom.json with jsonschema validator dependency
- dependency2. Create schema.json file in project root level
3. Paste generated json schema in schema.json file
4. Write following code snippet to validate the json schema
@Test
public void ValidateJsonSchema()
Response response = given().log().all()