SimpleClientHttpResponse catches any Exception on close

Issue: SPR-16773

(cherry picked from commit 21fad8e)
This commit is contained in:
Juergen Hoeller
2018-05-01 23:51:05 +02:00
parent 3502f6fdc2
commit 289a6091f1
2 changed files with 30 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -16,44 +16,36 @@
package org.springframework.http.client;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.StreamUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.*;
/**
* @author Brian Clozel
* @author Juergen Hoeller
*/
public class SimpleClientHttpResponseTests {
private final Charset UTF8 = Charset.forName("UTF-8");
private SimpleClientHttpResponse response;
private final HttpURLConnection connection = mock(HttpURLConnection.class);
private HttpURLConnection connection;
private final SimpleClientHttpResponse response = new SimpleClientHttpResponse(this.connection);
@Before
public void setup() throws Exception {
this.connection = mock(HttpURLConnection.class);
this.response = new SimpleClientHttpResponse(this.connection);
}
// SPR-14040
@Test
@Test // SPR-14040
public void shouldNotCloseConnectionWhenResponseClosed() throws Exception {
TestByteArrayInputStream is = new TestByteArrayInputStream("Spring".getBytes(UTF8));
given(this.connection.getErrorStream()).willReturn(null);
@@ -67,8 +59,7 @@ public class SimpleClientHttpResponseTests {
verify(this.connection, never()).disconnect();
}
// SPR-14040
@Test
@Test // SPR-14040
public void shouldDrainStreamWhenResponseClosed() throws Exception {
byte[] buf = new byte[6];
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
@@ -86,8 +77,7 @@ public class SimpleClientHttpResponseTests {
verify(this.connection, never()).disconnect();
}
// SPR-14040
@Test
@Test // SPR-14040
public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
byte[] buf = new byte[6];
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
@@ -104,8 +94,22 @@ public class SimpleClientHttpResponseTests {
verify(this.connection, never()).disconnect();
}
@Test // SPR-16773
public void shouldNotDrainWhenErrorStreamClosed() throws Exception {
InputStream is = mock(InputStream.class);
given(this.connection.getErrorStream()).willReturn(is);
doNothing().when(is).close();
given(is.read(any())).willThrow(new NullPointerException("from HttpURLConnection#ErrorStream"));
class TestByteArrayInputStream extends ByteArrayInputStream {
InputStream responseStream = this.response.getBody();
responseStream.close();
this.response.close();
verify(is).close();
}
private static class TestByteArrayInputStream extends ByteArrayInputStream {
private boolean closed;