What is API Testing?
API -Application Programming Interface Testing is a software testing practice that tests the APIs directly — from their functionality, reliability, performance, to security. Part of integration testing, API testing effectively validates the logic of the build architecture within a short amount of time. In software application (app) development, API is the middle layer between the presentation (UI) and the database layer. APIs enable communication and data exchange from one software system to another.
API Restaurant Analogy
API Testing Types
Validation Testing
Functional Testing
UI Testing
Security Testing
Load Testing
Runtime and error detection
Penetration Testing
Fuzz Testing
What is Request Body?
A request body is data sent by the client to your API. A response body is the data your API sends to the client. The request body is used to send and receive data via the REST API. If we are using POST/PUT API, then based on the REST API contract, we should send the whole resource information because these methods work on the whole resource.
API Framework
Sample API framework
Request body creation:
We can send payload using the following ways.
Hashmap
org.json
POJO (Plain Old Java Object)
External json file
Example: Sending POST request with the valid URL and valid data
Step 1:Create Feature file for POST request
Scenario: Verify POST request to post data into program module with valid base URL and valid data
Given User sets request for Program module with valid base URL and valid request body
When User sends POST request with valid data
Then Request should be successful with status code "201" for POST request
Note: Gherkin is highlighted since we have not created stepDefinition file and the steps are still unimplemented
Step 2: Create Payload File. This file is required for sending request body using POJO class. Please refer "Scenario THREE"
public class PostProgramPOJO {
String programName;
String programType;
String location;
String studentName;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getProgramName() {
return programName;
}
public void setProgramName(String programName) {
this.programName = programName;
}
public String getProgramType() {
return programType;
}
public void setProgramType(String programType) {
this.programType = programType;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
Step 3: Create json file to send request body using external file. Please refer to "Scenario four"
We have to create a file with extension .json and save it at project level. All the data must be entered only in json format. (Example: Timesheet1.json)
Step 4: Create StepDefinition file and add unimplemented steps
Scenario ONE : Sending Request Body using "HashMap"
@Given("User sets request for Program Module with valid base URL and valid request body")
public void user_sets_request_for_program_module_with_valid_base_url_and_valid_request_body() {
this.uri = Config.PostProgram_URL;
this.request = RestAssured.given().header("Content-Type", "application/json");
logger.info("Request set for Program module with valid base URL and valid data");
}
@When("User sends POST request with valid data")
public void user_sends_post_request_with_valid_data() {
HashMap data = new HashMap();
data.put("studentName", "John");
data.put("programName", "Business Analytics");
data.put("programType", "Geaduate");
data.put("location", "Boston");
response = this.request
.body(data)
.when()
.post(this.uri)
.then().log().all().extract().response();
logger.info("Post request sent with valid data");
}
@Then("Request should be successful with status code {string} for POST request")
public void request_should_be_successful_with_status_code_for_post_request(String statuscode) {
int Poststatuscode = response.getStatusCode();
if (Poststatuscode == 201) {
response.then().statusCode(Integer.parseInt(statuscode));
logger.info("Post Request Successful");
Scenario TWO : Sending Request Body using "org.Json"
@Given("User sets request for Program Module with valid base URL and valid request body")
public void user_sets_request_for_program_module_with_valid_base_url_and_valid_request_body() {
this.uri = Config.PostProgram_URL;
this.request = RestAssured.given().header("Content-Type", "application/json");
logger.info("Request set for Program module with valid base URL and valid data");
}
@When("User sends POST request with valid data")
public void user_sends_post_request_with_valid_data() {
JSONObject body = new JSONObject();
body.put("studentName", "John");
body.put("programName", "Business Analytics");
body.put("programType", "Geaduate");
body.put("location", "Boston");
response = this.request
.body(body.toJSONString())
.when()
.post(this.uri)
.then()
.log().all().extract().response();
}
@Then("Request should be successful with status code {string} for POST request")
public void request_should_be_successful_with_status_code_for_post_request(String statuscode) {
int Poststatuscode = response.getStatusCode();
if (Poststatuscode == 201) {
response.then().statusCode(Integer.parseInt(statuscode));
logger.info("Post Request Successful");
Scenario THREE: Sending Request Body using "POJO (PLAIN OLD JAVA OBJECT") CLASS
@Given("User sets request for Program Module with valid base URL and valid request body")
public void user_sets_request_for_program_module_with_valid_base_url_and_valid_request_body() {
this.uri = Config.PostProgram_URL;
this.request = RestAssured.given().header("Content-Type", "application/json");
logger.info("Request set for Program module with valid base URL and valid data");
}
@When("User sends POST request with valid data")
public void user_sends_post_request_with_valid_data() {
PostProgram data = new PostProgram();
data.setStudentName("John");
data.setProgramName("Business Analytics");
data.setProgramType("Graduate");
data.setLocation("Boston");
response = this.request.body(data).when().post(this.uri).then().log().all().extract().response();
logger.info("Post request sent with valid data");
}
@Then("Request should be successful with status code {string} for POST request")
public void request_should_be_successful_with_status_code_for_post_request(String statuscode) {
int Poststatuscode = response.getStatusCode();
if (Poststatuscode == 201) {
response.then().statusCode(Integer.parseInt(statuscode));
logger.info("Post Request Successful");
Scenario FOUR: Sending Request Body using "External json File"
@Given("User sets request for Program Module with valid base URL and valid request body")
public void user_sets_request_for_program_module_with_valid_base_url_and_valid_request_body() {
this.uri = Config.PostProgram_URL;
this.request = RestAssured.given().header("Content-Type", "application/json");
logger.info("Request set for Program module with valid base URL and valid data");
}
@When("User sends POST request with valid data")
public void user_sends_post_request_with_valid_data() {
File jsonfile = new File("JsonFiles/Timestamp1.json");
response = this.request.body(jsonfile).when().post(this.uri).then().log().all().extract().response();
logger.info("Post request sent with valid data");
}
@Then("Request should be successful with status code {string} for POST request")
public void request_should_be_successful_with_status_code_for_post_request(String statuscode) {
int Poststatuscode = response.getStatusCode();
if (Poststatuscode == 201) {
response.then().statusCode(Integer.parseInt(statuscode));
logger.info("Post Request Successful");
Conclusion:
In this blog, our main focus was to learn about different ways to request body. There are many features which could be incorporated to API testing to make it efficient and time saving. Example: Data Driven testing, various response body validations (header, cookie, status line, json schema), chaining concept, serialization and deserialization. Please leave your valuable comments to improve the blog.
Opmerkingen