Added test cases for HttpClientUtils
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1405511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e9ca02ccff
commit
87a46db5c2
|
@ -31,11 +31,13 @@ import java.io.IOException;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static helpers for dealing with {@link HttpResponse}s.
|
* Static helpers for dealing with {@link HttpResponse}s.
|
||||||
*
|
*
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
public class HttpClientUtils {
|
public class HttpClientUtils {
|
||||||
|
@ -47,7 +49,7 @@ public class HttpClientUtils {
|
||||||
* Unconditionally close a response.
|
* Unconditionally close a response.
|
||||||
* <p>
|
* <p>
|
||||||
* Example Code:
|
* Example Code:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* HttpResponse httpResponse = null;
|
* HttpResponse httpResponse = null;
|
||||||
* try {
|
* try {
|
||||||
|
@ -58,11 +60,11 @@ public class HttpClientUtils {
|
||||||
* HttpClientUtils.closeQuietly(httpResponse);
|
* HttpClientUtils.closeQuietly(httpResponse);
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param response
|
* @param response
|
||||||
* the HttpResponse to release resources, may be null or already
|
* the HttpResponse to release resources, may be null or already
|
||||||
* closed.
|
* closed.
|
||||||
*
|
*
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
public static void closeQuietly(final HttpResponse response) {
|
public static void closeQuietly(final HttpResponse response) {
|
||||||
|
@ -77,12 +79,47 @@ public class HttpClientUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unconditionally close a response.
|
||||||
|
* <p>
|
||||||
|
* Example Code:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* HttpResponse httpResponse = null;
|
||||||
|
* try {
|
||||||
|
* httpResponse = httpClient.execute(httpGet);
|
||||||
|
* } catch (Exception e) {
|
||||||
|
* // error handling
|
||||||
|
* } finally {
|
||||||
|
* HttpClientUtils.closeQuietly(httpResponse);
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param response
|
||||||
|
* the HttpResponse to release resources, may be null or already
|
||||||
|
* closed.
|
||||||
|
*
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
public static void closeQuietly(final CloseableHttpResponse response) {
|
||||||
|
if (response != null) {
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
EntityUtils.consume(response.getEntity());
|
||||||
|
} finally {
|
||||||
|
response.close();
|
||||||
|
}
|
||||||
|
} catch (final IOException ex) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unconditionally close a httpClient. Shuts down the underlying connection
|
* Unconditionally close a httpClient. Shuts down the underlying connection
|
||||||
* manager and releases the resources.
|
* manager and releases the resources.
|
||||||
* <p>
|
* <p>
|
||||||
* Example Code:
|
* Example Code:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* HttpClient httpClient = null;
|
* HttpClient httpClient = null;
|
||||||
* try {
|
* try {
|
||||||
|
@ -93,7 +130,7 @@ public class HttpClientUtils {
|
||||||
* HttpClientUtils.closeQuietly(httpClient);
|
* HttpClientUtils.closeQuietly(httpClient);
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param httpClient
|
* @param httpClient
|
||||||
* the HttpClient to close, may be null or already closed.
|
* the HttpClient to close, may be null or already closed.
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
|
@ -107,4 +144,34 @@ public class HttpClientUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unconditionally close a httpClient. Shuts down the underlying connection
|
||||||
|
* manager and releases the resources.
|
||||||
|
* <p>
|
||||||
|
* Example Code:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
|
* try {
|
||||||
|
* ...
|
||||||
|
* } catch (Exception e) {
|
||||||
|
* // error handling
|
||||||
|
* } finally {
|
||||||
|
* HttpClientUtils.closeQuietly(httpClient);
|
||||||
|
* }
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param httpClient
|
||||||
|
* the HttpClient to close, may be null or already closed.
|
||||||
|
* @since 4.3
|
||||||
|
*/
|
||||||
|
public static void closeQuietly(final CloseableHttpClient httpClient) {
|
||||||
|
if (httpClient != null) {
|
||||||
|
try {
|
||||||
|
httpClient.close();
|
||||||
|
} catch (final IOException ex) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,15 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.http.client.utils;
|
package org.apache.http.client.utils;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
public class TestHttpClientUtils {
|
public class TestHttpClientUtils {
|
||||||
|
|
||||||
|
@ -37,4 +44,113 @@ public class TestHttpClientUtils {
|
||||||
HttpClientUtils.closeQuietly(response);
|
HttpClientUtils.closeQuietly(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyResponseEntityNull() throws Exception {
|
||||||
|
HttpResponse response = Mockito.mock(HttpResponse.class);
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
Mockito.verify(response).getEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyResponseEntityNonStreaming() throws Exception {
|
||||||
|
HttpResponse response = Mockito.mock(HttpResponse.class);
|
||||||
|
HttpEntity entity = Mockito.mock(HttpEntity.class);
|
||||||
|
Mockito.when(response.getEntity()).thenReturn(entity);
|
||||||
|
Mockito.when(entity.isStreaming()).thenReturn(false);
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
Mockito.verify(entity, Mockito.never()).getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyResponseEntity() throws Exception {
|
||||||
|
HttpResponse response = Mockito.mock(HttpResponse.class);
|
||||||
|
HttpEntity entity = Mockito.mock(HttpEntity.class);
|
||||||
|
InputStream instream = Mockito.mock(InputStream.class);
|
||||||
|
Mockito.when(response.getEntity()).thenReturn(entity);
|
||||||
|
Mockito.when(entity.isStreaming()).thenReturn(true);
|
||||||
|
Mockito.when(entity.getContent()).thenReturn(instream);
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
Mockito.verify(instream).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyResponseIgnoreIOError() throws Exception {
|
||||||
|
HttpResponse response = Mockito.mock(HttpResponse.class);
|
||||||
|
HttpEntity entity = Mockito.mock(HttpEntity.class);
|
||||||
|
InputStream instream = Mockito.mock(InputStream.class);
|
||||||
|
Mockito.when(response.getEntity()).thenReturn(entity);
|
||||||
|
Mockito.when(entity.getContent()).thenReturn(instream);
|
||||||
|
Mockito.doThrow(new IOException()).when(instream).close();
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyCloseableResponseNull() throws Exception {
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyCloseableResponseEntityNull() throws Exception {
|
||||||
|
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
Mockito.verify(response).getEntity();
|
||||||
|
Mockito.verify(response).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyCloseableResponseEntityNonStreaming() throws Exception {
|
||||||
|
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
|
||||||
|
HttpEntity entity = Mockito.mock(HttpEntity.class);
|
||||||
|
Mockito.when(response.getEntity()).thenReturn(entity);
|
||||||
|
Mockito.when(entity.isStreaming()).thenReturn(false);
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
Mockito.verify(entity, Mockito.never()).getContent();
|
||||||
|
Mockito.verify(response).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyCloseableResponseEntity() throws Exception {
|
||||||
|
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
|
||||||
|
HttpEntity entity = Mockito.mock(HttpEntity.class);
|
||||||
|
InputStream instream = Mockito.mock(InputStream.class);
|
||||||
|
Mockito.when(response.getEntity()).thenReturn(entity);
|
||||||
|
Mockito.when(entity.isStreaming()).thenReturn(true);
|
||||||
|
Mockito.when(entity.getContent()).thenReturn(instream);
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
Mockito.verify(instream).close();
|
||||||
|
Mockito.verify(response).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyCloseableResponseIgnoreIOError() throws Exception {
|
||||||
|
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
|
||||||
|
HttpEntity entity = Mockito.mock(HttpEntity.class);
|
||||||
|
InputStream instream = Mockito.mock(InputStream.class);
|
||||||
|
Mockito.when(response.getEntity()).thenReturn(entity);
|
||||||
|
Mockito.when(entity.getContent()).thenReturn(instream);
|
||||||
|
Mockito.doThrow(new IOException()).when(instream).close();
|
||||||
|
HttpClientUtils.closeQuietly(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyHttpClientNull() throws Exception {
|
||||||
|
CloseableHttpClient httpclient = null;
|
||||||
|
HttpClientUtils.closeQuietly(httpclient);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyHttpClient() throws Exception {
|
||||||
|
CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class);
|
||||||
|
HttpClientUtils.closeQuietly(httpclient);
|
||||||
|
Mockito.verify(httpclient).close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseQuietlyCloseableHttpClientIgnoreIOError() throws Exception {
|
||||||
|
CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class);
|
||||||
|
Mockito.doThrow(new IOException()).when(httpclient).close();
|
||||||
|
HttpClientUtils.closeQuietly(httpclient);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue