ResponseUtils { @NotNull static Observable<String> attemptToReadErrorResponse(@NotNull final HttpClientResponse<ByteBuf> resp) { final HttpResponseHeaders headers = resp.getHeaders(); final String contentType = resp.getHeaders().get(HttpHeaderNames.CONTENT_TYPE); if (headers.isContentLengthSet() && headers.getContentLength() > 0 ) { if (contentType != null && contentType.startsWith("text/plain")) { return resp.getContent() .map(r -> r.toString(StandardCharsets.UTF_8)); } else { resp.ignoreContent(); final String errMsg = getErrMsg(contentType); return Observable.just(errMsg); } } else { return Observable.just(""); } } private  ResponseUtils();   }
@Test public void attemptToReadErrorResponse_contentLengthIsZero() throws Exception { final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(new byte[]{}), (headers) -> headers.add("Content-Length", "0") ); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo(""); }
@Test public void attemptToReadErrorResponse_nonTextPlain() throws Exception { final String errMsg = "{\"some\":\"json\"}"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> { headers.add("Content-Type", "application/json;charset=utf-8"); headers.add("Content-Length", bytes.length); }); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo("Not attempting to decode error response of type 'application/json;charset=utf-8' as string"); }
@Test public void attemptToReadErrorResponse_responseContentIgnoredByDefaultWhenNotString() throws Exception { final String errMsg = "lies"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> { headers.add("Content-Type", "application/json;charset=utf-8"); headers.add("Content-Length", bytes.length); }); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isNotEqualTo("lies"); try { resp.getContent().toBlocking().first(); } catch (IllegalStateException e) { assertThat(e.getMessage()).isEqualTo("Content stream is already disposed."); } }
@Test public void attemptToReadErrorResponse_plainString() throws Exception { final String errMsg = "some response"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> { headers.add("Content-Type", "text/plain;charset=utf-8"); headers.add("Content-Length", bytes.length); }); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo(errMsg); }
@Test public void attemptToReadErrorResponse_noContentLength() throws Exception { final String errMsg = "some response"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> headers.add("Content-Type", "text/plain;charset=utf-8") ); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo(""); }
@Test public void attemptToReadErrorResponse_noContentType() throws Exception { final String errMsg = "some response"; final byte[] bytes = errMsg.getBytes(StandardCharsets.UTF_8); final HttpClientResponse<ByteBuf> resp = response(Unpooled.copiedBuffer(bytes), (headers) -> headers.add("Content-Length", bytes.length) ); final String err = ResponseUtils.attemptToReadErrorResponse(resp).toBlocking().first(); assertThat(err).isEqualTo("Not attempting to decode error response with unspecified Content-Type"); }