InputStreamDelegateDecoder implements Decoder { @Override public Object decode(Response response, Type type) throws IOException, FeignException { if (type.equals(InputStream.class)) { byte[] body = response.body() != null ? Util.toByteArray(response.body().asInputStream()) : new byte[0]; return new ByteArrayInputStream(body); } else { return delegate.decode(response, type); } } InputStreamDelegateDecoder(Decoder delegate); @Override Object decode(Response response, Type type);  }
@Test public void testDecodesAsInputStream() throws Exception { String data = "data"; Response response = Response.create(200, "OK", ImmutableMap.of(), data, StandardCharsets.UTF_8); InputStream decoded = (InputStream) inputStreamDelegateDecoder.decode(response, InputStream.class); assertThat(new String(Util.toByteArray(decoded), StandardCharsets.UTF_8)) .isEqualTo(data); }
@Test public void testUsesDelegateWhenReturnTypeNotInputStream() throws Exception { String returned = "string"; when(delegate.decode(any(), any())).thenReturn(returned); Response response = Response.create(200, "OK", ImmutableMap.of(), returned, StandardCharsets.UTF_8); String decodedObject = (String) inputStreamDelegateDecoder.decode(response, String.class); assertThat(decodedObject).isEqualTo(returned); }
@Test public void testSupportsNullBody() throws Exception { String data = ""; Response response = Response.create(200, "OK", ImmutableMap.of(), (Response.Body) null); InputStream decoded = (InputStream) inputStreamDelegateDecoder.decode(response, InputStream.class); assertThat(new String(Util.toByteArray(decoded), StandardCharsets.UTF_8)) .isEqualTo(data); }