You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/09/17 19:37:28 UTC

svn commit: r1172023 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/protocol/NullNHttpEntity.java test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java

Author: olegk
Date: Sat Sep 17 17:37:27 2011
New Revision: 1172023

URL: http://svn.apache.org/viewvc?rev=1172023&view=rev
Log:
Added unit tests for BufferingAsyncRequestHandler

Added:
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullNHttpEntity.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullNHttpEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullNHttpEntity.java?rev=1172023&r1=1172022&r2=1172023&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullNHttpEntity.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NullNHttpEntity.java Sat Sep 17 17:37:27 2011
@@ -38,6 +38,7 @@ import org.apache.http.nio.ContentDecode
 import org.apache.http.nio.IOControl;
 import org.apache.http.nio.entity.ConsumingNHttpEntity;
 
+@Deprecated
 class NullNHttpEntity extends HttpEntityWrapper implements ConsumingNHttpEntity {
 
     private final ByteBuffer buffer;

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java?rev=1172023&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java Sat Sep 17 17:37:27 2011
@@ -0,0 +1,127 @@
+/*
+ * ====================================================================
+ * 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 junit.framework.Assert;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.concurrent.Cancellable;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.message.BasicRequestLine;
+import org.apache.http.nio.util.HeapByteBufferAllocator;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+public class TestBufferingAsyncRequestHandler {
+
+    private HttpRequestHandler requestHandler;
+    private BufferingAsyncRequestHandler asyncRequestHandler;
+    private HttpContext context;
+    private HttpRequest request;
+    private HttpAsyncResponseTrigger trigger;
+
+    @Before
+    public void setUp() throws Exception {
+        this.requestHandler = Mockito.mock(HttpRequestHandler.class);
+        this.asyncRequestHandler = new BufferingAsyncRequestHandler(this.requestHandler,
+                new DefaultHttpResponseFactory(), new HeapByteBufferAllocator());
+        this.context = new BasicHttpContext();
+        this.request = Mockito.mock(HttpRequest.class);
+        this.trigger = Mockito.mock(HttpAsyncResponseTrigger.class);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+
+    @Test
+    public void testInvalidConstruction() throws Exception {
+        try {
+            new BufferingAsyncRequestHandler(null, new DefaultHttpResponseFactory(), new HeapByteBufferAllocator());
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException ex) {
+        }
+        try {
+            new BufferingAsyncRequestHandler(this.requestHandler, null, new HeapByteBufferAllocator());
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException ex) {
+        }
+        try {
+            new BufferingAsyncRequestHandler(this.requestHandler, new DefaultHttpResponseFactory(), null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException ex) {
+        }
+    }
+
+    @Test
+    public void testProcessRequest() throws Exception {
+        HttpAsyncRequestConsumer<HttpRequest> requestConsumer = this.asyncRequestHandler.processRequest(
+                this.request, this.context);
+        Assert.assertTrue(requestConsumer instanceof BasicAsyncRequestConsumer);
+    }
+
+    @Test
+    public void testHandleRequest() throws Exception {
+        Mockito.when(this.request.getRequestLine()).thenReturn(new BasicRequestLine("GET", "/", HttpVersion.HTTP_1_0));
+
+        Cancellable cancellable = this.asyncRequestHandler.handle(this.request, this.trigger, this.context);
+
+        Assert.assertNull(cancellable);
+        ArgumentCaptor<HttpResponse> argCaptor = ArgumentCaptor.forClass(HttpResponse.class);
+        Mockito.verify(this.requestHandler).handle(
+                Mockito.eq(this.request), argCaptor.capture(), Mockito.eq(this.context));
+        HttpResponse response = argCaptor.getValue();
+        Assert.assertEquals(HttpVersion.HTTP_1_0, response.getProtocolVersion());
+        Mockito.verify(this.trigger).submitResponse(Mockito.any(BasicAsyncResponseProducer.class));
+    }
+
+    @Test
+    public void testHandleRequestUnsupportedVersion() throws Exception {
+        Mockito.when(this.request.getRequestLine()).thenReturn(new BasicRequestLine("GET", "/", new HttpVersion(234, 456)));
+
+        Cancellable cancellable = this.asyncRequestHandler.handle(this.request, this.trigger, this.context);
+
+        Assert.assertNull(cancellable);
+        ArgumentCaptor<HttpResponse> argCaptor = ArgumentCaptor.forClass(HttpResponse.class);
+        Mockito.verify(this.requestHandler).handle(
+                Mockito.eq(this.request), argCaptor.capture(), Mockito.eq(this.context));
+        HttpResponse response = argCaptor.getValue();
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
+        Mockito.verify(this.trigger).submitResponse(Mockito.any(BasicAsyncResponseProducer.class));
+    }
+
+}

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

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBufferingAsyncRequestHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain