italktothewind
italktothewind

Reputation: 2195

Karate js file: org.graalvm.polyglot.PolyglotException: Cannot read the array length because "src" is null

I'm using this util feature:

Feature: Set headers
  Scenario:
    * def authToken = call read('classpath:basic-auth.js') api.credentials

With the javascript basic-auth.js:

function auth(credentials){
    var temp = credentials.username + ':' + credentials.password;
    var Base64 = Java.type('java.util.Base64');
    var encoded = Base64.getEncoder().encodeToString(temp.bytes);
    return 'Basic ' + encoded;
}

That worked fine until I update the Karate version to 1.2.0.RC4 and Java version to 17. Now I'm getting the following exception while running the tests:

org.graalvm.polyglot.PolyglotException: Cannot read the array length because "src" is null
 - java.base/java.util.Base64$Encoder.encode(Base64.java:291)
 - java.base/java.util.Base64$Encoder.encodeToString(Base64.java:345)
 - <js>.auth(Unnamed:4)

Thanks in advance!

Upvotes: 2

Views: 6544

Answers (1)

italktothewind
italktothewind

Reputation: 2195

I fix it replacing:

var encoded = Base64.getEncoder().encodeToString(temp.bytes);

with:

var encoded = Base64.getEncoder().encodeToString(temp.getBytes());

Maybe someone can help me explaining why this works now.

Upvotes: 2

Related Questions