diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java index f4d032ef84..467a4767c1 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,12 +47,13 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { */ @Override public boolean hasError(ClientHttpResponse response) throws IOException { - try { - return hasError(getHttpStatusCode(response)); - } - catch (UnknownHttpStatusCodeException ex) { - return false; + int rawStatusCode = response.getRawStatusCode(); + for (HttpStatus statusCode : HttpStatus.values()) { + if (statusCode.value() == rawStatusCode) { + return hasError(statusCode); + } } + return false; } /** @@ -87,13 +88,15 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); default: - throw new RestClientException("Unknown status code [" + statusCode + "]"); + throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(), + response.getHeaders(), getResponseBody(response), getCharset(response)); } } /** * Determine the HTTP status of the given response. + *
Note: Only called from {@link #handleError}, not from {@link #hasError}. * @param response the response to inspect * @return the associated HTTP status * @throws IOException in case of I/O errors diff --git a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java index 950a21fdf1..1c5e911ad6 100644 --- a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.web.client; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.charset.Charset; import org.junit.Test; @@ -25,6 +26,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; +import org.springframework.util.StreamUtils; import static org.junit.Assert.*; import static org.mockito.BDDMockito.*; @@ -34,9 +36,12 @@ import static org.mockito.BDDMockito.*; * * @author Arjen Poutsma * @author Juergen Hoeller + * @author Denys Ivano */ public class DefaultResponseErrorHandlerTests { + private final Charset UTF8 = Charset.forName("UTF-8"); + private final DefaultResponseErrorHandler handler = new DefaultResponseErrorHandler(); private final ClientHttpResponse response = mock(ClientHttpResponse.class); @@ -44,13 +49,13 @@ public class DefaultResponseErrorHandlerTests { @Test public void hasErrorTrue() throws Exception { - given(response.getStatusCode()).willReturn(HttpStatus.NOT_FOUND); + given(response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); assertTrue(handler.hasError(response)); } @Test public void hasErrorFalse() throws Exception { - given(response.getStatusCode()).willReturn(HttpStatus.OK); + given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); assertFalse(handler.hasError(response)); } @@ -115,11 +120,63 @@ public class DefaultResponseErrorHandlerTests { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); - given(response.getStatusCode()).willThrow(new IllegalArgumentException("No matching constant for 999")); + given(response.getRawStatusCode()).willReturn(999); given(response.getStatusText()).willReturn("Custom status code"); given(response.getHeaders()).willReturn(headers); assertFalse(handler.hasError(response)); } + @Test // SPR-16604 + public void bodyAvailableAfterHasErrorForUnknownStatusCode() throws Exception { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.TEXT_PLAIN); + TestByteArrayInputStream body = new TestByteArrayInputStream("Hello World".getBytes("UTF-8")); + + given(response.getRawStatusCode()).willReturn(999); + given(response.getStatusText()).willReturn("Custom status code"); + given(response.getHeaders()).willReturn(headers); + given(response.getBody()).willReturn(body); + + assertFalse(handler.hasError(response)); + assertFalse(body.isClosed()); + assertEquals("Hello World", StreamUtils.copyToString(response.getBody(), UTF8)); + } + + + private static class TestByteArrayInputStream extends ByteArrayInputStream { + + private boolean closed; + + public TestByteArrayInputStream(byte[] buf) { + super(buf); + this.closed = false; + } + + public boolean isClosed() { + return closed; + } + + @Override + public boolean markSupported() { + return false; + } + + @Override + public synchronized void mark(int readlimit) { + throw new UnsupportedOperationException("mark/reset not supported"); + } + + @Override + public synchronized void reset() { + throw new UnsupportedOperationException("mark/reset not supported"); + } + + @Override + public void close() throws IOException { + super.close(); + this.closed = true; + } + } + }