GET-request: How to respond with a file-content verbatim, i.e. *exactly* as read from the file
I am trying to mock a REST service that responds with the content of a .CSV (i.e. comma-separated values) file.
This sounds pretty trivial but as turned out misc. character encoding issues make this more complicated as expected.
For the REST-response I am reading the file using a Groovy script as below ($path is set via properties and calculated - not shown here):
... path-calculation omitted...
log.info("reading data from '${path}'")
def file = new File(path)
if (file.exists() && file.isFile()) {
byte[] fileBytes = file.bytes
int length = fileBytes.length
log.info("read ${length} bytes of data: '${fileBytes}'")
String str = new String(fileBytes, java.nio.charset.StandardCharsets.ISO_8859_1)
mockResponse.content = str
} else {
throw new Exception("file '${path}' not found!")
}
This works "almost", i.e. the file's content gets delivered but its character encoding is wrong (i.e. special characters like German Umlauts get garbled).
The issue is that the original file's content is in ISO-8859-1 encoding and it has to be sent "verbatim", i.e. "bit-true" exactly as on the file.
The fileBytes after reading contain the correct bytes values according to the ISO-8859-1 encoding and the String - when logged (not shown here) - is also correct, since it gets converted to Java's internal encoding using the second ISO-8859-1 character-set argument.
However, the conversion from that String to the actually returned bytes is wrong.
The response is configured with a "Content | Media type" header value of "text/csv; charset=ISO-8859-1" which is also correctly sent and received as:
HTTP/1.1 200 OK
Content-Type: text/csv; charset=ISO-8859-1
The delivered character-payload data, however, is obviously not encoded using that specified encoding but something else (I guess UTF-8).
e.g. the string "männlich;8000 Zürich" arrives as "männlich;8000 Zürich" when correctly decoded - as specified in the header as ISO-8859-1 on the receiver side.
I also looked at the received bytes using a Hex-Editor and the Umlauts obviously get encoded as two bytes, not one!
I then tried to assign the original bytes to the mockResponse.content, i.e.
// String str = new String(fileBytes, java.nio.charset.StandardCharsets.ISO_8859_1)
mockResponse.responseContent = fileBytes
but that didn't work. That is then sent out as character array, i.e. as "[99, 114, 101, 97, 116, 101, 100, 59, 97, 114, 98, ..." and thus useless.
So - is there any possibility to convince SOAP-UI to actually encode the response in the character-code that is specified in the HTTP-response header? Or to assign the original fileBytes to the mockResponse's content somehow without byte=>String conversion?