hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r758645 [4/5] - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/ test/java/org/apache/http/impl/nio/reactor/ test/java/org/apache/http/mockup/ test/java/org/apache/http/n...
Date Thu, 26 Mar 2009 13:47:41 GMT
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNIOSSLHttp.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNIOSSLHttp.java?rev=758645&r1=758644&r2=758645&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNIOSSLHttp.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNIOSSLHttp.java
Thu Mar 26 13:47:19 2009
@@ -30,43 +30,38 @@
 
 package org.apache.http.nio.protocol;
 
-import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.net.InetSocketAddress;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.apache.http.HttpCoreNIOSSLTestBase;
-import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntityEnclosingRequest;
-import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
-import org.apache.http.entity.ByteArrayEntity;
-import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
 import org.apache.http.message.BasicHttpEntityEnclosingRequest;
 import org.apache.http.message.BasicHttpRequest;
-import org.apache.http.mockup.ByteSequence;
-import org.apache.http.mockup.RequestCount;
 import org.apache.http.mockup.SimpleEventListener;
-import org.apache.http.nio.NHttpClientHandler;
-import org.apache.http.nio.NHttpConnection;
-import org.apache.http.nio.NHttpServiceHandler;
-import org.apache.http.nio.entity.NByteArrayEntity;
-import org.apache.http.nio.entity.NHttpEntityWrapper;
+import org.apache.http.mockup.SimpleNHttpRequestHandlerResolver;
 import org.apache.http.nio.entity.NStringEntity;
-import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
 import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.CoreProtocolPNames;
-import org.apache.http.protocol.ExecutionContext;
-import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.util.EntityUtils;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
 
 /**
  * HttpCore NIO SSL tests.
@@ -93,102 +88,52 @@
         return new TestSuite(TestNIOSSLHttp.class);
     }
 
-    /**
-     * This test case executes a series of simple (non-pipelined) GET requests 
-     * over multiple connections. 
-     */
-    public void testSimpleHttpGets() throws Exception {
-        final int connNo = 3;
-        final int reqNo = 20;
-        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
-        
-        final ByteSequence requestData = new ByteSequence();
-        requestData.rnd(reqNo);
-        
-        List<ByteSequence> responseData = new ArrayList<ByteSequence>(connNo);
-        for (int i = 0; i < connNo; i++) {
-            responseData.add(new ByteSequence());
-        }
-        
-        HttpRequestHandler requestHandler = new HttpRequestHandler() {
-
-            public void handle(
-                    final HttpRequest request, 
-                    final HttpResponse response, 
-                    final HttpContext context) throws HttpException, IOException {
-                
-                String s = request.getRequestLine().getUri();
-                URI uri;
-                try {
-                    uri = new URI(s);
-                } catch (URISyntaxException ex) {
-                    throw new HttpException("Invalid request URI: " + s);
-                }
-                int index = Integer.parseInt(uri.getQuery());
-                byte[] bytes = requestData.getBytes(index);
-                NByteArrayEntity entity = new NByteArrayEntity(bytes); 
-                response.setEntity(entity);
-            }
-            
-        };
-        
-        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
-
-            public void initalizeContext(final HttpContext context, final Object attachment)
{
-                context.setAttribute("LIST", attachment);
-                context.setAttribute("REQ-COUNT", Integer.valueOf(0));
-                context.setAttribute("RES-COUNT", Integer.valueOf(0));
-            }
-
-            public void finalizeContext(final HttpContext context) {
-            }
-
-            public HttpRequest submitRequest(final HttpContext context) {
-                int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
-                BasicHttpRequest get = null;
-                if (i < reqNo) {
-                    get = new BasicHttpRequest("GET", "/?" + i);
-                    context.setAttribute("REQ-COUNT", Integer.valueOf(i + 1));
-                }
-                return get;
-            }
-            
-            public void handleResponse(final HttpResponse response, final HttpContext context)
{
-                NHttpConnection conn = (NHttpConnection) context.getAttribute(
-                        ExecutionContext.HTTP_CONNECTION);
-                
-                ByteSequence list = (ByteSequence) context.getAttribute("LIST");
-                int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
-                i++;
-                context.setAttribute("RES-COUNT", Integer.valueOf(i));
-
-                try {
-                    HttpEntity entity = response.getEntity();
-                    byte[] data = EntityUtils.toByteArray(entity);
-                    list.addBytes(data);
-                    requestCount.decrement();
-                } catch (IOException ex) {
-                    requestCount.abort();
-                }
-
-                if (i < reqNo) {
-                    conn.requestInput();
-                }
-            }
-            
-        };
-        
-        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
-                requestHandler, 
-                null,
+    private void executeStandardTest(
+            final NHttpRequestHandler requestHandler,
+            final NHttpRequestExecutionHandler requestExecutionHandler) throws Exception
{
+        int connNo = 3;
+        int reqNo = 20;
+        TestJob[] jobs = new TestJob[connNo * reqNo];
+        for (int i = 0; i < jobs.length; i++) {
+            jobs[i] = new TestJob(); 
+        }
+        Queue<TestJob> queue = new ConcurrentLinkedQueue<TestJob>();
+        for (int i = 0; i < jobs.length; i++) {
+            queue.add(jobs[i]); 
+        }
+
+        BasicHttpProcessor serverHttpProc = new BasicHttpProcessor();
+        serverHttpProc.addInterceptor(new ResponseDate());
+        serverHttpProc.addInterceptor(new ResponseServer());
+        serverHttpProc.addInterceptor(new ResponseContent());
+        serverHttpProc.addInterceptor(new ResponseConnControl());
+
+        AsyncNHttpServiceHandler serviceHandler = new AsyncNHttpServiceHandler(
+                serverHttpProc,
+                new DefaultHttpResponseFactory(),
+                new DefaultConnectionReuseStrategy(),
+                this.server.getParams());
+
+        serviceHandler.setHandlerResolver(
+                new SimpleNHttpRequestHandlerResolver(requestHandler));
+        serviceHandler.setEventListener(
                 new SimpleEventListener());
 
-        NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                new SimpleEventListener());
+        BasicHttpProcessor clientHttpProc = new BasicHttpProcessor();
+        clientHttpProc.addInterceptor(new RequestContent());
+        clientHttpProc.addInterceptor(new RequestTargetHost());
+        clientHttpProc.addInterceptor(new RequestConnControl());
+        clientHttpProc.addInterceptor(new RequestUserAgent());
+        clientHttpProc.addInterceptor(new RequestExpectContinue());
+
+        AsyncNHttpClientHandler clientHandler = new AsyncNHttpClientHandler(
+                clientHttpProc,
+                requestExecutionHandler,
+                new DefaultConnectionReuseStrategy(),
+                this.client.getParams());
 
-        this.server.setRequestCount(requestCount);
-        this.client.setRequestCount(requestCount);
+        clientHandler.setEventListener(
+                new SimpleEventListener());
         
         this.server.start(serviceHandler);
         this.client.start(clientHandler);
@@ -196,601 +141,141 @@
         ListenerEndpoint endpoint = this.server.getListenerEndpoint();
         endpoint.waitFor();
         InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
-        
-        for (int i = 0; i < responseData.size(); i++) {
+
+        for (int i = 0; i < connNo; i++) {
             this.client.openConnection(
-                    new InetSocketAddress("localhost", serverAddress.getPort()), 
-                    responseData.get(i));
+                    new InetSocketAddress("localhost", serverAddress.getPort()),
+                    queue);
         }
-     
-        requestCount.await(10000);
-        assertEquals(0, requestCount.getValue());
-        
-        this.client.shutdown();
-        this.server.shutdown();
 
-        for (int c = 0; c < responseData.size(); c++) {
-            ByteSequence receivedPackets = responseData.get(c);
-            ByteSequence expectedPackets = requestData;
-            assertEquals(expectedPackets.size(), receivedPackets.size());
-            for (int p = 0; p < requestData.size(); p++) {
-                byte[] expected = requestData.getBytes(p);
-                byte[] received = receivedPackets.getBytes(p);
-                
-                assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    assertEquals(expected[i], received[i]);
-                }
+        for (int i = 0; i < jobs.length; i++) {
+            TestJob testjob = jobs[i];
+            testjob.waitFor();
+            if (testjob.isSuccessful()) {
+                assertEquals(HttpStatus.SC_OK, testjob.getStatusCode());
+                assertEquals(testjob.getExpected(), testjob.getResult());
+            } else {
+                fail(testjob.getFailureMessage());
             }
         }
-        
     }
-
+    
     /**
-     * This test case executes a series of simple (non-pipelined) POST requests 
-     * with content length delimited content over multiple connections. 
+     * This test case executes a series of simple (non-pipelined) GET requests 
+     * over multiple connections. 
      */
-    public void testSimpleBasicHttpEntityEnclosingRequestsWithContentLength() throws Exception
{
-        
-        final int connNo = 3;
-        final int reqNo = 20;
-        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
-        
-        final ByteSequence requestData = new ByteSequence();
-        requestData.rnd(reqNo);
-        
-        List<ByteSequence> responseData = new ArrayList<ByteSequence>(connNo);
-        for (int i = 0; i < connNo; i++) {
-            responseData.add(new ByteSequence());
-        }
-        
-        HttpRequestHandler requestHandler = new HttpRequestHandler() {
+    public void testHttpGets() throws Exception {
+        NHttpRequestExecutionHandler requestExecutionHandler = new TestRequestExecutionHandler()
{
 
-            public void handle(
-                    final HttpRequest request, 
-                    final HttpResponse response, 
-                    final HttpContext context) throws HttpException, IOException {
-                
-                if (request instanceof HttpEntityEnclosingRequest) {
-                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
-                    byte[] data = EntityUtils.toByteArray(incoming);
-                    
-                    NByteArrayEntity outgoing = new NByteArrayEntity(data);
-                    outgoing.setChunked(false);
-                    response.setEntity(outgoing);
-                } else {
-                    NStringEntity outgoing = new NStringEntity("No content"); 
-                    response.setEntity(outgoing);
-                }
+            @Override
+            protected HttpRequest generateRequest(TestJob testjob) {
+                String s = testjob.getPattern() + "x" + testjob.getCount(); 
+                return new BasicHttpRequest("GET", s);
             }
             
         };
-        
-        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
-
-            public void initalizeContext(final HttpContext context, final Object attachment)
{
-                context.setAttribute("LIST", attachment);
-                context.setAttribute("REQ-COUNT", Integer.valueOf(0));
-                context.setAttribute("RES-COUNT", Integer.valueOf(0));
-            }
-
-            public void finalizeContext(final HttpContext context) {
-            }
+        executeStandardTest(new TestRequestHandler(), requestExecutionHandler);
+    }
 
-            public HttpRequest submitRequest(final HttpContext context) {
-                int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
-                BasicHttpEntityEnclosingRequest post = null;
-                if (i < reqNo) {
-                    post = new BasicHttpEntityEnclosingRequest("POST", "/?" + i);
-                    byte[] bytes = requestData.getBytes(i);
-                    NByteArrayEntity outgoing = new NByteArrayEntity(bytes);
-                    post.setEntity(outgoing);
-                    
-                    context.setAttribute("REQ-COUNT", Integer.valueOf(i + 1));
-                }
-                return post;
-            }
-            
-            public void handleResponse(final HttpResponse response, final HttpContext context)
{
-                NHttpConnection conn = (NHttpConnection) context.getAttribute(
-                        ExecutionContext.HTTP_CONNECTION);
-                
-                ByteSequence list = (ByteSequence) context.getAttribute("LIST");
-                int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
-                i++;
-                context.setAttribute("RES-COUNT", Integer.valueOf(i));
+    /**
+     * This test case executes a series of simple (non-pipelined) POST requests 
+     * with content length delimited content over multiple connections. 
+     */
+    public void testHttpPostsWithContentLength() throws Exception {
+        NHttpRequestExecutionHandler requestExecutionHandler = new TestRequestExecutionHandler()
{
 
+            @Override
+            protected HttpRequest generateRequest(TestJob testjob) {
+                String s = testjob.getPattern() + "x" + testjob.getCount(); 
+                HttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST",
s);
+                NStringEntity entity = null;
                 try {
-                    HttpEntity entity = response.getEntity();
-                    byte[] data = EntityUtils.toByteArray(entity);
-                    list.addBytes(data);
-                    requestCount.decrement();
-                } catch (IOException ex) {
-                    requestCount.abort();
-                }
-
-                if (i < reqNo) {
-                    conn.requestInput();
+                    entity = new NStringEntity(testjob.getExpected(), "US-ASCII");
+                    entity.setChunked(false);
+                } catch (UnsupportedEncodingException ignore) {
                 }
+                r.setEntity(entity);
+                return r;
             }
             
         };
-        
-        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
-                requestHandler, 
-                null,
-                new SimpleEventListener());
-
-        NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                new SimpleEventListener());
-
-        this.server.setRequestCount(requestCount);
-        this.client.setRequestCount(requestCount);
-        
-        this.server.start(serviceHandler);
-        this.client.start(clientHandler);
-
-        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
-        endpoint.waitFor();
-        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
-        
-        for (int i = 0; i < responseData.size(); i++) {
-            this.client.openConnection(
-                    new InetSocketAddress("localhost", serverAddress.getPort()), 
-                    responseData.get(i));
-        }
-     
-        requestCount.await(10000);
-        assertEquals(0, requestCount.getValue());
-        
-        this.client.shutdown();
-        this.server.shutdown();
-
-        for (int c = 0; c < responseData.size(); c++) {
-            ByteSequence receivedPackets = responseData.get(c);
-            ByteSequence expectedPackets = requestData;
-            assertEquals(expectedPackets.size(), receivedPackets.size());
-            for (int p = 0; p < requestData.size(); p++) {
-                byte[] expected = requestData.getBytes(p);
-                byte[] received = receivedPackets.getBytes(p);
-                
-                assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    assertEquals(expected[i], received[i]);
-                }
-            }
-        }
-        
+        executeStandardTest(new TestRequestHandler(), requestExecutionHandler);
     }
 
     /**
      * This test case executes a series of simple (non-pipelined) POST requests 
      * with chunk coded content content over multiple connections. 
      */
-    public void testSimpleBasicHttpEntityEnclosingRequestsChunked() throws Exception {
-        
-        final int connNo = 3;
-        final int reqNo = 20;
-        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
-        
-        final ByteSequence requestData = new ByteSequence();
-        requestData.rnd(reqNo);
-        
-        List<ByteSequence> responseData = new ArrayList<ByteSequence>(connNo);
-        for (int i = 0; i < connNo; i++) {
-            responseData.add(new ByteSequence());
-        }
-        
-        HttpRequestHandler requestHandler = new HttpRequestHandler() {
+    public void testHttpPostsChunked() throws Exception {
+        NHttpRequestExecutionHandler requestExecutionHandler = new TestRequestExecutionHandler()
{
 
-            public void handle(
-                    final HttpRequest request, 
-                    final HttpResponse response, 
-                    final HttpContext context) throws HttpException, IOException {
-                
-                if (request instanceof HttpEntityEnclosingRequest) {
-                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
-                    byte[] data = EntityUtils.toByteArray(incoming);
-                    NByteArrayEntity outgoing = new NByteArrayEntity(data);
-                    outgoing.setChunked(true);
-                    response.setEntity(outgoing);
-                } else {
-                    NStringEntity outgoing = new NStringEntity("No content"); 
-                    response.setEntity(outgoing);
-                }
-            }
-            
-        };
-        
-        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
-
-            public void initalizeContext(final HttpContext context, final Object attachment)
{
-                context.setAttribute("LIST", attachment);
-                context.setAttribute("REQ-COUNT", Integer.valueOf(0));
-                context.setAttribute("RES-COUNT", Integer.valueOf(0));
-            }
-
-            public void finalizeContext(final HttpContext context) {
-            }
-
-            public HttpRequest submitRequest(final HttpContext context) {
-                int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
-                BasicHttpEntityEnclosingRequest post = null;
-                if (i < reqNo) {
-                    post = new BasicHttpEntityEnclosingRequest("POST", "/?" + i);
-                    byte[] bytes = requestData.getBytes(i);
-                    NByteArrayEntity outgoing = new NByteArrayEntity(bytes);
-                    outgoing.setChunked(true);
-                    post.setEntity(outgoing);
-                    
-                    context.setAttribute("REQ-COUNT", Integer.valueOf(i + 1));
-                }
-                return post;
-            }
-            
-            public void handleResponse(final HttpResponse response, final HttpContext context)
{
-                NHttpConnection conn = (NHttpConnection) context.getAttribute(
-                        ExecutionContext.HTTP_CONNECTION);
-                
-                ByteSequence list = (ByteSequence) context.getAttribute("LIST");
-                int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
-                i++;
-                context.setAttribute("RES-COUNT", Integer.valueOf(i));
-                
+            @Override
+            protected HttpRequest generateRequest(TestJob testjob) {
+                String s = testjob.getPattern() + "x" + testjob.getCount(); 
+                HttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST",
s);
+                NStringEntity entity = null;
                 try {
-                    HttpEntity entity = response.getEntity();
-                    byte[] data = EntityUtils.toByteArray(entity);
-                    list.addBytes(data);
-                    requestCount.decrement();
-                } catch (IOException ex) {
-                    requestCount.abort();
-                }
-
-                if (i < reqNo) {
-                    conn.requestInput();
+                    entity = new NStringEntity(testjob.getExpected(), "US-ASCII");
+                    entity.setChunked(true);
+                } catch (UnsupportedEncodingException ignore) {
                 }
+                r.setEntity(entity);
+                return r;
             }
             
         };
-        
-        SimpleEventListener serverEventListener = new SimpleEventListener();
-        SimpleEventListener clientEventListener = new SimpleEventListener();
-        
-        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
-                requestHandler, 
-                null,
-                serverEventListener);
-
-        NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
-
-        this.server.setRequestCount(requestCount);
-        this.client.setRequestCount(requestCount);
-        
-        this.server.start(serviceHandler);
-        this.client.start(clientHandler);
-
-        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
-        endpoint.waitFor();
-        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
-        
-        for (int i = 0; i < responseData.size(); i++) {
-            this.client.openConnection(
-                    new InetSocketAddress("localhost", serverAddress.getPort()), 
-                    responseData.get(i));
-        }
-     
-        requestCount.await(10000);
-        assertEquals(0, requestCount.getValue());
-        
-        this.client.shutdown();
-        this.server.shutdown();
-
-        for (int c = 0; c < responseData.size(); c++) {
-            ByteSequence receivedPackets = responseData.get(c);
-            ByteSequence expectedPackets = requestData;
-            assertEquals(expectedPackets.size(), receivedPackets.size());
-            for (int p = 0; p < requestData.size(); p++) {
-                byte[] expected = requestData.getBytes(p);
-                byte[] received = receivedPackets.getBytes(p);
-                
-                assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    assertEquals(expected[i], received[i]);
-                }
-            }
-        }
-        
+        executeStandardTest(new TestRequestHandler(), requestExecutionHandler);
     }
 
     /**
      * This test case executes a series of simple (non-pipelined) HTTP/1.0 
      * POST requests over multiple persistent connections. 
      */
-    public void testSimpleBasicHttpEntityEnclosingRequestsHTTP10() throws Exception {
-        
-        final int connNo = 3;
-        final int reqNo = 20;
-        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
-        
-        final ByteSequence requestData = new ByteSequence();
-        requestData.rnd(reqNo);
-        
-        List<ByteSequence> responseData = new ArrayList<ByteSequence>(connNo);
-        for (int i = 0; i < connNo; i++) {
-            responseData.add(new ByteSequence());
-        }
-        
-        HttpRequestHandler requestHandler = new HttpRequestHandler() {
-            
-
-            public void handle(
-                    final HttpRequest request, 
-                    final HttpResponse response, 
-                    final HttpContext context) throws HttpException, IOException {
-                
-                if (request instanceof HttpEntityEnclosingRequest) {
-                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
-                    byte[] data = EntityUtils.toByteArray(incoming);
-                    
-                    NByteArrayEntity outgoing = new NByteArrayEntity(data);
-                    outgoing.setChunked(false);
-                    response.setEntity(outgoing);
-                } else {
-                    NStringEntity outgoing = new NStringEntity("No content"); 
-                    response.setEntity(outgoing);
-                }
-            }
-            
-        };
-        
-        // Set protocol level to HTTP/1.0
-        this.client.getParams().setParameter(
-                CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
-        
-        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
-
-            public void initalizeContext(final HttpContext context, final Object attachment)
{
-                context.setAttribute("LIST", attachment);
-                context.setAttribute("REQ-COUNT", Integer.valueOf(0));
-                context.setAttribute("RES-COUNT", Integer.valueOf(0));
-            }
-
-            public void finalizeContext(final HttpContext context) {
-            }
-
-            public HttpRequest submitRequest(final HttpContext context) {
-                int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
-                BasicHttpEntityEnclosingRequest post = null;
-                if (i < reqNo) {
-                    post = new BasicHttpEntityEnclosingRequest("POST", "/?" + i);
-                    byte[] bytes = requestData.getBytes(i);
-                    NByteArrayEntity outgoing = new NByteArrayEntity(bytes);
-                    post.setEntity(outgoing);
-                    
-                    context.setAttribute("REQ-COUNT", Integer.valueOf(i + 1));
-                }
-                return post;
-            }
-            
-            public void handleResponse(final HttpResponse response, final HttpContext context)
{
-                NHttpConnection conn = (NHttpConnection) context.getAttribute(
-                        ExecutionContext.HTTP_CONNECTION);
-
-                ByteSequence list = (ByteSequence) context.getAttribute("LIST");
-                int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
-                i++;
-                context.setAttribute("RES-COUNT", Integer.valueOf(i));
+    public void testHttpPostsHTTP10() throws Exception {
+        NHttpRequestExecutionHandler requestExecutionHandler = new TestRequestExecutionHandler()
{
 
+            @Override
+            protected HttpRequest generateRequest(TestJob testjob) {
+                String s = testjob.getPattern() + "x" + testjob.getCount(); 
+                HttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST",
s, 
+                        HttpVersion.HTTP_1_0);
+                NStringEntity entity = null;
                 try {
-                    HttpEntity entity = response.getEntity();
-                    byte[] data = EntityUtils.toByteArray(entity);
-                    list.addBytes(data);
-                    requestCount.decrement();
-                } catch (IOException ex) {
-                    requestCount.abort();
-                }
-
-                if (i < reqNo) {
-                    conn.requestInput();
+                    entity = new NStringEntity(testjob.getExpected(), "US-ASCII");
+                } catch (UnsupportedEncodingException ignore) {
                 }
+                r.setEntity(entity);
+                return r;
             }
             
         };
-        
-        SimpleEventListener serverEventListener = new SimpleEventListener();
-        SimpleEventListener clientEventListener = new SimpleEventListener();
-        
-        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
-                requestHandler, 
-                null,
-                serverEventListener);
-
-        NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
-
-        this.server.setRequestCount(requestCount);
-        this.client.setRequestCount(requestCount);
-        
-        this.server.start(serviceHandler);
-        this.client.start(clientHandler);
-
-        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
-        endpoint.waitFor();
-        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
-        
-        for (int i = 0; i < responseData.size(); i++) {
-            this.client.openConnection(
-                    new InetSocketAddress("localhost", serverAddress.getPort()), 
-                    responseData.get(i));
-        }
-     
-        requestCount.await(10000);
-        assertEquals(0, requestCount.getValue());
-        
-        this.client.shutdown();
-        this.server.shutdown();
-
-        for (int c = 0; c < responseData.size(); c++) {
-            ByteSequence receivedPackets = responseData.get(c);
-            ByteSequence expectedPackets = requestData;
-            assertEquals(expectedPackets.size(), receivedPackets.size());
-            for (int p = 0; p < requestData.size(); p++) {
-                byte[] expected = requestData.getBytes(p);
-                byte[] received = receivedPackets.getBytes(p);
-                
-                assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    assertEquals(expected[i], received[i]);
-                }
-            }
-        }
-        
+        executeStandardTest(new TestRequestHandler(), requestExecutionHandler);
     }
     
     /**
-     * This test case executes a series of simple (non-pipelined) POST requests 
-     * with chunk coded content over multiple connections using 
-     * {@link NHttpEntityWrapper}. 
+     * This test case executes a series of simple (non-pipelined) POST requests
+     * over multiple connections using the 'expect: continue' handshake.  This test
+     * uses nonblocking handlers & entities.
      */
-    public void testSimpleBasicHttpEntityEnclosingRequestsChunkedWithWrapper() throws Exception
{
-        
-        final int connNo = 1;
-        final int reqNo = 2;
-        final RequestCount requestCount = new RequestCount(connNo * reqNo); 
-        
-        final ByteSequence requestData = new ByteSequence();
-        requestData.rnd(reqNo);
-        
-        List<ByteSequence> responseData = new ArrayList<ByteSequence>(connNo);
-        for (int i = 0; i < connNo; i++) {
-            responseData.add(new ByteSequence());
-        }
-        
-        HttpRequestHandler requestHandler = new HttpRequestHandler() {
-
-            public void handle(
-                    final HttpRequest request, 
-                    final HttpResponse response, 
-                    final HttpContext context) throws HttpException, IOException {
-                
-                if (request instanceof HttpEntityEnclosingRequest) {
-                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
-                    byte[] data = EntityUtils.toByteArray(incoming);
-                    ByteArrayEntity outgoing = new ByteArrayEntity(data);
-                    outgoing.setChunked(true);
-                    response.setEntity(new NHttpEntityWrapper(outgoing));
-                } else {
-                    StringEntity outgoing = new StringEntity("No content"); 
-                    response.setEntity(new NHttpEntityWrapper(outgoing));
-                }
-            }
-            
-        };
-        
-        HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler()
{
-
-            public void initalizeContext(final HttpContext context, final Object attachment)
{
-                context.setAttribute("LIST", attachment);
-                context.setAttribute("REQ-COUNT", Integer.valueOf(0));
-                context.setAttribute("RES-COUNT", Integer.valueOf(0));
-            }
+    public void testHttpPostsWithExpectContinue() throws Exception {
+        NHttpRequestExecutionHandler requestExecutionHandler = new TestRequestExecutionHandler()
{
 
-            public void finalizeContext(final HttpContext context) {
-            }
-
-            public HttpRequest submitRequest(final HttpContext context) {
-                int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
-                BasicHttpEntityEnclosingRequest post = null;
-                if (i < reqNo) {
-                    post = new BasicHttpEntityEnclosingRequest("POST", "/?" + i);
-                    byte[] bytes = requestData.getBytes(i);
-                    ByteArrayEntity outgoing = new ByteArrayEntity(bytes);
-                    outgoing.setChunked(true);
-                    post.setEntity(outgoing);
-                    
-                    context.setAttribute("REQ-COUNT", Integer.valueOf(i + 1));
-                }
-                return post;
-            }
-            
-            public void handleResponse(final HttpResponse response, final HttpContext context)
{
-                NHttpConnection conn = (NHttpConnection) context.getAttribute(
-                        ExecutionContext.HTTP_CONNECTION);
-                
-                ByteSequence list = (ByteSequence) context.getAttribute("LIST");
-                int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
-                i++;
-                context.setAttribute("RES-COUNT", Integer.valueOf(i));
-                
+            @Override
+            protected HttpRequest generateRequest(TestJob testjob) {
+                String s = testjob.getPattern() + "x" + testjob.getCount(); 
+                HttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST",
s);
+                NStringEntity entity = null;
                 try {
-                    HttpEntity entity = response.getEntity();
-                    byte[] data = EntityUtils.toByteArray(entity);
-                    list.addBytes(data);
-                    requestCount.decrement();
-                } catch (IOException ex) {
-                    requestCount.abort();
-                }
-
-                if (i < reqNo) {
-                    conn.requestInput();
+                    entity = new NStringEntity(testjob.getExpected(), "US-ASCII");
+                } catch (UnsupportedEncodingException ignore) {
                 }
+                r.setEntity(entity);
+                r.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
true);
+                return r;
             }
             
         };
-        
-        SimpleEventListener serverEventListener = new SimpleEventListener();
-        SimpleEventListener clientEventListener = new SimpleEventListener();
-        
-        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
-                requestHandler, 
-                null,
-                serverEventListener);
-
-        NHttpClientHandler clientHandler = createHttpClientHandler(
-                requestExecutionHandler, 
-                clientEventListener);
-
-        this.server.setRequestCount(requestCount);
-        this.client.setRequestCount(requestCount);
-        
-        this.server.start(serviceHandler);
-        this.client.start(clientHandler);
-
-        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
-        endpoint.waitFor();
-        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
-        
-        for (int i = 0; i < responseData.size(); i++) {
-            this.client.openConnection(
-                    new InetSocketAddress("localhost", serverAddress.getPort()), 
-                    responseData.get(i));
-        }
-     
-        requestCount.await(10000);
-        assertEquals(0, requestCount.getValue());
-        
-        this.client.shutdown();
-        this.server.shutdown();
-
-        for (int c = 0; c < responseData.size(); c++) {
-            ByteSequence receivedPackets = responseData.get(c);
-            ByteSequence expectedPackets = requestData;
-            assertEquals(expectedPackets.size(), receivedPackets.size());
-            for (int p = 0; p < requestData.size(); p++) {
-                byte[] expected = requestData.getBytes(p);
-                byte[] received = receivedPackets.getBytes(p);
-                
-                assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    assertEquals(expected[i], received[i]);
-                }
-            }
-        }
-        
+        executeStandardTest(new TestRequestHandler(), requestExecutionHandler);
     }
 
 }

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestExecutionHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestExecutionHandler.java?rev=758645&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestExecutionHandler.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestExecutionHandler.java
Thu Mar 26 13:47:19 2009
@@ -0,0 +1,107 @@
+/*
+ * $HeadURL:$
+ * $Revision:$
+ * $Date:$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.protocol;
+
+import java.io.IOException;
+import java.util.Queue;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.nio.entity.BufferingNHttpEntity;
+import org.apache.http.nio.entity.ConsumingNHttpEntity;
+import org.apache.http.nio.util.HeapByteBufferAllocator;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
+
+abstract class TestRequestExecutionHandler 
+    implements NHttpRequestExecutionHandler, HttpRequestExecutionHandler {
+    
+    public void initalizeContext(final HttpContext context, final Object attachment) {
+        context.setAttribute("queue", attachment);
+    }
+
+    protected abstract HttpRequest generateRequest(TestJob testjob);
+    
+    public HttpRequest submitRequest(final HttpContext context) {
+
+        @SuppressWarnings("unchecked")
+        Queue<TestJob> queue = (Queue<TestJob>) context.getAttribute("queue");
+        if (queue == null) {
+            throw new IllegalStateException("Queue is null");
+        }
+        
+        TestJob testjob = queue.poll();
+        context.setAttribute("job", testjob);
+        
+        if (testjob != null) {
+            return generateRequest(testjob);
+        } else {
+            return null;
+        }
+    }
+
+    public ConsumingNHttpEntity responseEntity(
+            final HttpResponse response,
+            final HttpContext context) throws IOException {
+        return new BufferingNHttpEntity(response.getEntity(),
+                new HeapByteBufferAllocator());
+    }
+
+    public void handleResponse(final HttpResponse response, final HttpContext context) {
+        TestJob testjob = (TestJob) context.removeAttribute("job");
+        if (testjob == null) {
+            throw new IllegalStateException("TestJob is null");
+        }
+
+        int statusCode = response.getStatusLine().getStatusCode();
+        String content = null;
+        
+        HttpEntity entity = response.getEntity();
+        if (entity != null) {
+            try {
+                content = EntityUtils.toString(entity);
+            } catch (IOException ex) {
+                content = "I/O exception: " + ex.getMessage();
+            }
+        }
+        testjob.setResult(statusCode, content);
+    }
+
+    public void finalizeContext(final HttpContext context) {
+        TestJob testjob = (TestJob) context.removeAttribute("job");
+        if (testjob != null) {
+            testjob.fail("Request failed");
+        }
+    }
+    
+}
\ No newline at end of file

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestExecutionHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestHandler.java?rev=758645&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestHandler.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestHandler.java
Thu Mar 26 13:47:19 2009
@@ -0,0 +1,94 @@
+/*
+ * $HeadURL:$
+ * $Revision:$
+ * $Date:$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.protocol;
+
+import java.io.IOException;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.nio.entity.BufferingNHttpEntity;
+import org.apache.http.nio.entity.ConsumingNHttpEntity;
+import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.nio.util.HeapByteBufferAllocator;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.util.EntityUtils;
+
+final class TestRequestHandler extends SimpleNHttpRequestHandler implements HttpRequestHandler
{
+    
+    public ConsumingNHttpEntity entityRequest(
+            final HttpEntityEnclosingRequest request,
+            final HttpContext context) {
+        return new BufferingNHttpEntity(
+                request.getEntity(),
+                new HeapByteBufferAllocator());
+    }
+
+    @Override
+    public void handle(
+            final HttpRequest request,
+            final HttpResponse response,
+            final HttpContext context) throws HttpException, IOException {
+
+        String content = null;
+        if (request instanceof HttpEntityEnclosingRequest) {
+            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+            if (entity != null) {
+                content = EntityUtils.toString(entity); 
+            } else {
+                response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
+                content = "Request entity not avaialble";
+            }
+        } else {
+            String s = request.getRequestLine().getUri();
+            int idx = s.indexOf('x');
+            if (idx == -1) {
+                throw new HttpException("Unexpected request-URI format");
+            }
+            String pattern = s.substring(0, idx);
+            int count = Integer.parseInt(s.substring(idx + 1, s.length()));
+            
+            StringBuilder buffer = new StringBuilder();
+            for (int i = 0; i < count; i++) {
+                buffer.append(pattern);
+            }
+            content = buffer.toString();
+        }
+        NStringEntity entity = new NStringEntity(content, "US-ASCII");
+        response.setEntity(entity);
+    }
+    
+}
\ No newline at end of file

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestRequestHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message