22
22
import java .util .ArrayList ;
23
23
import java .util .List ;
24
24
25
+ import org .hamcrest .Matchers ;
25
26
import org .junit .Before ;
27
+ import org .junit .Rule ;
26
28
import org .junit .Test ;
29
+ import org .junit .rules .ExpectedException ;
27
30
28
31
import org .springframework .core .ParameterizedTypeReference ;
29
32
import org .springframework .http .HttpHeaders ;
33
36
import org .springframework .http .client .ClientHttpResponse ;
34
37
import org .springframework .http .converter .GenericHttpMessageConverter ;
35
38
import org .springframework .http .converter .HttpMessageConverter ;
39
+ import org .springframework .http .converter .HttpMessageNotReadableException ;
36
40
37
41
import static org .junit .Assert .*;
38
42
import static org .mockito .BDDMockito .*;
41
45
* Test fixture for {@link HttpMessageConverter}.
42
46
*
43
47
* @author Arjen Poutsma
48
+ * @author Brian Clozel
44
49
*/
45
50
public class HttpMessageConverterExtractorTests {
46
51
47
52
private HttpMessageConverterExtractor <?> extractor ;
48
53
49
54
private ClientHttpResponse response ;
50
55
56
+ @ Rule
57
+ public final ExpectedException exception = ExpectedException .none ();
58
+
51
59
@ Before
52
60
public void createMocks () {
53
61
response = mock (ClientHttpResponse .class );
@@ -104,8 +112,6 @@ public void zeroContentLength() throws IOException {
104
112
@ SuppressWarnings ("unchecked" )
105
113
public void emptyMessageBody () throws IOException {
106
114
HttpMessageConverter <String > converter = mock (HttpMessageConverter .class );
107
- List <HttpMessageConverter <?>> converters = new ArrayList <>();
108
- converters .add (converter );
109
115
HttpHeaders responseHeaders = new HttpHeaders ();
110
116
extractor = new HttpMessageConverterExtractor <>(String .class , createConverterList (converter ));
111
117
given (response .getStatusCode ()).willReturn (HttpStatus .OK );
@@ -120,13 +126,11 @@ public void emptyMessageBody() throws IOException {
120
126
@ SuppressWarnings ("unchecked" )
121
127
public void normal () throws IOException {
122
128
HttpMessageConverter <String > converter = mock (HttpMessageConverter .class );
123
- List <HttpMessageConverter <?>> converters = new ArrayList <>();
124
- converters .add (converter );
125
129
HttpHeaders responseHeaders = new HttpHeaders ();
126
130
MediaType contentType = MediaType .TEXT_PLAIN ;
127
131
responseHeaders .setContentType (contentType );
128
132
String expected = "Foo" ;
129
- extractor = new HttpMessageConverterExtractor <>(String .class , converters );
133
+ extractor = new HttpMessageConverterExtractor <>(String .class , createConverterList ( converter ) );
130
134
given (response .getStatusCode ()).willReturn (HttpStatus .OK );
131
135
given (response .getHeaders ()).willReturn (responseHeaders );
132
136
given (response .getBody ()).willReturn (new ByteArrayInputStream (expected .getBytes ()));
@@ -138,20 +142,19 @@ public void normal() throws IOException {
138
142
assertEquals (expected , result );
139
143
}
140
144
141
- @ Test ( expected = RestClientException . class )
145
+ @ Test
142
146
@ SuppressWarnings ("unchecked" )
143
147
public void cannotRead () throws IOException {
144
148
HttpMessageConverter <String > converter = mock (HttpMessageConverter .class );
145
- List <HttpMessageConverter <?>> converters = new ArrayList <>();
146
- converters .add (converter );
147
149
HttpHeaders responseHeaders = new HttpHeaders ();
148
150
MediaType contentType = MediaType .TEXT_PLAIN ;
149
151
responseHeaders .setContentType (contentType );
150
- extractor = new HttpMessageConverterExtractor <>(String .class , converters );
152
+ extractor = new HttpMessageConverterExtractor <>(String .class , createConverterList ( converter ) );
151
153
given (response .getStatusCode ()).willReturn (HttpStatus .OK );
152
154
given (response .getHeaders ()).willReturn (responseHeaders );
153
155
given (response .getBody ()).willReturn (new ByteArrayInputStream ("Foobar" .getBytes ()));
154
156
given (converter .canRead (String .class , contentType )).willReturn (false );
157
+ exception .expect (RestClientException .class );
155
158
156
159
extractor .extractData (response );
157
160
}
@@ -160,14 +163,13 @@ public void cannotRead() throws IOException {
160
163
@ SuppressWarnings ("unchecked" )
161
164
public void generics () throws IOException {
162
165
GenericHttpMessageConverter <String > converter = mock (GenericHttpMessageConverter .class );
163
- List <HttpMessageConverter <?>> converters = createConverterList (converter );
164
166
HttpHeaders responseHeaders = new HttpHeaders ();
165
167
MediaType contentType = MediaType .TEXT_PLAIN ;
166
168
responseHeaders .setContentType (contentType );
167
169
String expected = "Foo" ;
168
170
ParameterizedTypeReference <List <String >> reference = new ParameterizedTypeReference <List <String >>() {};
169
171
Type type = reference .getType ();
170
- extractor = new HttpMessageConverterExtractor <List <String >>(type , converters );
172
+ extractor = new HttpMessageConverterExtractor <List <String >>(type , createConverterList ( converter ) );
171
173
given (response .getStatusCode ()).willReturn (HttpStatus .OK );
172
174
given (response .getHeaders ()).willReturn (responseHeaders );
173
175
given (response .getBody ()).willReturn (new ByteArrayInputStream (expected .getBytes ()));
@@ -179,6 +181,46 @@ public void generics() throws IOException {
179
181
assertEquals (expected , result );
180
182
}
181
183
184
+ @ Test // SPR-13592
185
+ @ SuppressWarnings ("unchecked" )
186
+ public void converterThrowsIOException () throws IOException {
187
+ HttpMessageConverter <String > converter = mock (HttpMessageConverter .class );
188
+ HttpHeaders responseHeaders = new HttpHeaders ();
189
+ MediaType contentType = MediaType .TEXT_PLAIN ;
190
+ responseHeaders .setContentType (contentType );
191
+ extractor = new HttpMessageConverterExtractor <>(String .class , createConverterList (converter ));
192
+ given (response .getStatusCode ()).willReturn (HttpStatus .OK );
193
+ given (response .getHeaders ()).willReturn (responseHeaders );
194
+ given (response .getBody ()).willReturn (new ByteArrayInputStream ("Foobar" .getBytes ()));
195
+ given (converter .canRead (String .class , contentType )).willThrow (IOException .class );
196
+ exception .expect (RestClientException .class );
197
+ exception .expectMessage ("Error while extracting response for type " +
198
+ "[class java.lang.String] and content type [text/plain]" );
199
+ exception .expectCause (Matchers .instanceOf (IOException .class ));
200
+
201
+ extractor .extractData (response );
202
+ }
203
+
204
+ @ Test // SPR-13592
205
+ @ SuppressWarnings ("unchecked" )
206
+ public void converterThrowsHttpMessageNotReadableException () throws IOException {
207
+ HttpMessageConverter <String > converter = mock (HttpMessageConverter .class );
208
+ HttpHeaders responseHeaders = new HttpHeaders ();
209
+ MediaType contentType = MediaType .TEXT_PLAIN ;
210
+ responseHeaders .setContentType (contentType );
211
+ extractor = new HttpMessageConverterExtractor <>(String .class , createConverterList (converter ));
212
+ given (response .getStatusCode ()).willReturn (HttpStatus .OK );
213
+ given (response .getHeaders ()).willReturn (responseHeaders );
214
+ given (response .getBody ()).willReturn (new ByteArrayInputStream ("Foobar" .getBytes ()));
215
+ given (converter .canRead (String .class , contentType )).willThrow (HttpMessageNotReadableException .class );
216
+ exception .expect (RestClientException .class );
217
+ exception .expectMessage ("Error while extracting response for type " +
218
+ "[class java.lang.String] and content type [text/plain]" );
219
+ exception .expectCause (Matchers .instanceOf (HttpMessageNotReadableException .class ));
220
+
221
+ extractor .extractData (response );
222
+ }
223
+
182
224
private List <HttpMessageConverter <?>> createConverterList (
183
225
HttpMessageConverter <?> converter ) {
184
226
List <HttpMessageConverter <?>> converters = new ArrayList <>(1 );
0 commit comments