Karate API, How do I match 2 different responses

2019-07-29 16:31发布

I get 2 different responses from an endpoint depending on it's state and either one of them is fine.

first response:

{"available":'#boolean',"collection":'#boolean'}

second response:

{"code": "#string","message": "#string"}

I'm trying the following but it's not working:

  • def firstSchema = {"available":'#boolean',"collection":'#boolean'}
  • def secondSchema = {"code": "#string","message": "#string"}

match response contains any (firstSchema, secondSchema)

Any ideas how to best get this working so either response is fine?

Thanks

标签: karate
1条回答
叛逆
2楼-- · 2019-07-29 17:15

Try this:

* def first = { available: true, collection: true }
* def second = { code: 'foo', message: 'bar' }

* def response = second
* def expected = response.code ? { code: '#string', 'message': '#string' } : { available: '#boolean', collection: '#boolean' }
* match response == expected

Also refer to the documentation on "Conditional Logic". You can use JsonPath to "check" what shape the response is and then define expected results.

查看更多
登录 后发表回答