|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | import java.net.HttpURLConnection;
|
23 | 23 | import java.nio.charset.StandardCharsets;
|
24 | 24 |
|
25 |
| -import org.junit.Before; |
26 | 25 | import org.junit.Test;
|
27 | 26 |
|
28 | 27 | import org.springframework.util.StreamUtils;
|
29 | 28 |
|
30 | 29 | import static org.hamcrest.MatcherAssert.assertThat;
|
31 | 30 | import static org.hamcrest.Matchers.*;
|
32 | 31 | import static org.junit.Assert.*;
|
| 32 | +import static org.mockito.BDDMockito.any; |
33 | 33 | import static org.mockito.BDDMockito.*;
|
34 | 34 |
|
35 | 35 | /**
|
36 | 36 | * @author Brian Clozel
|
| 37 | + * @author Juergen Hoeller |
37 | 38 | */
|
38 | 39 | public class SimpleClientHttpResponseTests {
|
39 | 40 |
|
40 |
| - private SimpleClientHttpResponse response; |
| 41 | + private final HttpURLConnection connection = mock(HttpURLConnection.class); |
41 | 42 |
|
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); |
50 | 44 |
|
51 | 45 |
|
52 | 46 | @Test // SPR-14040
|
@@ -98,8 +92,22 @@ public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
|
98 | 92 | verify(this.connection, never()).disconnect();
|
99 | 93 | }
|
100 | 94 |
|
| 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 | + |
101 | 109 |
|
102 |
| - class TestByteArrayInputStream extends ByteArrayInputStream { |
| 110 | + private static class TestByteArrayInputStream extends ByteArrayInputStream { |
103 | 111 |
|
104 | 112 | private boolean closed;
|
105 | 113 |
|
|
0 commit comments