diff --git a/src/main/java/io/carbynestack/httpclient/CsResponseHandler.java b/src/main/java/io/carbynestack/httpclient/CsResponseHandler.java index 52dffb1..51d38e2 100644 --- a/src/main/java/io/carbynestack/httpclient/CsResponseHandler.java +++ b/src/main/java/io/carbynestack/httpclient/CsResponseHandler.java @@ -36,14 +36,21 @@ public static CsResponseHandler of( public CsResponseEntity handleResponse(HttpResponse response) throws IOException { int status = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); - String contentString = entity != null ? EntityUtils.toString(entity) : null; + if (status >= 200 && status < 300) { + if (this.successType.equals(byte[].class)) { + // for TupleLists serialized as Bytes + byte[] contentByteArray = entity != null ? EntityUtils.toByteArray(entity) : null; + return (CsResponseEntity) CsResponseEntity.success(status, contentByteArray); + } + String contentString = entity != null ? EntityUtils.toString(entity) : null; return CsResponseEntity.success( status, contentString != null && !contentString.isEmpty() ? OBJECT_MAPPER.readValue(contentString, successType) : null); } else { + String contentString = entity != null ? EntityUtils.toString(entity) : null; log.debug(String.format("Request failed with status code <%s>: %s", status, contentString)); return CsResponseEntity.failed( status, diff --git a/src/test/java/io/carbynestack/httpclient/CsResponseHandlerTest.java b/src/test/java/io/carbynestack/httpclient/CsResponseHandlerTest.java index 254289b..7c5d9ad 100644 --- a/src/test/java/io/carbynestack/httpclient/CsResponseHandlerTest.java +++ b/src/test/java/io/carbynestack/httpclient/CsResponseHandlerTest.java @@ -29,6 +29,8 @@ public class CsResponseHandlerTest { private static final Random RANDOM = new Random(); private static final CsResponseHandler RESPONSE_HANDLER = CsResponseHandler.of(String.class, String.class); + private static final CsResponseHandler OCTET_RESPONSE_HANDLER = + CsResponseHandler.of(String.class, byte[].class); private static HttpResponse getHttpResponseForObject(int httpStatus, Object obj) { StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, httpStatus, null); @@ -48,6 +50,16 @@ private static HttpResponse getHttpResponseForObject(int httpStatus, Object obj) return response; } + private static HttpResponse getHttpResponseForByteArr(int httpStatus, byte[] bytes) { + StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, httpStatus, null); + HttpResponse response = new BasicHttpResponse(statusLine); + BasicHttpEntity httpEntity = new BasicHttpEntity(); + httpEntity.setContent(new ByteArrayInputStream(bytes)); + httpEntity.setContentLength(bytes.length); + response.setEntity(httpEntity); + return response; + } + @SuppressWarnings("unchecked") private static String writeObjectAsJsonString(Object obj) throws E { try { @@ -57,6 +69,17 @@ private static String writeObjectAsJsonString(Object obj) } } + @Test + public void givenSuccessfulRequestForTupleList_whenHandlingResponse_thenReturnSuccesful() + throws IOException { + byte[] content = new byte[] {1, 2, 3, 5, 3, 5, 6}; + int httpStatus = HttpStatus.SC_OK; + HttpResponse httpResponse = getHttpResponseForByteArr(httpStatus, content); + CsResponseEntity actualResponse = + OCTET_RESPONSE_HANDLER.handleResponse(httpResponse); + assertThat(actualResponse.get()).isEqualTo(content); + } + @Test public void givenSuccessfulRequestWithValidBody_whenHandlingResponse_thenReturnSuccessful() throws IOException {