@Test public void testStaleWhenClosed() throws Exception { conn.bind(socket); conn.ensureOpen(); conn.close(); Assert.assertTrue(conn.isStale()); }
@Test public void testSetSocketTimeout() throws Exception { conn.bind(socket); conn.setSocketTimeout(123); Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123); }
@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 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 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 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 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()); }
@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 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 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 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 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 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 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 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)); }
public void bind(Socket socket) throws IOException { super.bind(socket); }