imbo / behat-api-extension

API extension for Behat, used to ease testing of JSON-based APIs
MIT License
107 stars 42 forks source link

jsonDecode is assuming that everything is an array #116

Open garak opened 2 years ago

garak commented 2 years ago

jsonDecode method assumes that every value decoded from a JSON string is an array. This is a wrong assumption since the value can also be a string, a boolean, or null. See also PHP Manual

christeredvartsen commented 2 years ago

Could you provide an example where this piece of code breaks because of this assumption?

garak commented 2 years ago

For example, when the JSON string passed is "OK"

christeredvartsen commented 2 years ago

Do you have a feature file that I can try out so I can see the example with some context?

garak commented 2 years ago

Not at the moment. Anyway, you can simply have an endpoint returning "OK" and assertion that the JSON response is that string

christeredvartsen commented 2 years ago

For values other than arrays you could simply use Then the response body is: <PyStringNode> instead though:

https://behat-api-extension.readthedocs.io/en/latest/guide/verify-server-response.html#then-the-response-body-is-pystringnode

garak commented 2 years ago

That would prevent me from validating that the content is a proper JSON string

christeredvartsen commented 2 years ago

You could probably do that quite easy with a custom check.

I don't personally see the benefit of fixing the existing code for this, but if you want to provide a PR I would most likely merge it.