16
16
17
17
package org .springframework .web .client ;
18
18
19
+ import java .io .ByteArrayInputStream ;
19
20
import java .io .IOException ;
20
21
import java .lang .reflect .Type ;
21
22
import java .util .ArrayList ;
26
27
27
28
import org .springframework .core .ParameterizedTypeReference ;
28
29
import org .springframework .http .HttpHeaders ;
30
+ import org .springframework .http .HttpInputMessage ;
29
31
import org .springframework .http .HttpStatus ;
30
32
import org .springframework .http .MediaType ;
31
33
import org .springframework .http .client .ClientHttpResponse ;
@@ -73,6 +75,17 @@ public void notModified() throws IOException {
73
75
assertNull (result );
74
76
}
75
77
78
+ @ Test
79
+ public void informational () throws IOException {
80
+ HttpMessageConverter <?> converter = mock (HttpMessageConverter .class );
81
+ extractor = new HttpMessageConverterExtractor <String >(String .class , createConverterList (converter ));
82
+ given (response .getStatusCode ()).willReturn (HttpStatus .CONTINUE );
83
+
84
+ Object result = extractor .extractData (response );
85
+
86
+ assertNull (result );
87
+ }
88
+
76
89
@ Test
77
90
public void zeroContentLength () throws IOException {
78
91
HttpMessageConverter <?> converter = mock (HttpMessageConverter .class );
@@ -87,6 +100,22 @@ public void zeroContentLength() throws IOException {
87
100
assertNull (result );
88
101
}
89
102
103
+ @ Test
104
+ @ SuppressWarnings ("unchecked" )
105
+ public void emptyMessageBody () throws IOException {
106
+ HttpMessageConverter <String > converter = mock (HttpMessageConverter .class );
107
+ List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
108
+ converters .add (converter );
109
+ HttpHeaders responseHeaders = new HttpHeaders ();
110
+ extractor = new HttpMessageConverterExtractor <String >(String .class , createConverterList (converter ));
111
+ given (response .getStatusCode ()).willReturn (HttpStatus .OK );
112
+ given (response .getHeaders ()).willReturn (responseHeaders );
113
+ given (response .getBody ()).willReturn (new ByteArrayInputStream ("" .getBytes ()));
114
+
115
+ Object result = extractor .extractData (response );
116
+ assertNull (result );
117
+ }
118
+
90
119
@ Test
91
120
@ SuppressWarnings ("unchecked" )
92
121
public void normal () throws IOException {
@@ -100,8 +129,9 @@ public void normal() throws IOException {
100
129
extractor = new HttpMessageConverterExtractor <String >(String .class , converters );
101
130
given (response .getStatusCode ()).willReturn (HttpStatus .OK );
102
131
given (response .getHeaders ()).willReturn (responseHeaders );
132
+ given (response .getBody ()).willReturn (new ByteArrayInputStream (expected .getBytes ()));
103
133
given (converter .canRead (String .class , contentType )).willReturn (true );
104
- given (converter .read (String .class , response )).willReturn (expected );
134
+ given (converter .read (eq ( String .class ), any ( HttpInputMessage . class ) )).willReturn (expected );
105
135
106
136
Object result = extractor .extractData (response );
107
137
@@ -120,28 +150,13 @@ public void cannotRead() throws IOException {
120
150
extractor = new HttpMessageConverterExtractor <String >(String .class , converters );
121
151
given (response .getStatusCode ()).willReturn (HttpStatus .OK );
122
152
given (response .getHeaders ()).willReturn (responseHeaders );
153
+ given (response .getBody ()).willReturn (new ByteArrayInputStream ("Foobar" .getBytes ()));
123
154
given (converter .canRead (String .class , contentType )).willReturn (false );
124
155
125
156
extractor .extractData (response );
126
157
}
127
158
128
159
@ Test
129
- public void connectionClose () throws IOException {
130
- HttpMessageConverter <String > converter = mock (HttpMessageConverter .class );
131
- List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
132
- converters .add (converter );
133
- HttpHeaders responseHeaders = new HttpHeaders ();
134
- responseHeaders .setConnection ("close" );
135
- extractor = new HttpMessageConverterExtractor <String >(String .class , createConverterList (converter ));
136
- given (response .getStatusCode ()).willReturn (HttpStatus .OK );
137
- given (response .getHeaders ()).willReturn (responseHeaders );
138
-
139
- Object result = extractor .extractData (response );
140
- assertNull (result );
141
- }
142
-
143
- @ Test
144
- @ SuppressWarnings ("unchecked" )
145
160
public void generics () throws IOException {
146
161
GenericHttpMessageConverter <String > converter = mock (GenericHttpMessageConverter .class );
147
162
List <HttpMessageConverter <?>> converters = createConverterList (converter );
@@ -154,8 +169,9 @@ public void generics() throws IOException {
154
169
extractor = new HttpMessageConverterExtractor <List <String >>(type , converters );
155
170
given (response .getStatusCode ()).willReturn (HttpStatus .OK );
156
171
given (response .getHeaders ()).willReturn (responseHeaders );
172
+ given (response .getBody ()).willReturn (new ByteArrayInputStream (expected .getBytes ()));
157
173
given (converter .canRead (type , null , contentType )).willReturn (true );
158
- given (converter .read (type , null , response )).willReturn (expected );
174
+ given (converter .read (eq ( type ), eq ( null ), any ( HttpInputMessage . class ) )).willReturn (expected );
159
175
160
176
Object result = extractor .extractData (response );
161
177
0 commit comments