Skip to content

Commit 289a609

Browse files
committed
SimpleClientHttpResponse catches any Exception on close
Issue: SPR-16773 (cherry picked from commit 21fad8e)
1 parent 3502f6f commit 289a609

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,7 +93,7 @@ public void close() {
9393
StreamUtils.drain(this.responseStream);
9494
this.responseStream.close();
9595
}
96-
catch (IOException ex) {
96+
catch (Exception ex) {
9797
// ignore
9898
}
9999
}

spring-web/src/test/java/org/springframework/http/client/SimpleClientHttpResponseTests.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,44 +16,36 @@
1616

1717
package org.springframework.http.client;
1818

19-
import static org.hamcrest.MatcherAssert.*;
20-
import static org.hamcrest.Matchers.*;
21-
import static org.junit.Assert.assertTrue;
22-
import static org.mockito.BDDMockito.*;
23-
import static org.mockito.Mockito.mock;
24-
import static org.mockito.Mockito.never;
25-
import static org.mockito.Mockito.verify;
26-
2719
import java.io.ByteArrayInputStream;
2820
import java.io.IOException;
2921
import java.io.InputStream;
3022
import java.net.HttpURLConnection;
3123
import java.nio.charset.Charset;
3224

33-
import org.junit.Before;
3425
import org.junit.Test;
3526

3627
import org.springframework.util.StreamUtils;
3728

29+
import static org.hamcrest.MatcherAssert.assertThat;
30+
import static org.hamcrest.Matchers.*;
31+
import static org.junit.Assert.*;
32+
import static org.mockito.BDDMockito.any;
33+
import static org.mockito.BDDMockito.*;
34+
3835
/**
3936
* @author Brian Clozel
37+
* @author Juergen Hoeller
4038
*/
4139
public class SimpleClientHttpResponseTests {
4240

4341
private final Charset UTF8 = Charset.forName("UTF-8");
4442

45-
private SimpleClientHttpResponse response;
43+
private final HttpURLConnection connection = mock(HttpURLConnection.class);
4644

47-
private HttpURLConnection connection;
45+
private final SimpleClientHttpResponse response = new SimpleClientHttpResponse(this.connection);
4846

49-
@Before
50-
public void setup() throws Exception {
51-
this.connection = mock(HttpURLConnection.class);
52-
this.response = new SimpleClientHttpResponse(this.connection);
53-
}
5447

55-
// SPR-14040
56-
@Test
48+
@Test // SPR-14040
5749
public void shouldNotCloseConnectionWhenResponseClosed() throws Exception {
5850
TestByteArrayInputStream is = new TestByteArrayInputStream("Spring".getBytes(UTF8));
5951
given(this.connection.getErrorStream()).willReturn(null);
@@ -67,8 +59,7 @@ public void shouldNotCloseConnectionWhenResponseClosed() throws Exception {
6759
verify(this.connection, never()).disconnect();
6860
}
6961

70-
// SPR-14040
71-
@Test
62+
@Test // SPR-14040
7263
public void shouldDrainStreamWhenResponseClosed() throws Exception {
7364
byte[] buf = new byte[6];
7465
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
@@ -86,8 +77,7 @@ public void shouldDrainStreamWhenResponseClosed() throws Exception {
8677
verify(this.connection, never()).disconnect();
8778
}
8879

89-
// SPR-14040
90-
@Test
80+
@Test // SPR-14040
9181
public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
9282
byte[] buf = new byte[6];
9383
TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(UTF8));
@@ -104,8 +94,22 @@ public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
10494
verify(this.connection, never()).disconnect();
10595
}
10696

97+
@Test // SPR-16773
98+
public void shouldNotDrainWhenErrorStreamClosed() throws Exception {
99+
InputStream is = mock(InputStream.class);
100+
given(this.connection.getErrorStream()).willReturn(is);
101+
doNothing().when(is).close();
102+
given(is.read(any())).willThrow(new NullPointerException("from HttpURLConnection#ErrorStream"));
103+
104+
InputStream responseStream = this.response.getBody();
105+
responseStream.close();
106+
this.response.close();
107+
108+
verify(is).close();
109+
}
110+
107111

108-
class TestByteArrayInputStream extends ByteArrayInputStream {
112+
private static class TestByteArrayInputStream extends ByteArrayInputStream {
109113

110114
private boolean closed;
111115

0 commit comments

Comments
 (0)