Include all bytes of body in UnknownContentTypeException

HttpMessageConverterExtractor uses MessageBodyClientHttpResponseWrapper
which may read the first byte of the response stream to check if there
is content. After that it is necessary to use the wrapper to get the
full body.

This commit ensures that when UnknownContentTypeException is raised
it gets the body through the wrapper, or otherwise the first byte is
missed if the InputStream is not markable.

Closes gh-27374
This commit is contained in:
Ruslan Stelmachenko 2021-09-09 01:43:20 +03:00 committed by Rossen Stoyanchev
parent b6037d0d07
commit 63ffa710d6
2 changed files with 22 additions and 2 deletions

View File

@ -122,8 +122,8 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
}
throw new UnknownContentTypeException(this.responseType, contentType,
response.getRawStatusCode(), response.getStatusText(), response.getHeaders(),
getResponseBody(response));
responseWrapper.getRawStatusCode(), responseWrapper.getStatusText(), responseWrapper.getHeaders(),
getResponseBody(responseWrapper));
}
/**

View File

@ -38,6 +38,7 @@ import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@ -194,4 +195,23 @@ public class HttpMessageConverterExtractorTests {
.withCauseInstanceOf(HttpMessageNotReadableException.class);
}
@Test
public void unknownContentTypeExceptionContainsCorrectResponseBody() throws IOException {
responseHeaders.setContentType(contentType);
given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value());
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getBody()).willReturn(new ByteArrayInputStream("Foobar".getBytes()) {
@Override
public boolean markSupported() {
return false;
}
});
given(converter.canRead(String.class, contentType)).willReturn(false);
UnknownContentTypeException unknownContentTypeException =
catchThrowableOfType(() -> extractor.extractData(response), UnknownContentTypeException.class);
assertThat(unknownContentTypeException).isNotNull();
assertThat(unknownContentTypeException.getResponseBodyAsString()).isEqualTo("Foobar");
}
}