hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1414671 - in /httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http: impl/client/integration/ localserver/
Date Wed, 28 Nov 2012 13:34:20 GMT
Author: olegk
Date: Wed Nov 28 13:34:18 2012
New Revision: 1414671

URL: http://svn.apache.org/viewvc?rev=1414671&view=rev
Log:
Refactored local HTTP server for integration tests

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java?rev=1414671&r1=1414670&r2=1414671&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
Wed Nov 28 13:34:18 2012
@@ -60,9 +60,10 @@ import org.apache.http.localserver.Reque
 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 ex
 
     @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 ex
 
     @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();
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java?rev=1414671&r1=1414670&r2=1414671&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFallBack.java
Wed Nov 28 13:34:18 2012
@@ -44,8 +44,9 @@ import org.apache.http.entity.StringEnti
 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 TestClientAuthenticationFal
 
     @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();
     }

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java?rev=1414671&r1=1414670&r2=1414671&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientReauthentication.java
Wed Nov 28 13:34:18 2012
@@ -53,8 +53,9 @@ import org.apache.http.localserver.Local
 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 
 
     @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();

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java?rev=1414671&r1=1414670&r2=1414671&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionAutoRelease.java
Wed Nov 28 13:34:18 2012
@@ -44,9 +44,9 @@ import org.apache.http.conn.ConnectionPo
 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 e
 
                         // do something comletely ugly in order to trigger
                         // MalformedChunkCodingException
-                        DefaultHttpServerConnection conn = (DefaultHttpServerConnection)
+                        DefaultBHttpServerConnection conn = (DefaultBHttpServerConnection)
                             context.getAttribute(ExecutionContext.HTTP_CONNECTION);
                         try {
                             conn.sendResponseHeader(response);

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java?rev=1414671&r1=1414670&r2=1414671&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java
Wed Nov 28 13:34:18 2012
@@ -53,6 +53,7 @@ public abstract class LocalServerTestBas
             localServer = new LocalTestServer(null, null);
             localServer.registerDefaultHandlers();
         }
+        localServer.setTimeout(5000);
         localServer.start();
     }
     

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java?rev=1414671&r1=1414670&r2=1414671&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java
Wed Nov 28 13:34:18 2012
@@ -43,26 +43,21 @@ import org.apache.http.ConnectionReuseSt
 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.Response
  */
 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 <code>null</code> to use
      *                  {@link #newConnectionReuseStrategy() default}
      *                  strategy.
-     * @param params    the parameters to be used by the server, or
-     *                  <code>null</code> to use
-     *                  {@link #newDefaultParams default} parameters
+     * @param responseFactory the response factory to be used by the
+     *                  server, or <code>null</code> to use
+     *                  {@link #newHttpResponseFactory() default} factory.
+     * @param expectationVerifier. the expectation verifier. May be
+     *                  <code>null</code>.
      * @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<Worker>());
         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
-     *                  <code>null</code> to use a
-     *                  {@link #newProcessor default} processor
-     * @param params    the parameters to be used by the server, or
-     *                  <code>null</code> 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.
      * <p>
      * 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);



Mime
View raw message