How to make sure no extra fields are returned in the response

做~自己de王妃 提交于 2019-12-24 00:45:11

问题


My actual api response is

{
"data": {
"0000164": {
"2019-02-11": {
"MAPLE": 5,
"OAK": 15
}
}
},
"request_data": null,
"status": 200
}

This is my expected api response

{
"data": {
"0000164": {
"2019-02-11": {
"MAPLE": 5,
"OAK": 15
}
}
},
"status": 200
}

If you notice I don't want my api response to have "request_data" field or any other field extra other than the expected

How do I make sure that my api is not returning any junk key value pairs

Currently this is how I am validating

And match $.data.0000164.2019-02-11.MAPLE == 5
And match $.data.0000164.2019-02-11.OAK == 15

回答1:


To specifically check that request_data is not present:

* match response.request_data == '#notpresent'

I don't understand your second question, isn't this what Karate does by default:

* match response ==
"""
{
  data: '#object',
  status: '#number'
}
"""

Read the section on fuzzy matching carefully: https://github.com/intuit/karate#fuzzy-matching




回答2:


If you know all the expected keys from the response, you could write expected schema for that and match it as == to stricly validate the expected keys as well.

eg for the above scenario,

{
  "data": {
    "0000164": {
      "2019-02-11": {
        "MAPLE": "#number",
        "OAK": "#number"
      }
    }
  },
  "status": 200
}

There are many other ways like retrieving all the keys from the JSON and look for unexpected keys.



来源:https://stackoverflow.com/questions/53723891/how-to-make-sure-no-extra-fields-are-returned-in-the-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!