api request testing code example
Example 1: what do you test in response api
Basicall we are checking response body
to verify if request matches with response.
In the response we are verifying
(body, status code, header, response time,
test structure of json against the given jsonSchema)
If file not under resources:
File schemaFile = new File("src/test/resources/postSuccessResponseSchema.json");
given()
.spec(adminReqSpec)
.contentType(ContentType.JSON)
.body(abcUtil.getRandomHeroPOJO_Payload()).
when()
.post("/HEROS").
then()
.body(matchesJsonSchema( schemaFile ) )
If schema file under resources:
given()
.spec(adminReqSpec)
.queryParam("nameContains","a")
.queryParam("gender","Female").
when()
.get("/abc/search").
then()
.time( lessThan(2000L));
.body(matchesJsonSchemaInClasspath("searchSpartanSchema.json") )
Example 2: why do you do rest api teting
The purpose of rest api testing is to record the response of rest api
by sending various HTTP/S requests to check if rest api is working fine or not.
Rest api testing is done by GET, POST, PUT and DELETE methods.
Rest stands for Representational State Transfer. It is an architectural style
and an approach for communication used in the development of Web Services.
REST has become a logical choice for building APIs. It enables users
to connect and interact with cloud services efficiently.