验证特定字符串在响应正文中是否可用
Validating if a Particular String is available in the Response Body
响应数据包含类似这样的内容。
"INBOUND_MESSAGE:
HTTP/1.1 200
内容类型:application/json
传输编码:分块
Date:Fri,2017 年 9 月 22 日 12:18:50 格林威治标准时间
{"errors":{"deviceId":{"message":"Path deviceId
is required.","name":"ValidatorError","properties": {"type":"required","message":"Path {PATH}
is required.","path":"deviceId","value":""},"kind":"required","path":"deviceId","value":"","$isValidatorError":true},"name":{"message" :"Path name
is required.","name":"ValidatorError","properties":{"type":"required","message":"Path {PATH}
is required.", "path":"name","value":""},"kind":"required","path":"name","value":"","$isValidatorError":true}},"_message":"devices validation failed","message":"devices validation failed: deviceId: Path deviceId
is required., name: Path name
is required.","name":"ValidationError"}
"
我需要查看我是否可以针对 $.Message 进行验证并且条件必须是 "deviceId` is required"。我使用了下面的代码,但是测试用例失败了,因为 $.Message
上有多个消息可用
http()
.client(IotDevice)
.receive()
.response(HttpStatus.OK)
.validate("$.message", "Path `deviceId` is required.");
有没有办法在响应消息中搜索预期的字符串。
问候
北京
对JSON路径的澄清
通过使用 JSON路径表达式 $.message
,您 不是 selecting 多个 message
条目,但正是一个在根目录下(你 可以 select 都使用 $..message
,有两个点)。
如果以格式化的方式查看您的 JSON 数据,您可以立即看到最顶部 message
条目的值是 devices validation failed: deviceId: Path deviceId is required., name: Path name is required.
,这似乎是errors
部分中的所有消息。
解决方案
因此,如果您的目标是验证字符串 Path deviceId is required
是最顶层 message
条目的一部分,您可以将以下 Citrus 匹配器与 JSON路径表达式(另请参阅 JSONPath and validation matchers 上的 Citrus 文档):
<message type="json">
<validate>
<json-path expression="$.message" value="@contains('Path deviceId is required')@"/>
</validate>
</message>
格式化JSON
{
"errors": {
"deviceId": {
"message": "Path deviceId is required.",
"name": "ValidatorError",
"properties": {
"type": "required",
"message": "Path {PATH} is required.",
"path": "deviceId",
"value": ""
},
"kind": "required",
"path": "deviceId",
"value": "",
"$isValidatorError": true
},
"name": {
"message": "Path name is required.",
"name": "ValidatorError",
"properties": {
"type": "required",
"message": "Path {PATH} is required.",
"path": "name",
"value": ""
},
"kind": "required",
"path": "name",
"value": "",
"$isValidatorError": true
}
},
"_message": "devices validation failed",
"message": "devices validation failed: deviceId: Path deviceId is required., name: Path name is required.",
"name": "ValidationError"
}
响应数据包含类似这样的内容。 "INBOUND_MESSAGE:
HTTP/1.1 200 内容类型:application/json 传输编码:分块 Date:Fri,2017 年 9 月 22 日 12:18:50 格林威治标准时间
{"errors":{"deviceId":{"message":"Path deviceId
is required.","name":"ValidatorError","properties": {"type":"required","message":"Path {PATH}
is required.","path":"deviceId","value":""},"kind":"required","path":"deviceId","value":"","$isValidatorError":true},"name":{"message" :"Path name
is required.","name":"ValidatorError","properties":{"type":"required","message":"Path {PATH}
is required.", "path":"name","value":""},"kind":"required","path":"name","value":"","$isValidatorError":true}},"_message":"devices validation failed","message":"devices validation failed: deviceId: Path deviceId
is required., name: Path name
is required.","name":"ValidationError"}
"
我需要查看我是否可以针对 $.Message 进行验证并且条件必须是 "deviceId` is required"。我使用了下面的代码,但是测试用例失败了,因为 $.Message
上有多个消息可用 http()
.client(IotDevice)
.receive()
.response(HttpStatus.OK)
.validate("$.message", "Path `deviceId` is required.");
有没有办法在响应消息中搜索预期的字符串。
问候 北京
对JSON路径的澄清
通过使用 JSON路径表达式 $.message
,您 不是 selecting 多个 message
条目,但正是一个在根目录下(你 可以 select 都使用 $..message
,有两个点)。
如果以格式化的方式查看您的 JSON 数据,您可以立即看到最顶部 message
条目的值是 devices validation failed: deviceId: Path deviceId is required., name: Path name is required.
,这似乎是errors
部分中的所有消息。
解决方案
因此,如果您的目标是验证字符串 Path deviceId is required
是最顶层 message
条目的一部分,您可以将以下 Citrus 匹配器与 JSON路径表达式(另请参阅 JSONPath and validation matchers 上的 Citrus 文档):
<message type="json">
<validate>
<json-path expression="$.message" value="@contains('Path deviceId is required')@"/>
</validate>
</message>
格式化JSON
{
"errors": {
"deviceId": {
"message": "Path deviceId is required.",
"name": "ValidatorError",
"properties": {
"type": "required",
"message": "Path {PATH} is required.",
"path": "deviceId",
"value": ""
},
"kind": "required",
"path": "deviceId",
"value": "",
"$isValidatorError": true
},
"name": {
"message": "Path name is required.",
"name": "ValidatorError",
"properties": {
"type": "required",
"message": "Path {PATH} is required.",
"path": "name",
"value": ""
},
"kind": "required",
"path": "name",
"value": "",
"$isValidatorError": true
}
},
"_message": "devices validation failed",
"message": "devices validation failed: deviceId: Path deviceId is required., name: Path name is required.",
"name": "ValidationError"
}