Browse Source

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
pull/27386/head
Ruslan Stelmachenko 3 years ago committed by Rossen Stoyanchev
parent
commit
63ffa710d6
  1. 4
      spring-web/src/main/java/org/springframework/web/client/HttpMessageConverterExtractor.java
  2. 20
      spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java

4
spring-web/src/main/java/org/springframework/web/client/HttpMessageConverterExtractor.java

@ -122,8 +122,8 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> { @@ -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));
}
/**

20
spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java

@ -38,6 +38,7 @@ import static java.util.Arrays.asList; @@ -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 { @@ -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");
}
}

Loading…
Cancel
Save