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
This commit is contained in:
@@ -135,7 +135,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
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");
|
||||
byte[] body = "Hello World".getBytes("UTF-8");
|
||||
|
||||
@@ -106,6 +106,18 @@ public class SimpleClientHttpResponseTests {
|
||||
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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user