Drain JDK HTTP client response body in all cases

Prior to this commit, when using the `SimpleClientHttpRequestFactory`
as a driver for `RestTemplate`, the HTTP response body would only be
drained if there was an attempt to read it in the first place.

This commit ensures that, even if there's no attempt at reading the
response body, it is properly drained when the response is closed to
make sure that the connection is released in a proper state and can be
put back in the connection pool for reuse.

Issue: SPR-17181
master
Brian Clozel 6 years ago
parent 432cdd7802
commit 23fc6f6b1d
  1. 15
      spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java
  2. 2
      spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java
  3. 12
      spring-web/src/test/java/org/springframework/http/client/SimpleClientHttpResponseTests.java

@ -91,14 +91,15 @@ final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
@Override @Override
public void close() { public void close() {
if (this.responseStream != null) { try {
try { if (this.responseStream == null) {
StreamUtils.drain(this.responseStream); getBody();
this.responseStream.close();
}
catch (Exception ex) {
// ignore
} }
StreamUtils.drain(this.responseStream);
this.responseStream.close();
}
catch (Exception ex) {
// ignore
} }
} }

@ -135,7 +135,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
public void headersAfterExecute() throws Exception { public void headersAfterExecute() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST); ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST);
request.getHeaders().add("MyHeader", "value"); request.getHeaders().add("MyHeader", "value");
byte[] body = "Hello World".getBytes("UTF-8"); byte[] body = "Hello World".getBytes("UTF-8");

@ -106,6 +106,18 @@ public class SimpleClientHttpResponseTests {
verify(is).close(); verify(is).close();
} }
@Test // SPR-17181
public void shouldDrainResponseEvenIfResponseNotRead() throws Exception {
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8));
given(this.connection.getErrorStream()).willReturn(null);
given(this.connection.getInputStream()).willReturn(is);
this.response.close();
assertThat(is.available(), is(0));
assertTrue(is.isClosed());
verify(this.connection, never()).disconnect();
}
private static class TestByteArrayInputStream extends ByteArrayInputStream { private static class TestByteArrayInputStream extends ByteArrayInputStream {

Loading…
Cancel
Save