Simulate connect failure

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1059107 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-01-14 18:55:36 +00:00
parent 8e914ae112
commit a877471bb9
1 changed files with 27 additions and 4 deletions

View File

@ -26,25 +26,31 @@
package org.apache.http.impl.client; package org.apache.http.impl.client;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SchemeSocketFactory;
import org.apache.http.impl.conn.SingleClientConnManager; import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class TestRequestRetryHandler { public class TestRequestRetryHandler {
@Test(expected=UnknownHostException.class) @Test(expected=HttpHostConnectException.class)
public void testUseRetryHandlerInConnectionTimeOutWithThreadSafeClientConnManager() public void testUseRetryHandlerInConnectionTimeOutWithThreadSafeClientConnManager()
throws Exception { throws Exception {
@ -55,7 +61,7 @@ public class TestRequestRetryHandler {
assertOnRetry(connManager); assertOnRetry(connManager);
} }
@Test(expected=UnknownHostException.class) @Test(expected=HttpHostConnectException.class)
public void testUseRetryHandlerInConnectionTimeOutWithSingleClientConnManager() public void testUseRetryHandlerInConnectionTimeOutWithSingleClientConnManager()
throws Exception { throws Exception {
@ -67,13 +73,30 @@ public class TestRequestRetryHandler {
protected void assertOnRetry(ClientConnectionManager connManager) throws Exception { protected void assertOnRetry(ClientConnectionManager connManager) throws Exception {
SchemeRegistry schemeRegistry = new SchemeRegistry(); SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("http", 80, new SchemeSocketFactory() {
public boolean isSecure(final Socket sock) throws IllegalArgumentException {
return false;
}
public Socket createSocket(final HttpParams params) throws IOException {
throw new UnknownHostException("Ooopsie");
}
public Socket connectSocket(
final Socket sock,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
throw new UnknownHostException("Ooopsie");
}
}));
DefaultHttpClient client = new DefaultHttpClient(connManager); DefaultHttpClient client = new DefaultHttpClient(connManager);
TestHttpRequestRetryHandler testRetryHandler = new TestHttpRequestRetryHandler(); TestHttpRequestRetryHandler testRetryHandler = new TestHttpRequestRetryHandler();
client.setHttpRequestRetryHandler(testRetryHandler); client.setHttpRequestRetryHandler(testRetryHandler);
HttpRequestBase request = new HttpGet("http://bogus.example.com/"); HttpRequestBase request = new HttpGet("http://www.example.com/");
HttpConnectionParams.setConnectionTimeout(request.getParams(), 1); HttpConnectionParams.setConnectionTimeout(request.getParams(), 1);
try { try {