首页 > 解决方案 > 使用匹配内容类型为文本/纯文本的响应

问题描述

根据文档 - 响应自动作为 JSON、XML 或 String 对象可用,具体取决于响应内容是什么。

就我而言,我的响应内容类型为 text/plain

My server response is
["a.b.c:must match \"(\\d{4}(0[1-9"]

这适用于我将字符串转换为响应然后验证(我已经转义了所有反斜杠)

 * string responseString = response
 * match responseString contains '["a.b.c match \\"(\\\\d{4}(0[1-9"]'

这失败了。当响应也可以作为字符串使用时,为什么会失败

* match response contains '["a.b.c match \\"(\\\\d{4}(0[1-9"]'

com.intuit.karate.exception.KarateException: 路径: $[*], 实际: ["abc match \"(\d{4}(0[1-9"], 预期: '["abc:must match \ "(\d{4}(0[1-9"]', 原因:实际值不包含预期

标签: karate

解决方案


请将下面的内容粘贴到一个新的场景中,看看它是否适合自己。它应该可以帮助您解决问题:

* string response = '["a.b.c:must match \"(\\d{4}(0[1-9"]'
* match response contains '["a.b.c:must match \"(\\d{4}(0[1-9"]'
* assert response.contains('["a.b.c:must match \"(\\d{4}(0[1-9"]')

推荐阅读