目录
代码依赖
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
有用的网址
Postman Echo | Published Postman Templates | Postman API Network
Get请求
@Test
public void testGetMethod() {
String url = "https://postman-echo.com/get";
Map<String, String> paramMap = new HashMap<>();
paramMap.put("foo1", "bar1");
paramMap.put("foo2", "bar2");
Response response = given().
queryParams(paramMap).
get(url);
JsonPath jsonPath = response.jsonPath();
String param1 = jsonPath.get("args.foo1");
String param2 = jsonPath.get("args.foo2");
Assert.assertEquals(param1, paramMap.get("foo1"));
Assert.assertEquals(param2, paramMap.get("foo2"));
}
Post请求
@Test
public void testPostMethod() {
String url = "https://postman-echo.com/post";
Map<String, String> paramMap = new HashMap<>();
paramMap.put("foo1", "bar1");
paramMap.put("foo2", "bar2");
Response response = given().
body(paramMap).
post(url);
Assert.assertEquals(response.statusCode(), 200);
}
Put请求
@Test
public void testPutMethod() {
String url = "https://postman-echo.com/put";
String requestBody = "This is expected to be sent back as part of response body.";
Response response = given().
body(requestBody).
put(url);
JsonPath jsonPath = response.jsonPath();
String data = jsonPath.get("data");
Assert.assertEquals(data, requestBody);
}
Delete请求
@Test
public void testDeleteMethod() {
String url = "https://postman-echo.com/delete";
String requestBody = "This is expected to be sent back as part of response body.";
Response response = given().
body(requestBody).
delete(url);
JsonPath jsonPath = response.jsonPath();
String data = jsonPath.get("data");
Assert.assertEquals(data, requestBody);
}