SPR-8883 - RestTemplate.headForHeaders throws "IllegalArgumentException: No InputStream specified" on server resource which status code are 4xx
This commit is contained in:
parent
b94040f329
commit
3beef9a92e
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
@ -82,11 +83,15 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
|||
|
||||
private byte[] getResponseBody(ClientHttpResponse response) {
|
||||
try {
|
||||
return FileCopyUtils.copyToByteArray(response.getBody());
|
||||
InputStream responseBody = response.getBody();
|
||||
if (responseBody != null) {
|
||||
return FileCopyUtils.copyToByteArray(responseBody);
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
return new byte[0];
|
||||
// ignore
|
||||
}
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,16 +19,17 @@ package org.springframework.web.client;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class DefaultResponseErrorHandlerTests {
|
||||
|
@ -96,4 +97,21 @@ public class DefaultResponseErrorHandlerTests {
|
|||
|
||||
verify(response);
|
||||
}
|
||||
|
||||
@Test(expected = HttpClientErrorException.class)
|
||||
public void handleErrorNullResponse() throws Exception {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
|
||||
expect(response.getStatusCode()).andReturn(HttpStatus.NOT_FOUND);
|
||||
expect(response.getStatusText()).andReturn("Not Found");
|
||||
expect(response.getHeaders()).andReturn(headers);
|
||||
expect(response.getBody()).andReturn(null);
|
||||
|
||||
replay(response);
|
||||
|
||||
handler.handleError(response);
|
||||
|
||||
verify(response);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue