Better empty response body support in RestTemplate
Prior to this change, RestTemplate returned an empty response body if: * HTTP return status 204 or 304 * Content-length header equals 0 This change adds a new condition for this, better supporting RFC7230 section 3.4, for connections that are closed without response body: * No Content-length header * No Transfer-encoding: chunked header value * a Connection: close header value See SPR-7911 for previous efforts in that space. Issue: SPR-8016
This commit is contained in:
@@ -108,7 +108,7 @@ public class HttpMessageConverterExtractorTests {
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = RestClientException.class)
|
||||
@SuppressWarnings("unchecked")
|
||||
public void cannotRead() throws IOException {
|
||||
HttpMessageConverter<String> converter = mock(HttpMessageConverter.class);
|
||||
@@ -122,13 +122,22 @@ public class HttpMessageConverterExtractorTests {
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(converter.canRead(String.class, contentType)).willReturn(false);
|
||||
|
||||
try {
|
||||
extractor.extractData(response);
|
||||
fail("RestClientException expected");
|
||||
}
|
||||
catch (RestClientException expected) {
|
||||
// expected
|
||||
}
|
||||
extractor.extractData(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionClose() throws IOException {
|
||||
HttpMessageConverter<String> converter = mock(HttpMessageConverter.class);
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(converter);
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setConnection("close");
|
||||
extractor = new HttpMessageConverterExtractor<String>(String.class, createConverterList(converter));
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
|
||||
Object result = extractor.extractData(response);
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user