How to make sure no extra fields are returned in t

2019-07-20 20:08发布

问题:

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.



标签: karate