Skip to content

Commit d74a273

Browse files
committed
SimpleClientHttpResponse catches any Exception on close
Issue: SPR-16773 (cherry picked from commit 21fad8e)
1 parent 3e47f45 commit d74a273

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
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-2017 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.
@@ -96,7 +96,7 @@ public void close() {
9696
StreamUtils.drain(this.responseStream);
9797
this.responseStream.close();
9898
}
99-
catch (IOException ex) {
99+
catch (Exception ex) {
100100
// ignore
101101
}
102102
}

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 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.
@@ -22,31 +22,25 @@
2222
import java.net.HttpURLConnection;
2323
import java.nio.charset.StandardCharsets;
2424

25-
import org.junit.Before;
2625
import org.junit.Test;
2726

2827
import org.springframework.util.StreamUtils;
2928

3029
import static org.hamcrest.MatcherAssert.assertThat;
3130
import static org.hamcrest.Matchers.*;
3231
import static org.junit.Assert.*;
32+
import static org.mockito.BDDMockito.any;
3333
import static org.mockito.BDDMockito.*;
3434

3535
/**
3636
* @author Brian Clozel
37+
* @author Juergen Hoeller
3738
*/
3839
public class SimpleClientHttpResponseTests {
3940

40-
private SimpleClientHttpResponse response;
41+
private final HttpURLConnection connection = mock(HttpURLConnection.class);
4142

42-
private HttpURLConnection connection;
43-
44-
45-
@Before
46-
public void setup() throws Exception {
47-
this.connection = mock(HttpURLConnection.class);
48-
this.response = new SimpleClientHttpResponse(this.connection);
49-
}
43+
private final SimpleClientHttpResponse response = new SimpleClientHttpResponse(this.connection);
5044

5145

5246
@Test // SPR-14040
@@ -98,8 +92,22 @@ public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
9892
verify(this.connection, never()).disconnect();
9993
}
10094

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

102-
class TestByteArrayInputStream extends ByteArrayInputStream {
110+
private static class TestByteArrayInputStream extends ByteArrayInputStream {
103111

104112
private boolean closed;
105113

0 commit comments

Comments
 (0)