1

We have a 404 response that returns an empty response, but when trying to validate that, we get the following error. Is there a suggested way to evaluate a truly empty response?

* if (responseStatus == 404 ) matchResult = karate.match("$ == ''")
js failed:
>>>>
01: if (responseStatus == 404 ) matchResult = karate.match("$ == ''")
<<<<
org.graalvm.polyglot.PolyglotException: json string can not be null or empty
- com.jayway.jsonpath.internal.Utils.notEmpty(Utils.java:401)
- com.jayway.jsonpath.internal.ParseContextImpl.parse(ParseContextImpl.java:36)
- com.jayway.jsonpath.JsonPath.parse(JsonPath.java:647)
- com.intuit.karate.Json.of(Json.java:63)
- com.intuit.karate.core.ScenarioEngine.evalJsonPath(ScenarioEngine.java:2000)
- com.intuit.karate.core.ScenarioEngine.evalJsonPathOnVariableByName(ScenarioEngine.java:2053)
- com.intuit.karate.core.ScenarioEngine.evalKarateExpression(ScenarioEngine.java:2085)

Here is our response, which you can see is empty: empty response error

We have tried the following match test, with no luck.

* if (responseStatus == 404 ) matchResult = karate.match("$ == ''")
* if (responseStatus == 404 ) matchResult = karate.match("$ == []")
* if (responseStatus == 404 ) matchResult = karate.match("$ == '#[0]'")
* if (responseStatus == 404 ) matchResult = karate.match("$ == '#notpresent'")
* if (responseStatus == 404 ) matchResult = karate.match("$ == '#null'")

Reviewed the following github posts, with no luck: Karate Framework : #notnull and #present are not working in case response is empty Karate DSL blank response passes tests

Also tried a simple match, versus a karate match with the above evaluations, but those fail as well.

* match $ == []
json string can not be null or empty

* match $ == {}
json string can not be null or empty

* match $ == '#notpresent'
json string can not be null or empty
mike
  • 383
  • 1
  • 12

1 Answers1

0

Try:

* assert responseBytes.length == 0

Note that an empty response is not JSON so $ will not work. Try:

* match response == ''
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Changing to response == '' worked, but using response == '#notpresent' did not. At least there is a working solution now. Thank you for the clarification on $ (json) versus response (string). – mike May 10 '23 at 19:31
  • @mike great ! why `#notpresent` does not work is also because it applies only to JSON. search the docs for this text: `Note that #present and #notpresent only make sense` ... – Peter Thomas May 10 '23 at 21:03