REST Assured 系列汇总 之 REST Assured 67 - How To Assert Full Response JSON Body In Rest Assured?
介绍
如果 response 是某种不变的或小量内容的,那我们就可以直接断言整个 response,就不用将 response 反序列化成某 Java objects,再提取一些值进行匹配断言。
本文我们来交接一下怎样断言整个 response。
前提条件
添加 rest assured 依赖库
<!-- REST Assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
</dependency>
断言整个 response body
需要用到 Hamcrest Matchers 和 body() 方法,以 GoRest APIs 为例。注意这些例子可能会有些变化,因为是公用的 APIs,如果想尝试,请按最新的 API 调用结果为准做些适当的调整。
import org.hamcrest.Matchers;
import io.restassured.RestAssured;
public class AssertFullResponseBody {
public static void main(String[] args) {
RestAssured
// Construct request
.given()
.log()
.all()
.baseUri("https://gorest.co.in/public-api/")
.basePath("users/{id}")
.pathParam("id", "2833")
// Hit API
.when()
.get()
.then()
// Assert response
.log()
.all()
.assertThat()
// Pass full expected response body with Hamcrest matchers
.body(Matchers.equalTo("{\"code\":200,\"meta\":null,\"data\":{\"id\":2833,\"name\":\"Kathline Villanveva\",\"email\":\"Kathline.Villanveva20040@test.com\",\"gender\":\"male\",\"status\":\"inactive\"}}"));
}
}
需要将期望的 JSON body 用工具格式 formatted JSON in to single line 转换成一行,传给 equalTo() 方法。如果不格式化一行,一些回车符号 (\r\n) 存在会影响验证。
输出:
Request method: GET
Request URI: https://gorest.co.in/public-api/users/2833
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: id=2833
Headers: Accept=*/*
Cookies: <none>
Multiparts: <none>
Body: <none>
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 05 Oct 2021 02:29:15 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
ETag: W/"318d7babd5dd7da3e542ed246f2de5f2"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: b6a8a468-263e-4b4f-bad4-77557340209b
X-Runtime: 0.004501
Strict-Transport-Security: max-age=63072000; includeSubDomains
Vary: Origin
Content-Encoding: gzip
{
"code": 200,
"meta": null,
"data": {
"id": 2833,
"name": "Kathline Villanveva",
"email": "Kathline.Villanveva20040@test.com",
"gender": "male",
"status": "inactive"
}
}
从外部文件断言整个 response body
将期望的 response body 存储在一个外部 JSON 文件:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.hamcrest.Matchers;
import io.restassured.RestAssured;
public class AssertFullResponseBodyFromExternalFile {
public static void main(String[] args) throws IOException {
RestAssured
// Construct request
.given()
.log()
.all()
.baseUri("https://gorest.co.in/public-api/")
.basePath("users/{id}")
.pathParam("id", "2833")
// Hit API
.when()
.get()
.then()
// Assert response
.log()
.all()
.assertThat()
// Pass full expected response body with Hamcrest matchers
.body(Matchers.equalTo(new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\src\\test\\resources\\UserDetails2833.json")))));
}
}
输出:
Request method: GET
Request URI: https://gorest.co.in/public-api/users/2833
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: id=2833
Headers: Accept=*/*
Cookies: <none>
Multiparts: <none>
Body: <none>
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 05 Oct 2021 02:48:47 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: strict-origin-when-cross-origin
ETag: W/"318d7babd5dd7da3e542ed246f2de5f2"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: cbd518be-3056-4c62-a20b-f2892a4dca6e
X-Runtime: 0.004087
Strict-Transport-Security: max-age=63072000; includeSubDomains
Vary: Origin
Content-Encoding: gzip
{
"code": 200,
"meta": null,
"data": {
"id": 2833,
"name": "Kathline Villanveva",
"email": "Kathline.Villanveva20040@test.com",
"gender": "male",
"status": "inactive"
}
}