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 2013/03/19 14:13:02 UTC
svn commit: r1458268 - in /httpcomponents/httpcore/trunk/httpcore/src:
main/java/org/apache/http/impl/ test/java/org/apache/http/impl/
Author: olegk
Date: Tue Mar 19 13:13:01 2013
New Revision: 1458268
URL: http://svn.apache.org/r1458268
Log:
Unit tests for default blocking connection implementations
Added:
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java (with props)
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java (with props)
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java (with props)
Modified:
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/BHttpConnectionBase.java
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/BHttpConnectionBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/BHttpConnectionBase.java?rev=1458268&r1=1458267&r2=1458268&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/BHttpConnectionBase.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/BHttpConnectionBase.java Tue Mar 19 13:13:01 2013
@@ -328,26 +328,31 @@ public class BHttpConnectionBase impleme
}
}
- protected boolean awaitInput(final int timeout) throws IOException {
- if (this.inbuffer.hasBufferedData()) {
- return true;
- }
+ private int fillInputBuffer(final int timeout) throws IOException {
final int oldtimeout = this.socket.getSoTimeout();
try {
this.socket.setSoTimeout(timeout);
- this.inbuffer.fillBuffer();
- return this.inbuffer.hasBufferedData();
+ return this.inbuffer.fillBuffer();
} finally {
this.socket.setSoTimeout(oldtimeout);
}
}
+ protected boolean awaitInput(final int timeout) throws IOException {
+ if (this.inbuffer.hasBufferedData()) {
+ return true;
+ }
+ fillInputBuffer(timeout);
+ return this.inbuffer.hasBufferedData();
+ }
+
public boolean isStale() {
if (!isOpen()) {
return true;
}
try {
- return !awaitInput(1);
+ final int bytesRead = fillInputBuffer(1);
+ return bytesRead < 0;
} catch (final SocketTimeoutException ex) {
return false;
} catch (final IOException ex) {
@@ -380,7 +385,7 @@ public class BHttpConnectionBase impleme
}
return buffer.toString();
} else {
- return super.toString();
+ return "[Not bound]";
}
}
Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java?rev=1458268&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java Tue Mar 19 13:13:01 2013
@@ -0,0 +1,390 @@
+/*
+ * ====================================================================
+ * 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;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+
+import junit.framework.Assert;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+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.io.ChunkedInputStream;
+import org.apache.http.impl.io.ChunkedOutputStream;
+import org.apache.http.impl.io.ContentLengthInputStream;
+import org.apache.http.impl.io.ContentLengthOutputStream;
+import org.apache.http.impl.io.IdentityInputStream;
+import org.apache.http.impl.io.IdentityOutputStream;
+import org.apache.http.message.BasicHttpResponse;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class TestBHttpConnectionBase {
+
+ @Mock
+ private Socket socket;
+
+ private BHttpConnectionBase conn;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ conn = new BHttpConnectionBase(1024, 1024,
+ null, null,
+ MessageConstraints.DEFAULT,
+ LaxContentLengthStrategy.INSTANCE,
+ StrictContentLengthStrategy.INSTANCE);
+ }
+
+ @Test
+ public void testBasics() throws Exception {
+ Assert.assertFalse(conn.isOpen());
+ Assert.assertEquals(-1, conn.getLocalPort());
+ Assert.assertEquals(-1, conn.getRemotePort());
+ Assert.assertEquals(null, conn.getLocalAddress());
+ Assert.assertEquals(null, conn.getRemoteAddress());
+ Assert.assertEquals("[Not bound]", conn.toString());
+ }
+
+ @Test
+ public void testSocketBind() throws Exception {
+ final InetAddress localAddress = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
+ final int localPort = 8888;
+ final InetAddress remoteAddress = InetAddress.getByAddress(new byte[] {10, 0, 0, 2});
+ final int remotePort = 80;
+ final InetSocketAddress localSockAddress = new InetSocketAddress(localAddress, localPort);
+ final InetSocketAddress remoteSockAddress = new InetSocketAddress(remoteAddress, remotePort);
+ Mockito.when(socket.getLocalSocketAddress()).thenReturn(localSockAddress);
+ Mockito.when(socket.getRemoteSocketAddress()).thenReturn(remoteSockAddress);
+ Mockito.when(socket.getLocalAddress()).thenReturn(localAddress);
+ Mockito.when(socket.getLocalPort()).thenReturn(localPort);
+ Mockito.when(socket.getInetAddress()).thenReturn(remoteAddress);
+ Mockito.when(socket.getPort()).thenReturn(remotePort);
+ conn.bind(socket);
+
+ Assert.assertEquals("127.0.0.1:8888<->10.0.0.2:80", 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 testConnectionClose() throws Exception {
+ final InputStream instream = Mockito.mock(InputStream.class);
+ final OutputStream outstream = Mockito.mock(OutputStream.class);
+
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+ Mockito.when(socket.getOutputStream()).thenReturn(outstream);
+
+ conn.bind(socket);
+ conn.ensureOpen();
+ conn.getSessionOutputBuffer().write(0);
+
+ Assert.assertTrue(conn.isOpen());
+
+ conn.close();
+
+ Assert.assertFalse(conn.isOpen());
+
+ Mockito.verify(outstream, Mockito.times(1)).write(
+ Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
+ Mockito.verify(socket, Mockito.times(1)).shutdownInput();
+ Mockito.verify(socket, Mockito.times(1)).shutdownOutput();
+ Mockito.verify(socket, Mockito.times(1)).close();
+
+ conn.close();
+ Mockito.verify(socket, Mockito.times(1)).close();
+ Mockito.verify(outstream, Mockito.times(1)).write(
+ Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
+ }
+
+ @Test
+ public void testConnectionShutdown() throws Exception {
+ final InputStream instream = Mockito.mock(InputStream.class);
+ final OutputStream outstream = Mockito.mock(OutputStream.class);
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+ Mockito.when(socket.getOutputStream()).thenReturn(outstream);
+
+ conn.bind(socket);
+ conn.ensureOpen();
+ conn.getSessionOutputBuffer().write(0);
+
+ Assert.assertTrue(conn.isOpen());
+
+ conn.shutdown();
+
+ Assert.assertFalse(conn.isOpen());
+
+ Mockito.verify(outstream, Mockito.never()).write(
+ Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
+ Mockito.verify(socket, Mockito.never()).shutdownInput();
+ Mockito.verify(socket, Mockito.never()).shutdownOutput();
+ Mockito.verify(socket, Mockito.times(1)).close();
+
+ conn.close();
+ Mockito.verify(socket, Mockito.times(1)).close();
+
+ conn.shutdown();
+ Mockito.verify(socket, Mockito.times(2)).close();
+ }
+
+ @Test
+ public void testPrepareInputLengthDelimited() throws Exception {
+ final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ message.addHeader("Content-Length", "10");
+ message.addHeader("Content-Type", "stuff");
+ message.addHeader("Content-Encoding", "identity");
+ final HttpEntity entity = conn.prepareInput(message);
+ Assert.assertNotNull(entity);
+ Assert.assertFalse(entity.isChunked());
+ Assert.assertEquals(10, entity.getContentLength());
+ final Header ct = entity.getContentType();
+ Assert.assertNotNull(ct);
+ Assert.assertEquals("stuff", ct.getValue());
+ final Header ce = entity.getContentEncoding();
+ Assert.assertNotNull(ce);
+ Assert.assertEquals("identity", ce.getValue());
+ final InputStream instream = entity.getContent();
+ Assert.assertNotNull(instream);
+ Assert.assertTrue((instream instanceof ContentLengthInputStream));
+ }
+
+ @Test
+ public void testPrepareInputChunked() throws Exception {
+ final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ message.addHeader("Transfer-Encoding", "chunked");
+ final HttpEntity entity = conn.prepareInput(message);
+ Assert.assertNotNull(entity);
+ Assert.assertTrue(entity.isChunked());
+ Assert.assertEquals(-1, entity.getContentLength());
+ final InputStream instream = entity.getContent();
+ Assert.assertNotNull(instream);
+ Assert.assertTrue((instream instanceof ChunkedInputStream));
+ }
+
+ @Test
+ public void testPrepareInputIdentity() throws Exception {
+ final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ final HttpEntity entity = conn.prepareInput(message);
+ Assert.assertNotNull(entity);
+ Assert.assertFalse(entity.isChunked());
+ Assert.assertEquals(-1, entity.getContentLength());
+ final InputStream instream = entity.getContent();
+ Assert.assertNotNull(instream);
+ Assert.assertTrue((instream instanceof IdentityInputStream));
+ }
+
+ @Test
+ public void testPrepareOutputLengthDelimited() throws Exception {
+ final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ message.addHeader("Content-Length", "10");
+ final OutputStream outstream = conn.prepareOutput(message);
+ Assert.assertNotNull(outstream);
+ Assert.assertTrue((outstream instanceof ContentLengthOutputStream));
+ }
+
+ @Test
+ public void testPrepareOutputChunked() throws Exception {
+ final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ message.addHeader("Transfer-Encoding", "chunked");
+ final OutputStream outstream = conn.prepareOutput(message);
+ Assert.assertNotNull(outstream);
+ Assert.assertTrue((outstream instanceof ChunkedOutputStream));
+ }
+
+ @Test
+ public void testPrepareOutputIdentity() throws Exception {
+ final HttpResponse message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ final OutputStream outstream = conn.prepareOutput(message);
+ Assert.assertNotNull(outstream);
+ Assert.assertTrue((outstream instanceof IdentityOutputStream));
+ }
+
+ @Test
+ public void testSetSocketTimeout() throws Exception {
+ conn.bind(socket);
+
+ conn.setSocketTimeout(123);
+
+ Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123);
+ }
+
+ @Test
+ public void testSetSocketTimeoutException() throws Exception {
+ conn.bind(socket);
+
+ Mockito.doThrow(new SocketException()).when(socket).setSoTimeout(Mockito.anyInt());
+
+ conn.setSocketTimeout(123);
+
+ Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123);
+ }
+
+ @Test
+ public void testGetSocketTimeout() throws Exception {
+ Assert.assertEquals(-1, conn.getSocketTimeout());
+
+ Mockito.when(socket.getSoTimeout()).thenReturn(345);
+ conn.bind(socket);
+
+ Assert.assertEquals(345, conn.getSocketTimeout());
+ }
+
+ @Test
+ public void testGetSocketTimeoutException() throws Exception {
+ Assert.assertEquals(-1, conn.getSocketTimeout());
+
+ Mockito.when(socket.getSoTimeout()).thenThrow(new SocketException());
+ conn.bind(socket);
+
+ Assert.assertEquals(-1, conn.getSocketTimeout());
+ }
+
+ @Test
+ public void testAwaitInputInBuffer() throws Exception {
+ final ByteArrayInputStream instream = Mockito.spy(new ByteArrayInputStream(
+ new byte[] {1, 2, 3, 4, 5}));
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+
+ conn.bind(socket);
+ conn.ensureOpen();
+ conn.getSessionInputBuffer().read();
+
+ Assert.assertTrue(conn.awaitInput(432));
+
+ Mockito.verify(socket, Mockito.never()).setSoTimeout(Mockito.anyInt());
+ Mockito.verify(instream, Mockito.times(1)).read(
+ Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
+ }
+
+ @Test
+ public void testAwaitInputInSocket() throws Exception {
+ final ByteArrayInputStream instream = Mockito.spy(new ByteArrayInputStream(
+ new byte[] {1, 2, 3, 4, 5}));
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+ Mockito.when(socket.getSoTimeout()).thenReturn(345);
+
+ conn.bind(socket);
+ conn.ensureOpen();
+
+ Assert.assertTrue(conn.awaitInput(432));
+
+ Mockito.verify(socket, Mockito.times(1)).setSoTimeout(432);
+ Mockito.verify(socket, Mockito.times(1)).setSoTimeout(345);
+ Mockito.verify(instream, Mockito.times(1)).read(
+ Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt());
+ }
+
+ @Test
+ public void testAwaitInputNoData() throws Exception {
+ final InputStream instream = Mockito.mock(InputStream.class);
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+ Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
+ .thenReturn(-1);
+
+ conn.bind(socket);
+ conn.ensureOpen();
+
+ Assert.assertFalse(conn.awaitInput(432));
+ }
+
+ @Test
+ public void testStaleWhenClosed() throws Exception {
+ conn.bind(socket);
+ conn.ensureOpen();
+ conn.close();
+ Assert.assertTrue(conn.isStale());
+ }
+
+ @Test
+ public void testNotStaleWhenHasData() throws Exception {
+ final ByteArrayInputStream instream = Mockito.spy(new ByteArrayInputStream(
+ new byte[] {1, 2, 3, 4, 5}));
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+
+ conn.bind(socket);
+ conn.ensureOpen();
+
+ Assert.assertFalse(conn.isStale());
+ }
+
+ @Test
+ public void testStaleWhenEndOfStream() throws Exception {
+ final InputStream instream = Mockito.mock(InputStream.class);
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+ Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
+ .thenReturn(-1);
+
+ conn.bind(socket);
+ conn.ensureOpen();
+
+ Assert.assertTrue(conn.isStale());
+ }
+
+ @Test
+ public void testNotStaleWhenTimeout() throws Exception {
+ final InputStream instream = Mockito.mock(InputStream.class);
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+ Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
+ .thenThrow(new SocketTimeoutException());
+
+ conn.bind(socket);
+ conn.ensureOpen();
+
+ Assert.assertFalse(conn.isStale());
+ }
+
+ @Test
+ public void testStaleWhenIOError() throws Exception {
+ final InputStream instream = Mockito.mock(InputStream.class);
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+ Mockito.when(instream.read(Mockito.<byte []>any(), Mockito.anyInt(), Mockito.anyInt()))
+ .thenThrow(new SocketException());
+
+ conn.bind(socket);
+ conn.ensureOpen();
+
+ Assert.assertTrue(conn.isStale());
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestBHttpConnectionBase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java?rev=1458268&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java Tue Mar 19 13:13:01 2013
@@ -0,0 +1,168 @@
+/*
+ * ====================================================================
+ * 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;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.net.Socket;
+
+import junit.framework.Assert;
+
+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.config.MessageConstraints;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.entity.LaxContentLengthStrategy;
+import org.apache.http.impl.entity.StrictContentLengthStrategy;
+import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
+import org.apache.http.impl.io.DefaultHttpResponseParserFactory;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.message.BasicHttpRequest;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class TestDefaultBHttpClientConnection {
+
+ @Mock
+ private Socket socket;
+
+ private DefaultBHttpClientConnection conn;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ conn = new DefaultBHttpClientConnection(1024, 1024,
+ null, null,
+ MessageConstraints.DEFAULT,
+ LaxContentLengthStrategy.INSTANCE,
+ StrictContentLengthStrategy.INSTANCE,
+ DefaultHttpRequestWriterFactory.INSTANCE,
+ DefaultHttpResponseParserFactory.INSTANCE);
+ }
+
+ @Test
+ public void testBasics() throws Exception {
+ Assert.assertFalse(conn.isOpen());
+ Assert.assertEquals("[Not bound]", conn.toString());
+ }
+
+ @Test
+ public void testReadRequestHead() throws Exception {
+ final String s = "HTTP/1.1 200 OK\r\nUser-Agent: test\r\n\r\n";
+ final ByteArrayInputStream instream = new ByteArrayInputStream(s.getBytes("ASCII"));
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+ final HttpResponse response = conn.receiveResponseHeader();
+ Assert.assertNotNull(response);
+ Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
+ Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+ Assert.assertTrue(response.containsHeader("User-Agent"));
+ Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+ }
+
+ @Test
+ public void testReadRequestEntity() throws Exception {
+ final String s = "HTTP/1.1 200 OK\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123";
+ final ByteArrayInputStream instream = new ByteArrayInputStream(s.getBytes("ASCII"));
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+ final HttpResponse response = conn.receiveResponseHeader();
+
+ Assert.assertNotNull(response);
+ Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
+ Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+ Assert.assertTrue(response.containsHeader("User-Agent"));
+ Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+
+ conn.receiveResponseEntity(response);
+
+ final HttpEntity entity = response.getEntity();
+ Assert.assertNotNull(entity);
+ Assert.assertEquals(3, entity.getContentLength());
+ Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+ }
+
+ @Test
+ public void testWriteResponseHead() throws Exception {
+ final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
+ Mockito.when(socket.getOutputStream()).thenReturn(outstream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getRequestCount());
+
+ final HttpRequest request = new BasicHttpRequest("GET", "/stuff", HttpVersion.HTTP_1_1);
+ request.addHeader("User-Agent", "test");
+
+ conn.sendRequestHeader(request);
+ conn.flush();
+
+ Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+ final String s = new String(outstream.toByteArray(), "ASCII");
+ Assert.assertEquals("GET /stuff HTTP/1.1\r\nUser-Agent: test\r\n\r\n", s);
+ }
+
+ @Test
+ public void testWriteResponseEntity() throws Exception {
+ final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
+ Mockito.when(socket.getOutputStream()).thenReturn(outstream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getRequestCount());
+
+ final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
+ "/stuff", HttpVersion.HTTP_1_1);
+ request.addHeader("User-Agent", "test");
+ request.addHeader("Content-Length", "3");
+ request.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
+
+ conn.sendRequestHeader(request);
+ conn.sendRequestEntity(request);
+ conn.flush();
+
+ Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+ final String s = new String(outstream.toByteArray(), "ASCII");
+ Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123", s);
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpClientConnection.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java?rev=1458268&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java Tue Mar 19 13:13:01 2013
@@ -0,0 +1,188 @@
+/*
+ * ====================================================================
+ * 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;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.net.Socket;
+
+import junit.framework.Assert;
+
+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.config.MessageConstraints;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.entity.LaxContentLengthStrategy;
+import org.apache.http.impl.entity.StrictContentLengthStrategy;
+import org.apache.http.impl.io.DefaultHttpRequestParserFactory;
+import org.apache.http.impl.io.DefaultHttpResponseWriterFactory;
+import org.apache.http.message.BasicHttpResponse;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class TestDefaultBHttpServerConnection {
+
+ @Mock
+ private Socket socket;
+
+ private DefaultBHttpServerConnection conn;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ conn = new DefaultBHttpServerConnection(1024, 1024,
+ null, null,
+ MessageConstraints.DEFAULT,
+ LaxContentLengthStrategy.INSTANCE,
+ StrictContentLengthStrategy.INSTANCE,
+ DefaultHttpRequestParserFactory.INSTANCE,
+ DefaultHttpResponseWriterFactory.INSTANCE);
+ }
+
+ @Test
+ public void testBasics() throws Exception {
+ Assert.assertFalse(conn.isOpen());
+ Assert.assertEquals("[Not bound]", conn.toString());
+ }
+
+ @Test
+ public void testReadRequestHead() throws Exception {
+ final String s = "GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n";
+ final ByteArrayInputStream instream = new ByteArrayInputStream(s.getBytes("ASCII"));
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getRequestCount());
+
+ final HttpRequest request = conn.receiveRequestHeader();
+ Assert.assertNotNull(request);
+ Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
+ Assert.assertEquals("/", request.getRequestLine().getUri());
+ Assert.assertEquals("GET", request.getRequestLine().getMethod());
+ Assert.assertTrue(request.containsHeader("User-Agent"));
+ Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+ }
+
+ @Test
+ public void testReadRequestEntity() throws Exception {
+ final String s = "POST / HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123";
+ final ByteArrayInputStream instream = new ByteArrayInputStream(s.getBytes("ASCII"));
+ Mockito.when(socket.getInputStream()).thenReturn(instream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getRequestCount());
+
+ final HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) conn.receiveRequestHeader();
+
+ Assert.assertNotNull(request);
+ Assert.assertEquals(HttpVersion.HTTP_1_1, request.getProtocolVersion());
+ Assert.assertEquals("/", request.getRequestLine().getUri());
+ Assert.assertEquals("POST", request.getRequestLine().getMethod());
+ Assert.assertTrue(request.containsHeader("User-Agent"));
+ Assert.assertNull(request.getEntity());
+ Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+
+ conn.receiveRequestEntity(request);
+
+ final HttpEntity entity = request.getEntity();
+ Assert.assertNotNull(entity);
+ Assert.assertEquals(3, entity.getContentLength());
+ Assert.assertEquals(1, conn.getMetrics().getRequestCount());
+ }
+
+ @Test
+ public void testWriteResponseHead() throws Exception {
+ final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
+ Mockito.when(socket.getOutputStream()).thenReturn(outstream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ response.addHeader("User-Agent", "test");
+
+ conn.sendResponseHeader(response);
+ conn.flush();
+
+ Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+ final String s = new String(outstream.toByteArray(), "ASCII");
+ Assert.assertEquals("HTTP/1.1 200 OK\r\nUser-Agent: test\r\n\r\n", s);
+ }
+
+ @Test
+ public void testWriteResponse100Head() throws Exception {
+ final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
+ Mockito.when(socket.getOutputStream()).thenReturn(outstream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Go on");
+
+ conn.sendResponseHeader(response);
+ conn.flush();
+
+ Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+ final String s = new String(outstream.toByteArray(), "ASCII");
+ Assert.assertEquals("HTTP/1.1 100 Go on\r\n\r\n", s);
+ }
+
+ @Test
+ public void testWriteResponseEntity() throws Exception {
+ final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
+ Mockito.when(socket.getOutputStream()).thenReturn(outstream);
+
+ conn.bind(socket);
+
+ Assert.assertEquals(0, conn.getMetrics().getResponseCount());
+
+ final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+ response.addHeader("User-Agent", "test");
+ response.addHeader("Content-Length", "3");
+ response.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
+
+ conn.sendResponseHeader(response);
+ conn.sendResponseEntity(response);
+ conn.flush();
+
+ Assert.assertEquals(1, conn.getMetrics().getResponseCount());
+ final String s = new String(outstream.toByteArray(), "ASCII");
+ Assert.assertEquals("HTTP/1.1 200 OK\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123", s);
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestDefaultBHttpServerConnection.java
------------------------------------------------------------------------------
svn:mime-type = text/plain