question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conversion of byte array to a string (StepDefs#convertResponseBody(byte[]) German ISO-8859-15

See original GitHub issue

Hi,

I am using this awesome framework in version 0.4.3 to test a soap webservice. The webservice contains data in German language, also names which contain umlauts like ‘ü’ or ‘ö’.

My tests run into an exception:

java.lang.RuntimeException: cannot convert to map: [type: INPUT_STREAM, value: java.io.ByteArrayInputStream@6e530ccc]

I was debugging for the cause and I found a candidate, the method convertResponseBody(byte[]) in class StepDefs. This method tries to convert a byte array. For soap data without umlauts this methods returns a raw string which works fine. But for data with umlauts it returns a ByteArrayInputStream. Later down the execution path the method getAsMap() of class ScriptValue throws a runtime exception because the value type ByteArrayInputStream is unknown.

I think the error cause is that the comparison of the byte array given by the input parameter and the byte array of the converted string is erroneous. It is this line:

if (Arrays.equals(bytes, rawString.getBytes())) {

The comparison of the byte arrays with Array#equals is not working when the string contains umlauts. I checked my assumption with this code:

 String mueller = "Müller";
 final byte[] bytes = mueller.getBytes();
 final byte[] bytesUtf8 = mueller.getBytes("utf-8");
 String rawString = new String(bytes, "utf-8");
 System.out.println("bytes Length: " + bytes.length);
 System.out.println("bytesUtf8 Length: " + bytesUtf8.length);
 System.out.println("rawString.getBytes() Length: " + rawString.getBytes().length);
 System.out.println();
 System.out.println("Arrays.equals(bytes, rawString.getBytes()): " + Arrays.equals(bytes, rawString.getBytes()));
 System.out.println("Arrays.equals(bytesUtf8, rawString.getBytes()): " + Arrays.equals(bytesUtf8, rawString.getBytes()));
 System.out.println("Arrays.equals(bytes, bytesUtf8): " + Arrays.equals(bytes, bytesUtf8));

The comparison of strings with umlauts by the method Arrays#equals is not working as expected. If the char ‘ü’ is removed then the comparison works fine.

Best regards, Georgios.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
GeorgiosAndreadakiscommented, Sep 15, 2017

Setting the file encoding to UTF-8 led to the desired result. The tests went green.

0reactions
ptrthomascommented, Sep 15, 2017

wow. that is great to know ! thanks @GeorgiosAndreadakis for the update.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found