hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1448420 [2/4] - in /httpcomponents/httpcore/trunk: ./ httpcore-ab/src/main/java/org/apache/http/benchmark/ httpcore-nio/src/main/java/org/apache/http/impl/nio/ httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ httpcore-nio/src/ma...
Date Wed, 20 Feb 2013 21:16:21 GMT
Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpClientConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpClientConnection.java?rev=1448420&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpClientConnection.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpClientConnection.java Wed Feb 20 21:16:19 2013
@@ -0,0 +1,670 @@
+/*
+ * ====================================================================
+ * 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.impl.nio;
+
+import java.nio.ByteBuffer;
+import java.nio.channels.ByteChannel;
+import java.nio.channels.SelectionKey;
+import java.util.LinkedList;
+
+import org.apache.http.ByteChannelMock;
+import org.apache.http.Consts;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.ReadableByteChannelMock;
+import org.apache.http.WritableByteChannelMock;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.nio.codecs.LengthDelimitedDecoder;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.nio.ContentDecoder;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.NHttpClientConnection;
+import org.apache.http.nio.NHttpClientEventHandler;
+import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.entity.HttpAsyncContentProducer;
+import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.nio.reactor.IOSession;
+import org.apache.http.nio.util.SimpleInputBuffer;
+import org.apache.http.protocol.HTTP;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class TestDefaultNHttpClientConnection {
+
+    @Mock
+    private IOSession session;
+    @Mock
+    private ByteChannel channel;
+    @Mock
+    private NHttpClientEventHandler handler;
+
+    private DefaultNHttpClientConnection conn;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        conn = new DefaultNHttpClientConnection(session, 32);
+    }
+
+    @Test
+    public void testSubmitRequest() throws Exception {
+        final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        conn.submitRequest(request);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertTrue(conn.hasBufferedOutput());
+
+        Mockito.verify(session).setEvent(SelectionKey.OP_WRITE);
+    }
+
+    @Test
+    public void testSubmitEntityEnclosingRequest() throws Exception {
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.setEntity(new StringEntity("stuff"));
+
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Assert.assertEquals(0, conn.getMetrics().getRequestCount());
+        conn.submitRequest(request);
+
+        Assert.assertSame(request, conn.getHttpRequest());
+        Assert.assertTrue(conn.hasBufferedOutput());
+        Assert.assertTrue(conn.isRequestSubmitted());
+        Assert.assertNotNull(conn.contentEncoder);
+        Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+
+        Mockito.verify(session).setEvent(SelectionKey.OP_WRITE);
+    }
+
+    @Test
+    public void testOutputReset() throws Exception {
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.setEntity(new StringEntity("stuff"));
+
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitRequest(request);
+
+        Assert.assertNotNull(conn.getHttpRequest());
+        Assert.assertNotNull(conn.contentEncoder);
+
+        conn.resetOutput();
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+    }
+
+    static class RequestReadyAnswer implements Answer<Void> {
+
+        private final HttpRequest request;
+
+        RequestReadyAnswer(final HttpRequest request) {
+            super();
+            this.request = request;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final NHttpClientConnection conn = (NHttpClientConnection) args[0];
+            conn.submitRequest(request);
+            return null;
+        }
+    };
+
+    static class ProduceContentAnswer implements Answer<Void> {
+
+        private final HttpAsyncContentProducer contentProducer;
+
+        ProduceContentAnswer(final HttpAsyncContentProducer contentProducer) {
+            super();
+            this.contentProducer = contentProducer;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final IOControl ioctrl = (IOControl) args[0];
+            final ContentEncoder encoder = (ContentEncoder) args[1];
+            contentProducer.produceContent(encoder, ioctrl);
+            return null;
+        }
+    };
+
+    @Test
+    public void testProduceOutputShortMessageAfterSubmit() throws Exception {
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        final NStringEntity entity = new NStringEntity("stuff");
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitRequest(request);
+        Assert.assertEquals(19, conn.outbuf.length());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\n\r\nstuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(1)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessageAfterSubmit() throws Exception {
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitRequest(request);
+        Assert.assertEquals(19, conn.outbuf.length());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\n\r\na lot of various stuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputShortMessage() throws Exception {
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        final NStringEntity entity = new NStringEntity("stuff");
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new RequestReadyAnswer(request)).when(
+            handler).requestReady(Mockito.<NHttpClientConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\n\r\nstuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(1)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessage() throws Exception {
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new RequestReadyAnswer(request)).when(
+            handler).requestReady(Mockito.<NHttpClientConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\n\r\na lot of various stuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessageSaturatedChannel() throws Exception {
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64, 24));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new RequestReadyAnswer(request)).when(
+            handler).requestReady(Mockito.<NHttpClientConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\n\r\na lot", wchannel.dump(Consts.ASCII));
+        Assert.assertEquals(17, conn.outbuf.length());
+
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessageSaturatedChannel2() throws Exception {
+        conn = new DefaultNHttpClientConnection(session, 24);
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        final NStringEntity entity = new NStringEntity("a loooooooooooooooooooooooot of various stuff");
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64, 24));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new RequestReadyAnswer(request)).when(
+            handler).requestReady(Mockito.<NHttpClientConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNotNull(conn.getHttpRequest());
+        Assert.assertNotNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\n\r\na loo", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(3)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongChunkedMessage() throws Exception {
+        conn = new DefaultNHttpClientConnection(session, 64);
+
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        entity.setChunked(true);
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new RequestReadyAnswer(request)).when(
+            handler).requestReady(Mockito.<NHttpClientConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n" +
+                "5\r\na lot\r\n11\r\n of various stuff\r\n0\r\n\r\n", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongChunkedMessageSaturatedChannel() throws Exception {
+        conn = new DefaultNHttpClientConnection(session, 64);
+
+        final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        entity.setChunked(true);
+        request.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64, 64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new RequestReadyAnswer(request)).when(
+            handler).requestReady(Mockito.<NHttpClientConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpRequest());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n" +
+                "5\r\na lot\r\n11\r\n of", wchannel.dump(Consts.ASCII));
+        Assert.assertEquals(21, conn.outbuf.length());
+
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputClosingConnection() throws Exception {
+        final BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitRequest(request);
+        conn.close();
+
+        Assert.assertEquals(NHttpConnection.CLOSING, conn.getStatus());
+
+        conn.produceOutput(handler);
+
+        Assert.assertEquals(NHttpConnection.CLOSED, conn.getStatus());
+
+        Mockito.verify(wchannel, Mockito.times(1)).write(Matchers.<ByteBuffer>any());
+        Mockito.verify(session, Mockito.times(1)).close();
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(handler, Mockito.never()).requestReady(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.never()).outputReady(
+            Mockito.<NHttpClientConnection>any(), Mockito.<ContentEncoder>any());
+    }
+
+    static class ResponseCapturingAnswer implements Answer<Void> {
+
+        private final LinkedList<HttpResponse> responses;
+
+        ResponseCapturingAnswer(final LinkedList<HttpResponse> responses) {
+            super();
+            this.responses = responses;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final NHttpClientConnection conn = (NHttpClientConnection) args[0];
+            if (conn != null) {
+                final HttpResponse response = conn.getHttpResponse();
+                if (response != null) {
+                    responses.add(response);
+                }
+            }
+            return null;
+        }
+
+    }
+
+    static class ConsumeContentAnswer implements Answer<Void> {
+
+        private final SimpleInputBuffer buf;
+
+        ConsumeContentAnswer(final SimpleInputBuffer buf) {
+            super();
+            this.buf = buf;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final ContentDecoder decoder = (ContentDecoder) args[1];
+            buf.consumeContent(decoder);
+            return null;
+        }
+
+    };
+
+    @Test
+    public void testConsumeInputShortMessage() throws Exception {
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nstuff"}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpResponse> responses = new LinkedList<HttpResponse>();
+
+        Mockito.doAnswer(new ResponseCapturingAnswer(responses)).when(
+            handler).responseReceived(Mockito.<NHttpClientConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentDecoder>any());
+
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(43, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.times(1)).responseReceived(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.times(1)).inputReady(
+            Mockito.<NHttpClientConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(2)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpClientConnection>any(), Mockito.<Exception>any());
+
+        Assert.assertFalse(responses.isEmpty());
+        final HttpResponse response = responses.getFirst();
+        Assert.assertNotNull(response);
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
+        final HttpEntity entity = response.getEntity();
+        Assert.assertNotNull(entity);
+        Assert.assertEquals(5, entity.getContentLength());
+    }
+
+    @Test
+    public void testConsumeInputLongMessage() throws Exception {
+        conn = new DefaultNHttpClientConnection(session, 1024);
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\na lot of stuff",
+                "", ""}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpResponse> responses = new LinkedList<HttpResponse>();
+
+        Mockito.doAnswer(new ResponseCapturingAnswer(responses)).when(
+            handler).responseReceived(Mockito.<NHttpClientConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentDecoder>any());
+
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNotNull(conn.getHttpResponse());
+        Assert.assertNotNull(conn.contentDecoder);
+        Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(54, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.times(1)).responseReceived(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.times(1)).inputReady(
+            Mockito.<NHttpClientConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(2)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpClientConnection>any(), Mockito.<Exception>any());
+
+        Assert.assertFalse(responses.isEmpty());
+        final HttpResponse response = responses.getFirst();
+        Assert.assertNotNull(response);
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
+        final HttpEntity entity = response.getEntity();
+        Assert.assertNotNull(entity);
+        Assert.assertEquals(100, entity.getContentLength());
+
+        conn.consumeInput(handler);
+
+        Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(54, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(rchannel, Mockito.times(3)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpClientConnection>any(), Mockito.<Exception>any());
+    }
+
+    @Test
+    public void testConsumeInputBasicMessageNoEntity() throws Exception {
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"HTTP/1.1 100 Continue\r\n\r\n"}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpResponse> responses = new LinkedList<HttpResponse>();
+
+        Mockito.doAnswer(new ResponseCapturingAnswer(responses)).when(
+            handler).responseReceived(Mockito.<NHttpClientConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentDecoder>any());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+
+        Mockito.verify(handler, Mockito.times(1)).responseReceived(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpClientConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(1)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpClientConnection>any(), Mockito.<Exception>any());
+
+        Assert.assertFalse(responses.isEmpty());
+        final HttpResponse response = responses.getFirst();
+        Assert.assertNotNull(response);
+        Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
+        Assert.assertEquals(100, response.getStatusLine().getStatusCode());
+        final HttpEntity entity = response.getEntity();
+        Assert.assertNull(entity);
+    }
+
+    @Test
+    public void testConsumeInputNoData() throws Exception {
+        conn = new DefaultNHttpClientConnection(session, 1024);
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"", ""}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpResponse> responses = new LinkedList<HttpResponse>();
+
+        Mockito.doAnswer(new ResponseCapturingAnswer(responses)).when(
+            handler).responseReceived(Mockito.<NHttpClientConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpClientConnection>any(), Mockito.<ContentDecoder>any());
+
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(0, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.never()).responseReceived(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpClientConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(1)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpClientConnection>any(), Mockito.<Exception>any());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(0, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.never()).responseReceived(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpClientConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(2)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpClientConnection>any(), Mockito.<Exception>any());
+
+        conn.consumeInput(handler);
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(0, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.never()).responseReceived(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpClientConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(3)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.times(1)).endOfInput(
+            Mockito.<NHttpClientConnection>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpClientConnection>any(), Mockito.<Exception>any());
+
+    }
+
+    @Test
+    public void testConsumeInputConnectionClosed() throws Exception {
+        conn = new DefaultNHttpClientConnection(session, 1024);
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"", ""}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        conn.close();
+        conn.consumeInput(handler);
+        Mockito.verify(rchannel, Mockito.never()).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_READ);
+    }
+
+}
\ No newline at end of file

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

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

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

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpServerConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpServerConnection.java?rev=1448420&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpServerConnection.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestDefaultNHttpServerConnection.java Wed Feb 20 21:16:19 2013
@@ -0,0 +1,672 @@
+/*
+ * ====================================================================
+ * 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.impl.nio;
+
+import java.nio.ByteBuffer;
+import java.nio.channels.ByteChannel;
+import java.nio.channels.SelectionKey;
+import java.util.LinkedList;
+
+import org.apache.http.ByteChannelMock;
+import org.apache.http.Consts;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.ReadableByteChannelMock;
+import org.apache.http.WritableByteChannelMock;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.nio.codecs.LengthDelimitedDecoder;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.nio.ContentDecoder;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.NHttpServerConnection;
+import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.NHttpServerEventHandler;
+import org.apache.http.nio.entity.HttpAsyncContentProducer;
+import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.nio.reactor.IOSession;
+import org.apache.http.nio.util.SimpleInputBuffer;
+import org.apache.http.protocol.HTTP;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class TestDefaultNHttpServerConnection {
+
+    @Mock
+    private IOSession session;
+    @Mock
+    private ByteChannel channel;
+    @Mock
+    private NHttpServerEventHandler handler;
+
+    private DefaultNHttpServerConnection conn;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        conn = new DefaultNHttpServerConnection(session, 32);
+    }
+
+    @Test
+    public void testSubmitRequest() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        conn.submitResponse(response);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertTrue(conn.hasBufferedOutput());
+
+        Mockito.verify(session).setEvent(SelectionKey.OP_WRITE);
+    }
+
+    @Test
+    public void testSubmitEntityEnclosingRequest() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.setEntity(new StringEntity("stuff"));
+
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+        conn.submitResponse(response);
+
+        Assert.assertSame(response, conn.getHttpResponse());
+        Assert.assertTrue(conn.hasBufferedOutput());
+        Assert.assertTrue(conn.isResponseSubmitted());
+        Assert.assertNotNull(conn.contentEncoder);
+        Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+
+        Mockito.verify(session).setEvent(SelectionKey.OP_WRITE);
+    }
+
+    @Test
+    public void testOutputReset() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.setEntity(new StringEntity("stuff"));
+
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitResponse(response);
+
+        Assert.assertNotNull(conn.getHttpResponse());
+        Assert.assertNotNull(conn.contentEncoder);
+
+        conn.resetOutput();
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+    }
+
+    static class ResponseReadyAnswer implements Answer<Void> {
+
+        private final HttpResponse response;
+
+        ResponseReadyAnswer(final HttpResponse response) {
+            super();
+            this.response = response;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final NHttpServerConnection conn = (NHttpServerConnection) args[0];
+            conn.submitResponse(response);
+            return null;
+        }
+    };
+
+    static class ProduceContentAnswer implements Answer<Void> {
+
+        private final HttpAsyncContentProducer contentProducer;
+
+        ProduceContentAnswer(final HttpAsyncContentProducer contentProducer) {
+            super();
+            this.contentProducer = contentProducer;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final IOControl ioctrl = (IOControl) args[0];
+            final ContentEncoder encoder = (ContentEncoder) args[1];
+            contentProducer.produceContent(encoder, ioctrl);
+            return null;
+        }
+    };
+
+    @Test
+    public void testProduceOutputShortMessageAfterSubmit() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        final NStringEntity entity = new NStringEntity("stuff");
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitResponse(response);
+        Assert.assertEquals(19, conn.outbuf.length());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\n\r\nstuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(1)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessageAfterSubmit() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitResponse(response);
+        Assert.assertEquals(19, conn.outbuf.length());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\n\r\na lot of various stuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputShortMessage() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        final NStringEntity entity = new NStringEntity("stuff");
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new ResponseReadyAnswer(response)).when(
+            handler).responseReady(Mockito.<NHttpServerConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\n\r\nstuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(1)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessage() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new ResponseReadyAnswer(response)).when(
+            handler).responseReady(Mockito.<NHttpServerConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\n\r\na lot of various stuff", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessageSaturatedChannel() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64, 24));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new ResponseReadyAnswer(response)).when(
+            handler).responseReady(Mockito.<NHttpServerConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\n\r\na lot", wchannel.dump(Consts.ASCII));
+        Assert.assertEquals(17, conn.outbuf.length());
+
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongMessageSaturatedChannel2() throws Exception {
+        conn = new DefaultNHttpServerConnection(session, 24);
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        final NStringEntity entity = new NStringEntity("a loooooooooooooooooooooooot of various stuff");
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64, 24));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new ResponseReadyAnswer(response)).when(
+            handler).responseReady(Mockito.<NHttpServerConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNotNull(conn.getHttpResponse());
+        Assert.assertNotNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\n\r\na loo", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(3)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongChunkedMessage() throws Exception {
+        conn = new DefaultNHttpServerConnection(session, 64);
+
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        entity.setChunked(true);
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new ResponseReadyAnswer(response)).when(
+            handler).responseReady(Mockito.<NHttpServerConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" +
+                "5\r\na lot\r\n11\r\n of various stuff\r\n0\r\n\r\n", wchannel.dump(Consts.ASCII));
+
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputLongChunkedMessageSaturatedChannel() throws Exception {
+        conn = new DefaultNHttpServerConnection(session, 64);
+
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
+        final NStringEntity entity = new NStringEntity("a lot of various stuff");
+        entity.setChunked(true);
+        response.setEntity(entity);
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64, 64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        Mockito.doAnswer(new ResponseReadyAnswer(response)).when(
+            handler).responseReady(Mockito.<NHttpServerConnection>any());
+
+        Mockito.doAnswer(new ProduceContentAnswer(entity)).when(
+            handler).outputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+
+        conn.produceOutput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentEncoder);
+        Assert.assertEquals("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" +
+                "5\r\na lot\r\n11\r\n of", wchannel.dump(Consts.ASCII));
+        Assert.assertEquals(21, conn.outbuf.length());
+
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(wchannel, Mockito.times(2)).write(Matchers.<ByteBuffer>any());
+    }
+
+    @Test
+    public void testProduceOutputClosingConnection() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+
+        final WritableByteChannelMock wchannel = Mockito.spy(new WritableByteChannelMock(64));
+        final ByteChannelMock channel = new ByteChannelMock(null, wchannel);
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.submitResponse(response);
+        conn.close();
+
+        Assert.assertEquals(NHttpConnection.CLOSING, conn.getStatus());
+
+        conn.produceOutput(handler);
+
+        Assert.assertEquals(NHttpConnection.CLOSED, conn.getStatus());
+
+        Mockito.verify(wchannel, Mockito.times(1)).write(Matchers.<ByteBuffer>any());
+        Mockito.verify(session, Mockito.times(1)).close();
+        Mockito.verify(session, Mockito.never()).clearEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(handler, Mockito.never()).responseReady(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.never()).outputReady(
+            Mockito.<NHttpServerConnection>any(), Mockito.<ContentEncoder>any());
+    }
+
+    static class RequestCapturingAnswer implements Answer<Void> {
+
+        private final LinkedList<HttpRequest> requests;
+
+        RequestCapturingAnswer(final LinkedList<HttpRequest> requests) {
+            super();
+            this.requests = requests;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final NHttpServerConnection conn = (NHttpServerConnection) args[0];
+            if (conn != null) {
+                final HttpRequest request = conn.getHttpRequest();
+                if (request != null) {
+                    requests.add(request);
+                }
+            }
+            return null;
+        }
+
+    }
+
+    static class ConsumeContentAnswer implements Answer<Void> {
+
+        private final SimpleInputBuffer buf;
+
+        ConsumeContentAnswer(final SimpleInputBuffer buf) {
+            super();
+            this.buf = buf;
+        }
+
+        public Void answer(final InvocationOnMock invocation) throws Throwable {
+            final Object[] args = invocation.getArguments();
+            final ContentDecoder decoder = (ContentDecoder) args[1];
+            buf.consumeContent(decoder);
+            return null;
+        }
+
+    };
+
+    @Test
+    public void testConsumeInputShortMessage() throws Exception {
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"POST / HTTP/1.1\r\nContent-Length: 5\r\n\r\nstuff"}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpRequest> requests = new LinkedList<HttpRequest>();
+
+        Mockito.doAnswer(new RequestCapturingAnswer(requests)).when(
+            handler).requestReceived(Mockito.<NHttpServerConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentDecoder>any());
+
+        Assert.assertEquals(0, conn.getMetrics().getRequestCount());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+        Assert.assertEquals(43, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.times(1)).requestReceived(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.times(1)).inputReady(
+            Mockito.<NHttpServerConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(2)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpServerConnection>any(), Mockito.<Exception>any());
+
+        Assert.assertFalse(requests.isEmpty());
+        final HttpRequest request = requests.getFirst();
+        Assert.assertNotNull(request);
+        Assert.assertEquals(HttpVersion.HTTP_1_1, request.getRequestLine().getProtocolVersion());
+        Assert.assertEquals("POST", request.getRequestLine().getMethod());
+        Assert.assertEquals("/", request.getRequestLine().getUri());
+        Assert.assertTrue(request instanceof HttpEntityEnclosingRequest);
+        final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+        Assert.assertNotNull(entity);
+        Assert.assertEquals(5, entity.getContentLength());
+    }
+
+    @Test
+    public void testConsumeInputLongMessage() throws Exception {
+        conn = new DefaultNHttpServerConnection(session, 1024);
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"POST / HTTP/1.1\r\nContent-Length: 100\r\n\r\na lot of stuff",
+                "", ""}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpRequest> requests = new LinkedList<HttpRequest>();
+
+        Mockito.doAnswer(new RequestCapturingAnswer(requests)).when(
+            handler).requestReceived(Mockito.<NHttpServerConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentDecoder>any());
+
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNotNull(conn.getHttpRequest());
+        Assert.assertNotNull(conn.contentDecoder);
+        Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+        Assert.assertEquals(54, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.times(1)).requestReceived(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.times(1)).inputReady(
+            Mockito.<NHttpServerConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(2)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpServerConnection>any(), Mockito.<Exception>any());
+
+        Assert.assertFalse(requests.isEmpty());
+        final HttpRequest request = requests.getFirst();
+        Assert.assertNotNull(request);
+        Assert.assertEquals(HttpVersion.HTTP_1_1, request.getRequestLine().getProtocolVersion());
+        Assert.assertEquals("POST", request.getRequestLine().getMethod());
+        Assert.assertEquals("/", request.getRequestLine().getUri());
+        Assert.assertTrue(request instanceof HttpEntityEnclosingRequest);
+        final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+        Assert.assertNotNull(entity);
+        Assert.assertEquals(100, entity.getContentLength());
+
+        conn.consumeInput(handler);
+
+        Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+        Assert.assertEquals(54, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(rchannel, Mockito.times(3)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpServerConnection>any(), Mockito.<Exception>any());
+    }
+
+    @Test
+    public void testConsumeInputBasicMessageNoEntity() throws Exception {
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"GET / HTTP/1.1\r\n\r\n"}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpRequest> requests = new LinkedList<HttpRequest>();
+
+        Mockito.doAnswer(new RequestCapturingAnswer(requests)).when(
+            handler).requestReceived(Mockito.<NHttpServerConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentDecoder>any());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+
+        Mockito.verify(handler, Mockito.times(1)).requestReceived(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpServerConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(1)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpServerConnection>any(), Mockito.<Exception>any());
+
+        Assert.assertFalse(requests.isEmpty());
+        final HttpRequest request = requests.getFirst();
+        Assert.assertNotNull(request);
+        Assert.assertEquals(HttpVersion.HTTP_1_1, request.getRequestLine().getProtocolVersion());
+        Assert.assertEquals("GET", request.getRequestLine().getMethod());
+        Assert.assertEquals("/", request.getRequestLine().getUri());
+        Assert.assertFalse(request instanceof HttpEntityEnclosingRequest);
+    }
+
+    @Test
+    public void testConsumeInputNoData() throws Exception {
+        conn = new DefaultNHttpServerConnection(session, 1024);
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"", ""}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        final LinkedList<HttpRequest> requests = new LinkedList<HttpRequest>();
+
+        Mockito.doAnswer(new RequestCapturingAnswer(requests)).when(
+            handler).requestReceived(Mockito.<NHttpServerConnection>any());
+        Mockito.doAnswer(new ConsumeContentAnswer(new SimpleInputBuffer(64))).when(
+            handler).inputReady(Mockito.<NHttpServerConnection>any(), Mockito.<ContentDecoder>any());
+
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(0, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.never()).requestReceived(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpServerConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(1)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpServerConnection>any(), Mockito.<Exception>any());
+
+        conn.consumeInput(handler);
+
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(0, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.never()).requestReceived(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpServerConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(2)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpServerConnection>any(), Mockito.<Exception>any());
+
+        conn.consumeInput(handler);
+        Assert.assertNull(conn.getHttpResponse());
+        Assert.assertNull(conn.contentDecoder);
+        Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+        Assert.assertEquals(0, conn.getMetrics().getReceivedBytesCount());
+
+        Mockito.verify(handler, Mockito.never()).requestReceived(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.never()).inputReady(
+            Mockito.<NHttpServerConnection>any(), Mockito.<LengthDelimitedDecoder>any());
+        Mockito.verify(rchannel, Mockito.times(3)).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(handler, Mockito.times(1)).endOfInput(
+            Mockito.<NHttpServerConnection>any());
+        Mockito.verify(handler, Mockito.never()).exception(
+            Mockito.<NHttpServerConnection>any(), Mockito.<Exception>any());
+
+    }
+
+    @Test
+    public void testConsumeInputConnectionClosed() throws Exception {
+        conn = new DefaultNHttpServerConnection(session, 1024);
+        final ReadableByteChannelMock rchannel = Mockito.spy(new ReadableByteChannelMock(
+            new String[] {"", ""}, Consts.ASCII));
+        final ByteChannelMock channel = new ByteChannelMock(rchannel, null);
+        Mockito.when(session.channel()).thenReturn(channel);
+        Mockito.when(session.getEventMask()).thenReturn(SelectionKey.OP_READ);
+
+        conn.close();
+        conn.consumeInput(handler);
+        Mockito.verify(rchannel, Mockito.never()).read(Mockito.<ByteBuffer>any());
+        Mockito.verify(session, Mockito.times(1)).clearEvent(SelectionKey.OP_READ);
+    }
+
+}
\ No newline at end of file

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

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

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

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestNHttpConnectionBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestNHttpConnectionBase.java?rev=1448420&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestNHttpConnectionBase.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/TestNHttpConnectionBase.java Wed Feb 20 21:16:19 2013
@@ -0,0 +1,279 @@
+/*
+ * ====================================================================
+ * 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.impl.nio;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.nio.channels.ByteChannel;
+import java.nio.channels.SelectionKey;
+
+import junit.framework.Assert;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpVersion;
+import org.apache.http.config.MessageConstraints;
+import org.apache.http.impl.entity.LaxContentLengthStrategy;
+import org.apache.http.impl.entity.StrictContentLengthStrategy;
+import org.apache.http.impl.nio.codecs.ChunkDecoder;
+import org.apache.http.impl.nio.codecs.ChunkEncoder;
+import org.apache.http.impl.nio.codecs.IdentityDecoder;
+import org.apache.http.impl.nio.codecs.IdentityEncoder;
+import org.apache.http.impl.nio.codecs.LengthDelimitedDecoder;
+import org.apache.http.impl.nio.codecs.LengthDelimitedEncoder;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.reactor.IOSession;
+import org.apache.http.nio.util.HeapByteBufferAllocator;
+import org.apache.http.protocol.HTTP;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class TestNHttpConnectionBase {
+
+    @Mock
+    private IOSession session;
+    @Mock
+    private ByteChannel channel;
+
+    private NHttpConnectionBase conn;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        conn = new NHttpConnectionBase(session, 1024, 1024, HeapByteBufferAllocator.INSTANCE,
+            null, null,
+            MessageConstraints.DEFAULT,
+            LaxContentLengthStrategy.INSTANCE,
+            StrictContentLengthStrategy.INSTANCE);
+    }
+
+    @Test
+    public void testBasics() throws Exception {
+        Assert.assertEquals("[ACTIVE]", conn.toString());
+
+        Mockito.verify(session).setBufferStatus(conn);
+    }
+
+    @Test
+    public void testSessionBind() throws Exception {
+        final InetSocketAddress local = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), 8888);
+        final InetSocketAddress remote = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), 80);
+        Mockito.when(session.getLocalAddress()).thenReturn(local);
+        Mockito.when(session.getRemoteAddress()).thenReturn(remote);
+        Mockito.when(session.isClosed()).thenReturn(Boolean.FALSE);
+
+        conn.bind(session);
+
+        Mockito.verify(session, Mockito.times(2)).setBufferStatus(conn);
+
+        Assert.assertEquals("127.0.0.1:8888<->10.0.0.2:80[ACTIVE]", conn.toString());
+        Assert.assertTrue(conn.isOpen());
+        Assert.assertEquals(8888, conn.getLocalPort());
+        Assert.assertEquals(80, conn.getRemotePort());
+        Assert.assertEquals(InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), conn.getLocalAddress());
+        Assert.assertEquals(InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), conn.getRemoteAddress());
+    }
+
+    @Test
+    public void testClose() throws Exception {
+        final InetSocketAddress local = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), 8888);
+        final InetSocketAddress remote = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), 80);
+        Mockito.when(session.getLocalAddress()).thenReturn(local);
+        Mockito.when(session.getRemoteAddress()).thenReturn(remote);
+        Mockito.when(session.isClosed()).thenReturn(Boolean.FALSE);
+
+        conn.close();
+
+        Assert.assertEquals(NHttpConnection.CLOSED, conn.getStatus());
+        Assert.assertEquals("127.0.0.1:8888<->10.0.0.2:80[CLOSED]", conn.toString());
+
+        Mockito.verify(session).close();
+    }
+
+    @Test
+    public void testCloseWithBufferedData() throws Exception {
+        final InetSocketAddress local = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), 8888);
+        final InetSocketAddress remote = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), 80);
+        Mockito.when(session.getLocalAddress()).thenReturn(local);
+        Mockito.when(session.getRemoteAddress()).thenReturn(remote);
+        Mockito.when(session.isClosed()).thenReturn(Boolean.FALSE);
+
+        conn.outbuf.writeLine("stuff");
+        conn.close();
+
+        Assert.assertEquals(NHttpConnection.CLOSING, conn.getStatus());
+        conn.close();
+        Assert.assertEquals(NHttpConnection.CLOSING, conn.getStatus());
+        Assert.assertEquals("127.0.0.1:8888<->10.0.0.2:80[CLOSING]", conn.toString());
+
+        Mockito.verify(session).setEvent(SelectionKey.OP_WRITE);
+        Mockito.verify(session, Mockito.never()).close();
+    }
+
+    @Test
+    public void testShutdown() throws Exception {
+        final InetSocketAddress local = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), 8888);
+        final InetSocketAddress remote = new InetSocketAddress(
+            InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), 80);
+        Mockito.when(session.getLocalAddress()).thenReturn(local);
+        Mockito.when(session.getRemoteAddress()).thenReturn(remote);
+        Mockito.when(session.isClosed()).thenReturn(Boolean.FALSE);
+
+        conn.outbuf.writeLine("stuff");
+        conn.shutdown();
+
+        Assert.assertEquals(NHttpConnection.CLOSED, conn.getStatus());
+        Assert.assertEquals("127.0.0.1:8888<->10.0.0.2:80[CLOSED]", conn.toString());
+
+        Mockito.verify(session).shutdown();
+    }
+
+    @Test
+    public void testContextOperations() throws Exception {
+        conn.getContext().getAttribute("stuff");
+        conn.getContext().setAttribute("stuff", "blah");
+        conn.getContext().removeAttribute("other stuff");
+
+        Mockito.verify(session).getAttribute("stuff");
+        Mockito.verify(session).setAttribute("stuff", "blah");
+        Mockito.verify(session).removeAttribute("other stuff");
+    }
+
+    @Test
+    public void testIOOperations() throws Exception {
+        conn.suspendInput();
+        Mockito.verify(session).clearEvent(SelectionKey.OP_READ);
+
+        conn.suspendOutput();
+        Mockito.verify(session).clearEvent(SelectionKey.OP_WRITE);
+
+        conn.requestInput();
+        Mockito.verify(session).setEvent(SelectionKey.OP_READ);
+
+        conn.requestOutput();
+        Mockito.verify(session).setEvent(SelectionKey.OP_WRITE);
+    }
+
+    @Test
+    public void testSocketTimeout() throws Exception {
+        conn.getSocketTimeout();
+        Mockito.verify(session).getSocketTimeout();
+
+        conn.setSocketTimeout(123);
+        Mockito.verify(session).setSocketTimeout(123);
+    }
+
+    @Test
+    public void testPrepareIdentityDecoder() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        final HttpEntity entity = conn.prepareDecoder(response);
+        Assert.assertNotNull(entity);
+        Assert.assertEquals(-1, entity.getContentLength());
+        Assert.assertFalse(entity.isChunked());
+        Assert.assertTrue(conn.contentDecoder instanceof IdentityDecoder);
+    }
+
+    @Test
+    public void testPrepareLengthDelimitedDecoder() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.addHeader(HTTP.CONTENT_LEN, "10");
+        response.addHeader(HTTP.CONTENT_TYPE, "stuff");
+        response.addHeader(HTTP.CONTENT_ENCODING, "identity");
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        final HttpEntity entity = conn.prepareDecoder(response);
+        Assert.assertNotNull(entity);
+        Assert.assertEquals(10, entity.getContentLength());
+        Assert.assertFalse(entity.isChunked());
+        Assert.assertNotNull(entity.getContentType());
+        Assert.assertEquals("stuff", entity.getContentType().getValue());
+        Assert.assertNotNull(entity.getContentEncoding());
+        Assert.assertEquals("identity", entity.getContentEncoding().getValue());
+        Assert.assertTrue(conn.contentDecoder instanceof LengthDelimitedDecoder);
+    }
+
+    @Test
+    public void testPrepareChunkDecoder() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.addHeader(HTTP.TRANSFER_ENCODING, "chunked");
+        response.addHeader(HTTP.CONTENT_TYPE, "stuff");
+        response.addHeader(HTTP.CONTENT_ENCODING, "identity");
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        final HttpEntity entity = conn.prepareDecoder(response);
+        Assert.assertNotNull(entity);
+        Assert.assertEquals(-1, entity.getContentLength());
+        Assert.assertTrue(entity.isChunked());
+        Assert.assertNotNull(entity.getContentType());
+        Assert.assertEquals("stuff", entity.getContentType().getValue());
+        Assert.assertNotNull(entity.getContentEncoding());
+        Assert.assertEquals("identity", entity.getContentEncoding().getValue());
+        Assert.assertTrue(conn.contentDecoder instanceof ChunkDecoder);
+    }
+
+    @Test
+    public void testPrepareIdentityEncoder() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.prepareEncoder(response);
+        Assert.assertTrue(conn.contentEncoder instanceof IdentityEncoder);
+    }
+
+    @Test
+    public void testPrepareLengthDelimitedEncoder() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.addHeader(HTTP.CONTENT_LEN, "10");
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.prepareEncoder(response);
+        Assert.assertTrue(conn.contentEncoder instanceof LengthDelimitedEncoder);
+    }
+
+    @Test
+    public void testPrepareChunkEncoder() throws Exception {
+        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        response.addHeader(HTTP.TRANSFER_ENCODING, "chunked");
+        Mockito.when(session.channel()).thenReturn(channel);
+
+        conn.prepareEncoder(response);
+        Assert.assertTrue(conn.contentEncoder instanceof ChunkEncoder);
+    }
+
+}

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

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

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

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/CodecTestUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/CodecTestUtils.java?rev=1448420&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/CodecTestUtils.java (added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/CodecTestUtils.java Wed Feb 20 21:16:19 2013
@@ -0,0 +1,68 @@
+/*
+ * ====================================================================
+ * 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.impl.nio.codecs;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.nio.ByteBuffer;
+
+import org.apache.http.util.EncodingUtils;
+
+class CodecTestUtils {
+
+    public static ByteBuffer wrap(final String s) {
+        return ByteBuffer.wrap(EncodingUtils.getAsciiBytes(s));
+    }
+
+    public static String convert(final ByteBuffer src) {
+        src.flip();
+        final StringBuilder buffer = new StringBuilder(src.remaining());
+        while (src.hasRemaining()) {
+            buffer.append((char)(src.get() & 0xff));
+        }
+        return buffer.toString();
+    }
+
+    public static String readFromFile(final File file) throws Exception {
+        final FileInputStream filestream = new FileInputStream(file);
+        final InputStreamReader reader = new InputStreamReader(filestream);
+        try {
+            final StringBuilder buffer = new StringBuilder();
+            final char[] tmp = new char[2048];
+            int l;
+            while ((l = reader.read(tmp)) != -1) {
+                buffer.append(tmp, 0, l);
+            }
+            return buffer.toString();
+        } finally {
+            reader.close();
+        }
+    }
+
+}

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

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

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



Mime
View raw message