Merge pull request #127 from panchenko/empty_reason_phrase_master

replace empty HttpResponseException.message with statusCode
This commit is contained in:
Oleg Kalnichevski 2018-12-19 16:47:16 +01:00 committed by GitHub
commit 59ee896faa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -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;
}

View File

@ -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();
}
}