使用空手道进行JSON API响应匹配

时间:2018-09-11 11:58:19

标签: karate

我需要使用空手道匹配api的GET响应。 虽然我可以使用单个匹配来做到这一点,但它正成为一大堆代码。我希望减少代码行。 PFB响应匹配的工作方式。

获取API响应是

{
"address": {
    "city": "Warsaw",
    "street": "1212 Main St",
    "postalCode": "22-333"
},
"dateOfBirth": "1996-09-08T00:00:00+0000",
"email": "auth@mail.com",
"id": "123456",
"givenName": "Clap",
"mobilePhone": "23456778787",
"familyName": "Customer"
}

我可以使用以下

管理验证
And match header vary == 'origin' 
And match header Content-Type == 'application/json; charset=utf-8' 
And match header access-control-expose-headers == 'WWW-Authenticate,Server-Authorization' 
And match header cache-control == 'no-cache' 
And match header accept-ranges == 'bytes' 
And match header Content-Length == '225' 

And match $.dateOfBirth == '1996-09-08T00:00:00+0000' 
And match $.email == 'auth@mail.com' 
And match $.id == '123456' 
And match $.givenName == 'Clap' 
And match $.mobilePhone == '23456778787' 
And match $.familyName == 'Customer' 

And match response.address.city contains 'Warsaw' 
And match response.address.street contains '1212 Main St' 
And match response.address.postalCode contains '22-333' 

我已经尝试了给出的一些方法,但对验证没有帮助。

1 个答案:

答案 0 :(得分:0)

阅读文档。您可以在一行中匹配响应JSON:

* match response ==
"""
{
"address": {
    "city": "Warsaw",
    "street": "1212 Main St",
    "postalCode": "22-333"
},
"dateOfBirth": "1996-09-08T00:00:00+0000",
"email": "auth@mail.com",
"id": "123456",
"givenName": "Clap",
"mobilePhone": "23456778787",
"familyName": "Customer"
}
"""

通常,您不需要关注标题,因为标题会根据服务器,操作系统等的类型而变化。也许某些测试可以完成您的工作,但并非全部。