From 8dccdcc7c886ef8fb992c83159217b6cdc66138f Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 28 Nov 2012 13:34:18 +0000 Subject: [PATCH] Refactored local HTTP server for integration tests git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1414671 13f79535-47bb-0310-9956-ffa450edef68 --- .../integration/TestClientAuthentication.java | 33 +++--- .../TestClientAuthenticationFallBack.java | 18 +-- .../TestClientReauthentication.java | 17 +-- .../TestConnectionAutoRelease.java | 4 +- .../http/localserver/LocalServerTestBase.java | 1 + .../http/localserver/LocalTestServer.java | 106 +++++++----------- 6 files changed, 80 insertions(+), 99 deletions(-) diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java index e9fed8217..6ada354c5 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java @@ -60,9 +60,10 @@ import org.apache.http.localserver.RequestBasicAuth; import org.apache.http.localserver.ResponseBasicUnauthorized; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.BasicHttpProcessor; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpExpectationVerifier; +import org.apache.http.protocol.HttpProcessor; +import org.apache.http.protocol.HttpProcessorBuilder; import org.apache.http.protocol.HttpRequestHandler; import org.apache.http.protocol.ResponseConnControl; import org.apache.http.protocol.ResponseContent; @@ -80,13 +81,13 @@ public class TestClientAuthentication extends IntegrationTestBase { @Before public void setUp() throws Exception { - BasicHttpProcessor httpproc = new BasicHttpProcessor(); - httpproc.addInterceptor(new ResponseDate()); - httpproc.addInterceptor(new ResponseServer()); - httpproc.addInterceptor(new ResponseContent()); - httpproc.addInterceptor(new ResponseConnControl()); - httpproc.addInterceptor(new RequestBasicAuth()); - httpproc.addInterceptor(new ResponseBasicUnauthorized()); + HttpProcessor httpproc = HttpProcessorBuilder.create() + .add(new ResponseDate()) + .add(new ResponseServer()) + .add(new ResponseContent()) + .add(new ResponseConnControl()) + .add(new RequestBasicAuth()) + .add(new ResponseBasicUnauthorized()).build(); this.localServer = new LocalTestServer(httpproc, null); startServer(); } @@ -222,15 +223,15 @@ public class TestClientAuthentication extends IntegrationTestBase { @Test public void testBasicAuthenticationSuccessOnNonRepeatablePutExpectContinue() throws Exception { - BasicHttpProcessor httpproc = new BasicHttpProcessor(); - httpproc.addInterceptor(new ResponseDate()); - httpproc.addInterceptor(new ResponseServer()); - httpproc.addInterceptor(new ResponseContent()); - httpproc.addInterceptor(new ResponseConnControl()); - httpproc.addInterceptor(new RequestBasicAuth()); - httpproc.addInterceptor(new ResponseBasicUnauthorized()); + HttpProcessor httpproc = HttpProcessorBuilder.create() + .add(new ResponseDate()) + .add(new ResponseServer(LocalTestServer.ORIGIN)) + .add(new ResponseContent()) + .add(new ResponseConnControl()) + .add(new RequestBasicAuth()) + .add(new ResponseBasicUnauthorized()).build(); this.localServer = new LocalTestServer( - httpproc, null, null, new AuthExpectationVerifier(), null, null); + httpproc, null, null, new AuthExpectationVerifier(), null); this.localServer.register("*", new AuthHandler()); this.localServer.start(); diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java index 00f308fc0..e767fbfad 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java @@ -44,8 +44,9 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.localserver.LocalTestServer; import org.apache.http.localserver.RequestBasicAuth; -import org.apache.http.protocol.BasicHttpProcessor; import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpProcessor; +import org.apache.http.protocol.HttpProcessorBuilder; import org.apache.http.protocol.HttpRequestHandler; import org.apache.http.protocol.ResponseConnControl; import org.apache.http.protocol.ResponseContent; @@ -73,14 +74,13 @@ public class TestClientAuthenticationFallBack extends IntegrationTestBase { @Before public void setUp() throws Exception { - BasicHttpProcessor httpproc = new BasicHttpProcessor(); - httpproc.addInterceptor(new ResponseDate()); - httpproc.addInterceptor(new ResponseServer()); - httpproc.addInterceptor(new ResponseContent()); - httpproc.addInterceptor(new ResponseConnControl()); - httpproc.addInterceptor(new RequestBasicAuth()); - httpproc.addInterceptor(new ResponseBasicUnauthorized()); - + HttpProcessor httpproc = HttpProcessorBuilder.create() + .add(new ResponseDate()) + .add(new ResponseServer(LocalTestServer.ORIGIN)) + .add(new ResponseContent()) + .add(new ResponseConnControl()) + .add(new RequestBasicAuth()) + .add(new ResponseBasicUnauthorized()).build(); this.localServer = new LocalTestServer(httpproc, null); startServer(); } diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java index 59c8b6427..65ae705ae 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java @@ -53,8 +53,9 @@ import org.apache.http.localserver.LocalTestServer; import org.apache.http.localserver.RequestBasicAuth; import org.apache.http.params.HttpParams; import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.BasicHttpProcessor; import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpProcessor; +import org.apache.http.protocol.HttpProcessorBuilder; import org.apache.http.protocol.HttpRequestHandler; import org.apache.http.protocol.ResponseConnControl; import org.apache.http.protocol.ResponseContent; @@ -81,13 +82,13 @@ public class TestClientReauthentication extends IntegrationTestBase { @Before public void setUp() throws Exception { - BasicHttpProcessor httpproc = new BasicHttpProcessor(); - httpproc.addInterceptor(new ResponseDate()); - httpproc.addInterceptor(new ResponseServer()); - httpproc.addInterceptor(new ResponseContent()); - httpproc.addInterceptor(new ResponseConnControl()); - httpproc.addInterceptor(new RequestBasicAuth()); - httpproc.addInterceptor(new ResponseBasicUnauthorized()); + HttpProcessor httpproc = HttpProcessorBuilder.create() + .add(new ResponseDate()) + .add(new ResponseServer(LocalTestServer.ORIGIN)) + .add(new ResponseContent()) + .add(new ResponseConnControl()) + .add(new RequestBasicAuth()) + .add(new ResponseBasicUnauthorized()).build(); this.localServer = new LocalTestServer(httpproc, null); startServer(); diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java index 5daa701e5..ecd1500ea 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java @@ -44,9 +44,9 @@ import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.ConnectionRequest; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.entity.BasicHttpEntity; -import org.apache.http.impl.DefaultHttpServerConnection; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.impl.DefaultBHttpServerConnection; import org.apache.http.pool.PoolStats; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; @@ -197,7 +197,7 @@ public class TestConnectionAutoRelease extends IntegrationTestBase { // do something comletely ugly in order to trigger // MalformedChunkCodingException - DefaultHttpServerConnection conn = (DefaultHttpServerConnection) + DefaultBHttpServerConnection conn = (DefaultBHttpServerConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION); try { conn.sendResponseHeader(response); diff --git a/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java b/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java index 5da3cd4b7..5f3720f54 100644 --- a/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java +++ b/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java @@ -53,6 +53,7 @@ public abstract class LocalServerTestBase { localServer = new LocalTestServer(null, null); localServer.registerDefaultHandlers(); } + localServer.setTimeout(5000); localServer.start(); } diff --git a/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java b/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java index 39e81c4eb..5200df298 100644 --- a/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java +++ b/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java @@ -43,26 +43,21 @@ import org.apache.http.ConnectionReuseStrategy; import org.apache.http.HttpResponseFactory; import org.apache.http.HttpResponseInterceptor; import org.apache.http.HttpServerConnection; +import org.apache.http.impl.DefaultBHttpServerConnection; import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.DefaultHttpResponseFactory; -import org.apache.http.impl.DefaultHttpServerConnection; -import org.apache.http.params.CoreConnectionPNames; -import org.apache.http.params.HttpParams; -import org.apache.http.params.CoreProtocolPNames; -import org.apache.http.params.SyncBasicHttpParams; -import org.apache.http.protocol.BasicHttpProcessor; -import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpExpectationVerifier; import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpRequestHandler; -import org.apache.http.protocol.HttpRequestHandlerRegistry; import org.apache.http.protocol.HttpService; import org.apache.http.protocol.ImmutableHttpProcessor; import org.apache.http.protocol.ResponseConnControl; import org.apache.http.protocol.ResponseContent; import org.apache.http.protocol.ResponseDate; import org.apache.http.protocol.ResponseServer; +import org.apache.http.protocol.UriHttpRequestHandlerMapper; /** * Local HTTP server for tests that require one. @@ -70,6 +65,8 @@ import org.apache.http.protocol.ResponseServer; */ public class LocalTestServer { + public final static String ORIGIN = "LocalTestServer/1.1"; + /** * The local address to bind to. * The host is an IP number rather than "localhost" to avoid surprises @@ -80,7 +77,7 @@ public class LocalTestServer { new InetSocketAddress("127.0.0.1", 0); /** The request handler registry. */ - private final HttpRequestHandlerRegistry handlerRegistry; + private final UriHttpRequestHandlerMapper handlerRegistry; private final HttpService httpservice; @@ -99,6 +96,8 @@ public class LocalTestServer { /** The number of connections this accepted. */ private final AtomicInteger acceptedConnections = new AtomicInteger(0); + private volatile int timeout; + /** * Creates a new test server. * @@ -109,55 +108,45 @@ public class LocalTestServer { * server, or null to use * {@link #newConnectionReuseStrategy() default} * strategy. - * @param params the parameters to be used by the server, or - * null to use - * {@link #newDefaultParams default} parameters + * @param responseFactory the response factory to be used by the + * server, or null to use + * {@link #newHttpResponseFactory() default} factory. + * @param expectationVerifier. the expectation verifier. May be + * null. * @param sslcontext optional SSL context if the server is to leverage * SSL/TLS transport security */ public LocalTestServer( - final BasicHttpProcessor proc, + final HttpProcessor proc, final ConnectionReuseStrategy reuseStrat, final HttpResponseFactory responseFactory, final HttpExpectationVerifier expectationVerifier, - final HttpParams params, final SSLContext sslcontext) { super(); - this.handlerRegistry = new HttpRequestHandlerRegistry(); + this.handlerRegistry = new UriHttpRequestHandlerMapper(); this.workers = Collections.synchronizedSet(new HashSet()); this.httpservice = new HttpService( proc != null ? proc : newProcessor(), reuseStrat != null ? reuseStrat: newConnectionReuseStrategy(), responseFactory != null ? responseFactory: newHttpResponseFactory(), handlerRegistry, - expectationVerifier, - params != null ? params : newDefaultParams()); + expectationVerifier); this.sslcontext = sslcontext; } + public LocalTestServer( + final HttpProcessor proc, + final ConnectionReuseStrategy reuseStrat) { + this(proc, reuseStrat, null, null, null); + } + /** * Creates a new test server with SSL/TLS encryption. * * @param sslcontext SSL context */ public LocalTestServer(final SSLContext sslcontext) { - this(null, null, null, null, null, sslcontext); - } - - /** - * Creates a new test server. - * - * @param proc the HTTP processors to be used by the server, or - * null to use a - * {@link #newProcessor default} processor - * @param params the parameters to be used by the server, or - * null to use - * {@link #newDefaultParams default} parameters - */ - public LocalTestServer( - BasicHttpProcessor proc, - HttpParams params) { - this(proc, null, null, null, params, null); + this(null, null, null, null, sslcontext); } /** @@ -169,36 +158,18 @@ public class LocalTestServer { return new ImmutableHttpProcessor( new HttpResponseInterceptor[] { new ResponseDate(), - new ResponseServer(), + new ResponseServer(ORIGIN), new ResponseContent(), new ResponseConnControl() }); } - - /** - * Obtains a set of reasonable default parameters for a server. - * - * @return default parameters - */ - protected HttpParams newDefaultParams() { - HttpParams params = new SyncBasicHttpParams(); - params - .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000) - .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024) - .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) - .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true) - .setParameter(CoreProtocolPNames.ORIGIN_SERVER, - "LocalTestServer/1.1"); - return params; - } - protected ConnectionReuseStrategy newConnectionReuseStrategy() { - return new DefaultConnectionReuseStrategy(); + return DefaultConnectionReuseStrategy.INSTANCE; } protected HttpResponseFactory newHttpResponseFactory() { - return new DefaultHttpResponseFactory(); + return DefaultHttpResponseFactory.INSTANCE; } /** @@ -222,7 +193,6 @@ public class LocalTestServer { handlerRegistry.register("/random/*", new RandomHandler()); } - /** * Registers a handler with the local registry. * @@ -233,7 +203,6 @@ public class LocalTestServer { handlerRegistry.register(pattern, handler); } - /** * Unregisters a handler from the local registry. * @@ -243,6 +212,13 @@ public class LocalTestServer { handlerRegistry.unregister(pattern); } + public int getTimeout() { + return timeout; + } + + public void setTimeout(int timeout) { + this.timeout = timeout; + } /** * Starts this test server. @@ -259,6 +235,7 @@ public class LocalTestServer { ssock = new ServerSocket(); } + ssock.setSoTimeout(timeout); ssock.setReuseAddress(true); // probably pointless for port '0' ssock.bind(TEST_SERVER_ADDR); servicedSocket = ssock; @@ -318,16 +295,16 @@ public class LocalTestServer { } /** - * Creates an instance of {@link DefaultHttpServerConnection} to be used + * Creates an instance of {@link DefaultBHttpServerConnection} to be used * in the Worker thread. *

* This method can be overridden in a super class in order to provide - * a different implementation of the {@link DefaultHttpServerConnection}. + * a different implementation of the {@link DefaultBHttpServerConnection}. * - * @return DefaultHttpServerConnection. + * @return DefaultBHttpServerConnection. */ - protected DefaultHttpServerConnection createHttpServerConnection() { - return new DefaultHttpServerConnection(); + protected DefaultBHttpServerConnection createHttpServerConnection() { + return new DefaultBHttpServerConnection(8 * 1024); } /** @@ -348,8 +325,9 @@ public class LocalTestServer { while (!interrupted()) { Socket socket = servicedSocket.accept(); acceptedConnections.incrementAndGet(); - DefaultHttpServerConnection conn = createHttpServerConnection(); - conn.bind(socket, httpservice.getParams()); + DefaultBHttpServerConnection conn = createHttpServerConnection(); + conn.bind(socket); + conn.setSocketTimeout(timeout); // Start worker thread Worker worker = new Worker(conn); workers.add(worker);