replace empty HttpResponseException.message with statusCode

This commit is contained in:
Alex Panchenko 2018-12-19 11:37:46 +02:00
parent a0c20ec14d
commit 20dfff12c4
2 changed files with 26 additions and 1 deletions

View File

@ -26,6 +26,8 @@
*/ */
package org.apache.http.client; package org.apache.http.client;
import org.apache.http.util.TextUtils;
/** /**
* Signals a non 2xx HTTP response. * Signals a non 2xx HTTP response.
* *
@ -38,7 +40,7 @@ public class HttpResponseException extends ClientProtocolException {
private final int statusCode; private final int statusCode;
public HttpResponseException(final int statusCode, final String s) { public HttpResponseException(final int statusCode, final String s) {
super(s); super(TextUtils.isBlank(s) ? Integer.toString(statusCode) : s);
this.statusCode = statusCode; this.statusCode = statusCode;
} }

View File

@ -90,4 +90,27 @@ public void testUnsuccessfulResponse() throws Exception {
Mockito.verify(inStream).close(); 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 StatusLine sl = new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "");
final HttpResponse response = Mockito.mock(HttpResponse.class);
Mockito.when(response.getStatusLine()).thenReturn(sl);
Mockito.when(response.getEntity()).thenReturn(entity);
final BasicResponseHandler handler = new BasicResponseHandler();
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();
}
} }