From 6ce22ce1668e3939aa23e5b5a8b9b7eb0808019b Mon Sep 17 00:00:00 2001 From: Peter Thomas Date: Thu, 19 Nov 2020 16:55:04 +0530 Subject: [PATCH] mock bodyPath() should return null not throw exceptions #1365 --- .../com/intuit/karate/core/MockHandler.java | 14 ++++- .../java/com/intuit/karate/core/Variable.java | 4 ++ .../intuit/karate/core/MockHandlerTest.java | 55 ++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java b/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java index f6c6679ad..c3ae68fd8 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java +++ b/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java @@ -273,10 +273,20 @@ public Object bodyPath(String path) { } if (path.startsWith("/")) { Variable v = ScenarioEngine.evalXmlPath(new Variable(body), path); - return JsValue.fromJava(v.getValue()); + if (v.isNotPresent()) { + return null; + } else { + return JsValue.fromJava(v.getValue()); + } } else { Json json = Json.of(body); - return JsValue.fromJava(json.get(path)); + Object result; + try { + result = json.get(path); + } catch (Exception e) { + return null; + } + return JsValue.fromJava(result); } } diff --git a/karate-core/src/main/java/com/intuit/karate/core/Variable.java b/karate-core/src/main/java/com/intuit/karate/core/Variable.java index d96a780c4..e892761b9 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/Variable.java +++ b/karate-core/src/main/java/com/intuit/karate/core/Variable.java @@ -296,6 +296,10 @@ public Variable toLowerCase() { } } + public boolean isNotPresent() { + return "#notpresent".equals(value); + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/karate-core/src/test/java/com/intuit/karate/core/MockHandlerTest.java b/karate-core/src/test/java/com/intuit/karate/core/MockHandlerTest.java index 8ff379ca7..1f56a0fcd 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/MockHandlerTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/MockHandlerTest.java @@ -259,11 +259,64 @@ void testJsVariableInBackground() { background( "def nextId = call read('increment.js')" ).scenario( - "pathMatches('/hello')", "def response = nextId()" + "pathMatches('/hello')", + "def response = nextId()" ); request.path("/hello"); handle(); match(response.getBodyAsString(), "1"); } + + @Test + void testJsonBodyPathThatExists() { + background().scenario( + "pathMatches('/hello')", + "def response = bodyPath('/root/foo')" + ); + request.path("/hello") + .bodyJson("{ root: { foo: 'bar' } }"); + handle(); + match(response.getBodyAsString(), "bar"); + } + + @Test + void testJsonBodyPathThatDoesNotExist() { + background().scenario( + "pathMatches('/hello')", + "def result = bodyPath('root.nope')", + "def response = result == null ? 'NULL' : 'NOTNULL'" + ); + request.path("/hello") + .bodyJson("{ root: { foo: 'bar' } }"); + handle(); + match(response.getBodyAsString(), "NULL"); + } + + @Test + void testXmlBodyPathThatExists() { + background().scenario( + "pathMatches('/hello')", + "def response = bodyPath('/root/foo')" + ); + request.path("/hello") + .body("bar") + .contentType("application/xml"); + handle(); + match(response.getBodyAsString(), "bar"); + } + + @Test + void testXmlBodyPathThatDoesNotExist() { + background().scenario( + "pathMatches('/hello')", + "def result = bodyPath('/root/nope')", + "def response = result == null ? 'NULL' : 'NOTNULL'" + ); + request.path("/hello") + .body("bar") + .contentType("application/xml"); + handle(); + match(response.getBodyAsString(), "NULL"); + } }