32
32
33
33
import static org .assertj .core .api .Assertions .assertThat ;
34
34
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
35
+ import static org .assertj .core .api .Assertions .catchThrowable ;
35
36
import static org .mockito .BDDMockito .given ;
36
37
import static org .mockito .Mockito .mock ;
37
38
@@ -160,15 +161,29 @@ public void hasErrorForCustomClientError() throws Exception {
160
161
161
162
@ Test
162
163
public void handleErrorForCustomClientError () throws Exception {
164
+ int statusCode = 499 ;
165
+ String statusText = "Custom status code" ;
166
+
163
167
HttpHeaders headers = new HttpHeaders ();
164
168
headers .setContentType (MediaType .TEXT_PLAIN );
165
169
166
- given (response .getRawStatusCode ()).willReturn (499 );
167
- given (response .getStatusText ()).willReturn ("Custom status code" );
170
+ String responseBody = "Hello World" ;
171
+ TestByteArrayInputStream body = new TestByteArrayInputStream (responseBody .getBytes (StandardCharsets .UTF_8 ));
172
+
173
+ given (response .getRawStatusCode ()).willReturn (statusCode );
174
+ given (response .getStatusText ()).willReturn (statusText );
168
175
given (response .getHeaders ()).willReturn (headers );
176
+ given (response .getBody ()).willReturn (body );
169
177
170
- assertThatExceptionOfType (UnknownHttpStatusCodeException .class ).isThrownBy (() ->
171
- handler .handleError (response ));
178
+ Throwable throwable = catchThrowable (() -> handler .handleError (response ));
179
+
180
+ // validate exception
181
+ assertThat (throwable ).isInstanceOf (UnknownHttpStatusCodeException .class );
182
+ UnknownHttpStatusCodeException actualUnknownHttpStatusCodeException = (UnknownHttpStatusCodeException ) throwable ;
183
+ assertThat (actualUnknownHttpStatusCodeException .getRawStatusCode ()).isEqualTo (statusCode );
184
+ assertThat (actualUnknownHttpStatusCodeException .getStatusText ()).isEqualTo (statusText );
185
+ assertThat (actualUnknownHttpStatusCodeException .getResponseHeaders ()).isEqualTo (headers );
186
+ assertThat (actualUnknownHttpStatusCodeException .getResponseBodyAsString ()).isEqualTo (responseBody );
172
187
}
173
188
174
189
@ Test // SPR-17461
@@ -185,15 +200,29 @@ public void hasErrorForCustomServerError() throws Exception {
185
200
186
201
@ Test
187
202
public void handleErrorForCustomServerError () throws Exception {
203
+ int statusCode = 599 ;
204
+ String statusText = "Custom status code" ;
205
+
188
206
HttpHeaders headers = new HttpHeaders ();
189
207
headers .setContentType (MediaType .TEXT_PLAIN );
190
208
191
- given (response .getRawStatusCode ()).willReturn (599 );
192
- given (response .getStatusText ()).willReturn ("Custom status code" );
209
+ String responseBody = "Hello World" ;
210
+ TestByteArrayInputStream body = new TestByteArrayInputStream (responseBody .getBytes (StandardCharsets .UTF_8 ));
211
+
212
+ given (response .getRawStatusCode ()).willReturn (statusCode );
213
+ given (response .getStatusText ()).willReturn (statusText );
193
214
given (response .getHeaders ()).willReturn (headers );
215
+ given (response .getBody ()).willReturn (body );
194
216
195
- assertThatExceptionOfType (UnknownHttpStatusCodeException .class ).isThrownBy (() ->
196
- handler .handleError (response ));
217
+ Throwable throwable = catchThrowable (() -> handler .handleError (response ));
218
+
219
+ // validate exception
220
+ assertThat (throwable ).isInstanceOf (UnknownHttpStatusCodeException .class );
221
+ UnknownHttpStatusCodeException actualUnknownHttpStatusCodeException = (UnknownHttpStatusCodeException ) throwable ;
222
+ assertThat (actualUnknownHttpStatusCodeException .getRawStatusCode ()).isEqualTo (statusCode );
223
+ assertThat (actualUnknownHttpStatusCodeException .getStatusText ()).isEqualTo (statusText );
224
+ assertThat (actualUnknownHttpStatusCodeException .getResponseHeaders ()).isEqualTo (headers );
225
+ assertThat (actualUnknownHttpStatusCodeException .getResponseBodyAsString ()).isEqualTo (responseBody );
197
226
}
198
227
199
228
@ Test // SPR-16604
@@ -212,7 +241,6 @@ public void bodyAvailableAfterHasErrorForUnknownStatusCode() throws Exception {
212
241
assertThat (StreamUtils .copyToString (response .getBody (), StandardCharsets .UTF_8 )).isEqualTo ("Hello World" );
213
242
}
214
243
215
-
216
244
private static class TestByteArrayInputStream extends ByteArrayInputStream {
217
245
218
246
private boolean closed ;
0 commit comments