Professional Documents
Culture Documents
Manjyot Singh
Ruchika Rawat
Introduction
[
{
"speaker": {
"id": "007",
"name": "Manjyot Singh",
"role": "QA"
}
},
{
"speaker": {
"id": "001",
"name": "Ruchika Rawat",
"role": "QA"
}
}
]
What is a web service – QA point of view?
● Resources are manipulated using a fixed set of four create, read, update,
delete operations: PUT, GET, POST, and DELETE.
http://localhost:9000/users/1
Let’s talk about...
Why REST-assured ?
● Install JDK
● IDE (Eclipse/Intellij)
● Rest-assured jars
● Hamcrest-matchers jars
● Junit jars
Understanding a Request
Simple GET Request
given().
contentType(“application/json”).
when().
get("/users").
then().
assertThat().
statusCode(HttpStatus.SC_OK);
GET Request
given().
contentType(“application/json”).
when().
get("/users/1").
then().
assertThat().
body("userId", equalTo(1)).
body("userName", equalTo("Robert")).
body("employer", equalTo("facebook")).
body("location.state", equalTo("California")).
body("location.city", equalTo("San Jose"));
POST Request
given().
contentType("application/json").
body("[{\"userName\":\"Jayant2\",\"employer\":\"Google\",\"location\":
{\"state\":\"California\",\"city\":\"Mountain View\"}}]").
when().
post("/users").
then().
assertThat().
body("userName", hasItems("Jayant2"));
PUT Request
int userId = 1;
given().
contentType("application/json").
when().
body("{\"userName\":\"Taylor\"}").
put("/users/" + userId).
then().
statusCode(HttpStatus.SC_OK).
body("userName", equalTo("Taylor"));
DELETE Request
int userId = 9;
given().
when().
delete("/users/" + userId).
then().
statusCode(HttpStatus.SC_OK);
Response parsing
Response response =
given().
contentType(ContentType.JSON).
when().
get("/users/5").
then().
extract().response();
Assert.assertTrue(userName.equals("Steve"));
Assert.assertTrue(userCity.equals("San Francisco"));
Json parsing
String jsonResponse =
get("/users/5").
asString();
Assert.assertTrue(state.equals("California"));
Assert.assertTrue(city.equals("San Francisco"));
Authentication
String sessionToken =
given().
body("{\"userName\" : \"ruchikar\",\"password\" : \"P@ssW0rd\"}").
when().
with().
header("Content-Type", "application/json").
header("X-Forwarded-Proto", "https").
post("/sessionTokens").
then().
statusCode(200).
contentType(ContentType.JSON).
extract().
response().path("response.sessionToken");
contd...
given().
when().
with().
header("X-Forwarded-Proto", "https").
header("Content-Type", "application/json").
header("X-Auth", sessionToken).
get(“/users”).
then().
statusCode(HttpStatus.SC_OK).
contentType(ContentType.JSON);
Other available tools/api
References
Rest-Assured: https://github.com/jayway/rest-assured
Github : https://github.com/jayway/rest-assured/wiki/Usage