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/07 20:59:26 UTC

svn commit: r1166317 - in /httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol: TestErrorResponseProducer.java TestNHttpEntityWrapper.java

Author: olegk
Date: Wed Sep  7 18:59:25 2011
New Revision: 1166317

URL: http://svn.apache.org/viewvc?rev=1166317&view=rev
Log:
HTTPCORE-276: Test cases for async protocol handlers
Contributed by William R. Speirs <bill.speirs at gmail.com>

Added:
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java   (with props)
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNHttpEntityWrapper.java   (with props)

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java?rev=1166317&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestErrorResponseProducer.java Wed Sep  7 18:59:25 2011
@@ -0,0 +1,89 @@
+/*
+ * ====================================================================
+ * 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 static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import junit.framework.Assert;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.nio.entity.ProducingNHttpEntity;
+import org.apache.http.protocol.HTTP;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class TestErrorResponseProducer {
+
+    private ErrorResponseProducer erp;
+    @Mock private ProducingNHttpEntity entity;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test
+    public void testGenerateResponseKeepAlive() {
+        erp = new ErrorResponseProducer(HttpVersion.HTTP_1_1, 200, entity, true);
+        HttpResponse res = erp.generateResponse();
+
+        Assert.assertEquals(HTTP.CONN_KEEP_ALIVE, res.getFirstHeader(HTTP.CONN_DIRECTIVE).getValue());
+        Assert.assertEquals(entity, res.getEntity());
+        Assert.assertEquals(200, res.getStatusLine().getStatusCode());
+    }
+
+    @Test
+    public void testGenerateResponseClose() {
+        erp = new ErrorResponseProducer(HttpVersion.HTTP_1_1, 200, entity, false);
+        HttpResponse res = erp.generateResponse();
+
+        Assert.assertEquals(HTTP.CONN_CLOSE, res.getFirstHeader(HTTP.CONN_DIRECTIVE).getValue());
+        Assert.assertEquals(entity, res.getEntity());
+        Assert.assertEquals(200, res.getStatusLine().getStatusCode());
+    }
+
+    @Test
+    public void testProduceContent() throws Exception {
+        erp = new ErrorResponseProducer(HttpVersion.HTTP_1_1, 200, entity, true);
+
+        erp.produceContent(null, null);
+
+        verify(entity, times(1)).produceContent(null, null);
+    }
+
+}

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

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

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

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNHttpEntityWrapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNHttpEntityWrapper.java?rev=1166317&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNHttpEntityWrapper.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestNHttpEntityWrapper.java Wed Sep  7 18:59:25 2011
@@ -0,0 +1,104 @@
+/*
+ * ====================================================================
+ * 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 static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.ByteArrayInputStream;
+
+import junit.framework.Assert;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.nio.ContentEncoder;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class TestNHttpEntityWrapper {
+
+    private NHttpEntityWrapper wrapper;
+    @Mock private HttpEntity entity;
+    private ByteArrayInputStream is;
+    private byte[] buff = { 0x01, 0x02, 0x03 };
+    @Mock private ContentEncoder encoder;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        is = new ByteArrayInputStream(buff);
+        when(entity.getContent()).thenReturn(is);
+
+        wrapper = new NHttpEntityWrapper(entity);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test(expected=UnsupportedOperationException.class)
+    public void testGetContent() throws Exception {
+        wrapper.getContent();
+    }
+
+    @Test
+    public void testIsStreaming() {
+        boolean res = wrapper.isStreaming();
+
+        Assert.assertEquals(true, res);
+    }
+
+    @Test(expected=UnsupportedOperationException.class)
+    public void testWriteTo() throws Exception {
+        wrapper.writeTo(null);
+    }
+
+    @Test
+    public void testProduceContentWithContent() throws Exception {
+        wrapper.produceContent(encoder, null);
+        wrapper.finish();
+
+        verify(encoder, times(0)).complete();
+    }
+
+    @Test
+    public void testProduceContentNoContent() throws Exception {
+        is = new ByteArrayInputStream(new byte[] { });
+        when(entity.getContent()).thenReturn(is);
+
+        wrapper.produceContent(encoder, null);
+        wrapper.finish();
+
+        verify(encoder, times(1)).complete();
+    }
+
+}

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

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

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