Skip to content

Commit

Permalink
Additional fix for karatelabs#1558 allowing access to Java classes wi…
Browse files Browse the repository at this point in the history
…th callSingle() in karate-config.js
  • Loading branch information
joelpramos committed Apr 18, 2021
1 parent bbecea4 commit fdd4a3b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3913,7 +3913,7 @@ Refer to this example:

You *can* use `karate.callSingle()` directly in a `*.feature` file, but it logically fits better in the global "bootstrap". Ideally it should return "pure JSON" and note that you always get a "deep clone" of the cached result object.

IMPORTANT: There are some restrictions when using `karate.callSingle()` especially within [`karate-config.js`](#karate-configjs). Ideally you should return only *pure* JSON data (or a primitive string, number etc.). Keep in mind that the reason this exists is to "cache" data, and *not* behavior. So if you return complex objects such as a custom Java instance or a JS function that depends on complex objects, this [will cause issues when you run in parallel](https://github.com/intuit/karate/issues/1558).
IMPORTANT: There are some restrictions when using `karate.callSingle()` especially within [`karate-config.js`](#karate-configjs). Ideally you should return only *pure* JSON data (or a primitive string, number etc.). Keep in mind that the reason this exists is to "cache" data, and *not* behavior. So if you return complex objects such as a custom Java instance or a JS function that depends on complex objects, this [might cause issues when you run in parallel](https://github.com/intuit/karate/issues/1558).

#### `configure callSingleCache`
When re-running tests in development mode and when your test suite depends on say an `Authorization` header set by [`karate.callSingle()`](#karatecallsingle), you can cache the results locally to a file, which is very convenient when your "auth token" is valid for a period of a few minutes - which typically is the case. This means that as long as the token "on file" is valid, you can save time by not having to make the one or two HTTP calls needed to "sign-in" or create "throw-away" users in your SSO store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,8 @@ private Object recurseAndAttach(Object o, Set<Object> seen) {
return attach(value);
}
} catch (Exception e) {
logger.warn("failed to re-attach graal value: {}", e.getMessage());
logger.warn("failed to re-attach graal value and/or is not executable. Will try to use as host object: {}", e.getMessage());
return Value.asValue(value.asHostObject());
}
return null;
} else if (o instanceof JsFunction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function fn() {
var result = karate.callSingle('call-single-from-config.feature', config);
config.message = result.response.message;
// this will throw the [Multi threaded access requested by thread xxx but is not allowed for language(s) js.] error
// config.Hello = result.Hello;
config.Hello = result.Hello;
var result2 = karate.callSingle('call-single-from-config2.feature', result);
config.message2 = result2.message;
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ Background:
# background http builder should work even for a dynamic scenario outline
* url serverUrl
* def data = [ { name: 'value1' }, { name: 'value2' }, { name: 'value3' }, { name: 'value4' } ]
* def helloClass = Hello

Scenario Outline:
* match functionFromKarateBase() == 'fromKarateBase'
* path 'fromfeature'
* method get
* status 200
* match response == { message: 'from feature' }
* match helloClass.sayHello('from the other side') == 'hello from the other side'
* match helloClass.sayHello(name) == 'hello ' + name

Examples:
| data |
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Scenario: one
* match response == { one: '#string' }
* def result = karate.callSingle('call-single-from-feature.feature')
* match result.response == { message: 'from feature' }
* match Hello.sayHello('Bob') == 'hello Bob'

Scenario: two
* path 'two'
Expand Down

0 comments on commit fdd4a3b

Please sign in to comment.