replace empty HttpResponseException.message with statusCode

(cherry picked from commit 20dfff12c4)
This commit is contained in:
Alex Panchenko 2018-12-19 11:37:46 +02:00
parent 6b410fe0e7
commit 6275a39c00
2 changed files with 27 additions and 1 deletions

View File

@ -26,6 +26,8 @@
*/ */
package org.apache.hc.client5.http; package org.apache.hc.client5.http;
import org.apache.hc.core5.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

@ -71,6 +71,7 @@ public class TestAbstractHttpClientResponseHandler {
Mockito.when(entity.getContent()).thenReturn(inStream); Mockito.when(entity.getContent()).thenReturn(inStream);
final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
Mockito.when(response.getCode()).thenReturn(404); Mockito.when(response.getCode()).thenReturn(404);
Mockito.when(response.getReasonPhrase()).thenReturn("NOT FOUND");
Mockito.when(response.getEntity()).thenReturn(entity); Mockito.when(response.getEntity()).thenReturn(entity);
final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler();
@ -79,9 +80,32 @@ public class TestAbstractHttpClientResponseHandler {
Assert.fail("HttpResponseException expected"); Assert.fail("HttpResponseException expected");
} catch (final HttpResponseException ex) { } catch (final HttpResponseException ex) {
Assert.assertEquals(404, ex.getStatusCode()); Assert.assertEquals(404, ex.getStatusCode());
Assert.assertEquals("NOT FOUND", ex.getMessage());
} }
Mockito.verify(entity).getContent(); Mockito.verify(entity).getContent();
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 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();
}
} }