diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java b/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java index 4d9b8edac..fab64d797 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java @@ -26,6 +26,8 @@ */ package org.apache.hc.client5.http; +import org.apache.hc.core5.util.TextUtils; + /** * Signals a non 2xx HTTP response. * @@ -38,7 +40,7 @@ public class HttpResponseException extends ClientProtocolException { private final int statusCode; public HttpResponseException(final int statusCode, final String s) { - super(s); + super(TextUtils.isBlank(s) ? Integer.toString(statusCode) : s); this.statusCode = statusCode; } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java index 0f0b23df2..d3e9c53bb 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java @@ -71,6 +71,7 @@ public class TestAbstractHttpClientResponseHandler { Mockito.when(entity.getContent()).thenReturn(inStream); final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); Mockito.when(response.getCode()).thenReturn(404); + Mockito.when(response.getReasonPhrase()).thenReturn("NOT FOUND"); Mockito.when(response.getEntity()).thenReturn(entity); final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); @@ -79,9 +80,32 @@ public class TestAbstractHttpClientResponseHandler { Assert.fail("HttpResponseException expected"); } catch (final HttpResponseException ex) { Assert.assertEquals(404, ex.getStatusCode()); + Assert.assertEquals("NOT FOUND", ex.getMessage()); } Mockito.verify(entity).getContent(); Mockito.verify(inStream).close(); } + @SuppressWarnings("boxing") + @Test + public void testUnsuccessfulResponseEmptyReason() throws Exception { + final InputStream inStream = Mockito.mock(InputStream.class); + final HttpEntity entity = Mockito.mock(HttpEntity.class); + Mockito.when(entity.isStreaming()).thenReturn(true); + Mockito.when(entity.getContent()).thenReturn(inStream); + final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); + Mockito.when(response.getCode()).thenReturn(404); + Mockito.when(response.getEntity()).thenReturn(entity); + + final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); + try { + handler.handleResponse(response); + Assert.fail("HttpResponseException expected"); + } catch (final HttpResponseException ex) { + Assert.assertEquals(404, ex.getStatusCode()); + Assert.assertEquals("404", ex.getMessage()); + } + Mockito.verify(entity).getContent(); + Mockito.verify(inStream).close(); + } }