如何使用空手道验证非 JSON 响应主体 [英] How to validate Non-JSON response body using Karate
问题描述
我正在尝试验证非 json 格式的错误响应正文.我尝试使用架构以及使用 contains 匹配的关键字.它对我不起作用.
I am trying to validate an error response body of non-json format. I have tried using the schema as well as a keyword using contains to match. It is not working for me.
在后调用中观察到多个匹配时从 API 返回的实际响应响应:
Actual Response response returned from API when multiple matches are observed in a post call:
{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'NewEmp' for key 'employee_name_unique'}}
我的空手道特色:
Given url 'http://dummy.restapiexample.com/api/v1/create'
def errResp = "error"
And request {"name":"Hello1","salary":"123","age":"23"}
When method POST
Then status 200
Then match $ contains error #not working
Then match $ contains errResp #not working
尝试 * 匹配实际 [0] == '#(^partSchema)' 也来自 https:///gist.github.com/ptrthomas/2a1e30bcb4d782279019b3d5c10b3ed1.不工作
Tried * match actual[0] == '#(^partSchema)' too from https://gist.github.com/ptrthomas/2a1e30bcb4d782279019b3d5c10b3ed1 . Not working
推荐答案
请先了解类型转换:https://github.com/intuit/karate#type-conversion
然后将响应转换为字符串,然后再进行字符串包含检查:
Then convert the response to a string before you want to do string contains checks:
* string temp = response
* match temp contains 'error'
这篇关于如何使用空手道验证非 JSON 响应主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!