如何使用空手道验证非JSON响应主体 [英] How to validate Non-JSON response body using Karate
问题描述
我正在尝试验证非json格式的错误响应正文.我尝试使用架构以及使用包含匹配的关键字.它对我不起作用.
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
推荐答案
请先了解类型转换:然后将响应转换为字符串,然后再执行 string 包含检查的操作:
Then convert the response to a string before you want to do string contains checks:
* string temp = response
* match temp contains 'error'
这篇关于如何使用空手道验证非JSON响应主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!