1
1
/*
2
- * Copyright 2002-2016 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.
16
16
17
17
package org .springframework .http .client ;
18
18
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
-
27
19
import java .io .ByteArrayInputStream ;
28
20
import java .io .IOException ;
29
21
import java .io .InputStream ;
30
22
import java .net .HttpURLConnection ;
31
23
import java .nio .charset .Charset ;
32
24
33
- import org .junit .Before ;
34
25
import org .junit .Test ;
35
26
36
27
import org .springframework .util .StreamUtils ;
37
28
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
+
38
35
/**
39
36
* @author Brian Clozel
37
+ * @author Juergen Hoeller
40
38
*/
41
39
public class SimpleClientHttpResponseTests {
42
40
43
41
private final Charset UTF8 = Charset .forName ("UTF-8" );
44
42
45
- private SimpleClientHttpResponse response ;
43
+ private final HttpURLConnection connection = mock ( HttpURLConnection . class ) ;
46
44
47
- private HttpURLConnection connection ;
45
+ private final SimpleClientHttpResponse response = new SimpleClientHttpResponse ( this . connection ) ;
48
46
49
- @ Before
50
- public void setup () throws Exception {
51
- this .connection = mock (HttpURLConnection .class );
52
- this .response = new SimpleClientHttpResponse (this .connection );
53
- }
54
47
55
- // SPR-14040
56
- @ Test
48
+ @ Test // SPR-14040
57
49
public void shouldNotCloseConnectionWhenResponseClosed () throws Exception {
58
50
TestByteArrayInputStream is = new TestByteArrayInputStream ("Spring" .getBytes (UTF8 ));
59
51
given (this .connection .getErrorStream ()).willReturn (null );
@@ -67,8 +59,7 @@ public void shouldNotCloseConnectionWhenResponseClosed() throws Exception {
67
59
verify (this .connection , never ()).disconnect ();
68
60
}
69
61
70
- // SPR-14040
71
- @ Test
62
+ @ Test // SPR-14040
72
63
public void shouldDrainStreamWhenResponseClosed () throws Exception {
73
64
byte [] buf = new byte [6 ];
74
65
TestByteArrayInputStream is = new TestByteArrayInputStream ("SpringSpring" .getBytes (UTF8 ));
@@ -86,8 +77,7 @@ public void shouldDrainStreamWhenResponseClosed() throws Exception {
86
77
verify (this .connection , never ()).disconnect ();
87
78
}
88
79
89
- // SPR-14040
90
- @ Test
80
+ @ Test // SPR-14040
91
81
public void shouldDrainErrorStreamWhenResponseClosed () throws Exception {
92
82
byte [] buf = new byte [6 ];
93
83
TestByteArrayInputStream is = new TestByteArrayInputStream ("SpringSpring" .getBytes (UTF8 ));
@@ -104,8 +94,22 @@ public void shouldDrainErrorStreamWhenResponseClosed() throws Exception {
104
94
verify (this .connection , never ()).disconnect ();
105
95
}
106
96
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
+
107
111
108
- class TestByteArrayInputStream extends ByteArrayInputStream {
112
+ private static class TestByteArrayInputStream extends ByteArrayInputStream {
109
113
110
114
private boolean closed ;
111
115
0 commit comments