Browse Source

Improve empty body check

Fixes gh-22265
pull/22392/head
Rossen Stoyanchev 7 years ago
parent
commit
4560dc2818
  1. 7
      spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java
  2. 2
      spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java
  3. 16
      spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java

7
spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -79,8 +79,13 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse { @@ -79,8 +79,13 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
* @return {@code true} if the response has a zero-length message body, {@code false} otherwise
* @throws IOException in case of I/O errors
*/
@SuppressWarnings("ConstantConditions")
public boolean hasEmptyMessageBody() throws IOException {
InputStream body = this.response.getBody();
// Per contract body shouldn't be null, but check anyway..
if (body == null) {
return true;
}
if (body.markSupported()) {
body.mark(1);
if (body.read() == -1) {

2
spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java

@ -175,7 +175,7 @@ public class Jackson2JsonDecoderTests extends AbstractDecoderTestCase<Jackson2Js @@ -175,7 +175,7 @@ public class Jackson2JsonDecoderTests extends AbstractDecoderTestCase<Jackson2Js
.verifyError(DecodingException.class));
}
@Test // #22042
@Test // gh-22042
public void decodeWithNullLiteral() {
Flux<Object> result = this.decoder.decode(Flux.concat(stringBuffer("null")),
ResolvableType.forType(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap());

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -113,6 +113,20 @@ public class HttpMessageConverterExtractorTests { @@ -113,6 +113,20 @@ public class HttpMessageConverterExtractorTests {
assertNull(result);
}
@Test // gh-22265
@SuppressWarnings("unchecked")
public void nullMessageBody() throws IOException {
HttpMessageConverter<String> converter = mock(HttpMessageConverter.class);
HttpHeaders responseHeaders = new HttpHeaders();
extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter));
given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value());
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getBody()).willReturn(null);
Object result = extractor.extractData(response);
assertNull(result);
}
@Test
@SuppressWarnings("unchecked")
public void normal() throws IOException {

Loading…
Cancel
Save