本文中将展示如何使用 REST Assured 框架发送 API 请求。例子中包含了 GET,POST,PUT,PATCH 和 DELETE 格式的请求。
创建一个 maven 项目 rest-assured-examples,并在 pom.xml 中添加 REST Assured 和 Junit 的依赖,如下代码所示:
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
GET 请求是用来向服务器获取资源的。
接下来的例子将会使用 REST Assured 的 get()
方法实现 GET 请求。
在 test 包下创建一个 RestAssuredGetRequest 类,用来发送不带参数 GET 请求,代码如下:
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
public class RestAssuredRequest {
@BeforeAll
public static void setup(){
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}
@Test
public void getRequest(){
Response response = given()
.contentType(ContentType.JSON)
.when()
.get("/posts")
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals("qui est esse", response.jsonPath().getString("title[1]"));
}
}
在 GET 请求中发送数据,可以使用 query()
方法,在 RestAssuredGetRequest 类中添加 getRequestWithQueryParam 方法,代码如下:
@Test
public void getRequestWithQueryParam(){
Response response = given()
.contentType(ContentType.JSON)
.param("postId","2")
.when()
.get("/comments")
.then()
.extract()
.response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals("Meghan_Littel@rene.us",response.jsonPath().getString("email[3]"));
}
在浏览器中访问 https://jsonplaceholder.typicode.com/comments?postId=2 页面显示结果如下:
代码 response.jsonPath().getString("email[3]"
其实就是获取第 4 个 post 中的 email 字段的内容,通过与 Assertions 中提供的期望值进行比较。
执行测试,控制台显示结果如下:
POST 格式请求常用于往服务端发送数据或者创建一个资源。
在 REST Assured 中发送一个 POST 请求,这里使用 post()
方法。
新创建一个 RestAssuredPostRequest 类,添加一个 requestBody 作为 POST 请求的请求体,再添加一个 postRequest 方法用于发送 POST 请求。
public class RestAssuredPostRequest {
@BeforeAll
public static void setup(){
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}
private static String requestBody = "{\n" +
" \"title\": \"foo\",\n" +
" \"body\": \"bar\",\n" +
" \"userId\": \"1\" \n}";
@Test
public void postRequest(){
Response response = given()
.header("Content-type", "application/json")
.and()
.body(requestBody)
.when()
.post("/posts")
.then()
.extract().response();
Assertions.assertEquals(201, response.statusCode());
Assertions.assertEquals("foo", response.jsonPath().getString("title"));
Assertions.assertEquals("bar", response.jsonPath().getString("body"));
Assertions.assertEquals("1", response.jsonPath().getString("userId"));
Assertions.assertEquals("101", response.jsonPath().getString("id"));
}
}
执行测试,测试结果如下:
PUT 请求用更新资源,PUT 请求要求传递一个 JSON 请求体。
REST Assured 中发送 PUT 格式请求需要使用 put()
方法。
新创建一个 RestAssuredPutRequest 类,添加一个 requestBody 作为 PUT 请求的请求体,这个 requestBody 中只包含更新的内容,再添加一个 putRequest 方法用于发送 PUT 请求。
public class RestAssuredPutRequest {
@BeforeAll
public static void setup(){
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}
private static String requestBody = "{\n" +
" \"title\": \"foo\",\n" +
" \"body\": \"baz\",\n" +
" \"userId\": \"1\",\n" +
" \"id\": \"1\" \n}";
@Test
public void putRequest(){
Response response = given()
.header("Content-type", "application/json")
.and()
.body(requestBody)
.when()
.put("/posts/1")
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals("foo", response.jsonPath().getString("title"));
Assertions.assertEquals("baz", response.jsonPath().getString("body"));
Assertions.assertEquals("1", response.jsonPath().getString("userId"));
Assertions.assertEquals("1", response.jsonPath().getString("id"));
}
}
执行测试,测试结果如下图所示:
PATCH 请求也用于更新资源,但只需要有请求 body 中正在更新的字段即可。
新创建一个 RestAssuredPatchRequest 类,添加一个 requestBody 作为 Patch 请求的请求体,这个 requestBody 中只包含更新的内容即可,再添加一个 patchRequest 方法用于发送 Patch 请求。
public class RestAssuredPatchRequest {
@BeforeAll
public static void setup(){
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}
private static String requestBody = "{\n" +
" \"title\": \"bax\",\n";
@Test
public void putRequest(){
Response response = given()
.header("Content-type", "application/json")
.and()
.body(requestBody)
.when()
.patch("/posts/1")
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals("bax", response.jsonPath().getString("body"));
Assertions.assertEquals("1", response.jsonPath().getString("userId"));
Assertions.assertEquals("1", response.jsonPath().getString("id"));
}
}
执行测试用例,显示结果如下:
DELETE 请求可以删除服务端的资源。
REST Assured 中发送 DELETE 请求可以使用到 delete()
方法。
新创建一个 RestAssuredDeleteRequest 类,再添加一个 deleteRequest 方法用于发送 DELETE 请求。
public class RestAssuredDeleteRequest {
@BeforeAll
public static void setup(){
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
}
@Test
public void putRequest(){
Response response = given()
.header("Content-type", "application/json")
.and()
.when()
.put("/posts/1")
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
}
}
执行测试用例,显示结果如下: