The function summary and basic principles of the REST ASSURD framework
The REST ASSURD framework is a Java library for testing Restful API.It provides a set of easy -to -use methods and functions that facilitate developers to write simple, readable and easy -to -maintain API test code.This article will introduce the function summary and basic principles of the REST ASSURED framework, and provide some Java code examples.
Functional Overview:
1. Send HTTP request: The REST ASSURED framework can send various types of HTTP requests, such as Get, Post, PUT, Delete, etc., and supporting multiple data formats, such as JSON and XML.
2. Verification response: You can use the REST Assured framework to verify the status code, response header information, response body, etc. of HTTP response.It provides a simple and powerful way of assertion, and can easily write verification logic through chain calls.
3. Parameterization request: The REST ASSURD framework allows transmitting dynamic parameters in the request to simulate different scenarios and data.It supports passing parameters by query parameters, path parameters, request body and other methods, and provides corresponding methods to set these parameters.
4. Chain calls: The method and function of the REST ASSURD framework can be used in a combination of chain calls to achieve more concise and readable code writing.This method makes the writing logic more flexible and convenient.
5. Support HTTP authentication and authorization: The REST ASSURD framework provides various methods to handle HTTP certification and authorization, such as basic certification, abstract certification, OAUTH, etc.Developers can easily add certification information to the request to verify the security and authority of API.
Fundamental:
The basic principle of the REST assured framework is based on a language called Gherkin.It uses a DSL (specific language) to describe the API test scene and verification logic.
The REST ASSURD framework makes the test code more concise and easy to write the test code by using static import and chain call.In this way, developers can use grammar similar to natural language to describe the results of test scenes and expectations.
The following is an example code that uses the REST ASSURD framework to get a GET request and verify the response:
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class APITestExample {
public void testGETRequest() {
given()
.baseUri("http://api.example.com")
.contentType("application/json")
.when()
.get("/users")
.then()
.statusCode(200)
.body("size()", equalTo(5))
.body("users[0].name", equalTo("John Doe"));
}
public static void main(String[] args) {
APITestExample testExample = new APITestExample();
testExample.testGETRequest();
}
}
In the above example, we first introduced the method and assertion of the Rest Assured framework with static introduction.Then, the request's basic URI and the request header information is set in the given block.Next, the GET request was performed in the block () `block.Finally, use an assertion to verify the response status code and the content of the response in the `THEN ()` block.
Through the REST ASSURD framework, we can easily write and perform a variety of different types of API tests to verify the correctness and robustness of the API.