@Override public boolean isDataAvailable(final int timeout) throws IOException { ensureOpen(); try { return awaitInput(timeout); } catch (final SocketTimeoutException ex) { return false; } }
@Override public void bind(final Socket socket) throws IOException { super.bind(socket); }
@Override public boolean isStale() throws IOException { if (!isOpen()) { return true; } try { final int bytesRead = fillInputBuffer(1); return bytesRead < 0; } catch (final SocketTimeoutException ex) { return false; } catch (final SocketException ex) { return true; } }
@Test public void testAwaitInputNoData() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) .thenReturn(-1); conn.bind(socket); conn.ensureOpen(); Assert.assertFalse(conn.awaitInput(432)); }
@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 testBasics() throws Exception { Assert.assertFalse(conn.isOpen()); Assert.assertEquals(null, conn.getLocalAddress()); Assert.assertEquals(null, conn.getRemoteAddress()); Assert.assertEquals("[Not bound]", conn.toString()); }
@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.outbuffer.write(0, outStream); Assert.assertTrue(conn.isOpen()); conn.close(CloseMode.GRACEFUL); Assert.assertFalse(conn.isOpen()); Mockito.verify(outStream, Mockito.never()).write( ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.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.close(CloseMode.GRACEFUL); Mockito.verify(socket, Mockito.times(1)).close(); }
@Test public void testStaleWhenClosed() throws Exception { final OutputStream outStream = Mockito.mock(OutputStream.class); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); conn.ensureOpen(); conn.close(); Assert.assertTrue(conn.isStale()); }
@Test public void testGetSocketTimeoutException() throws Exception { Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout()); Mockito.when(socket.getSoTimeout()).thenThrow(new SocketException()); conn.bind(socket); Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout()); }
@Test public void testSetSocketTimeout() throws Exception { conn.bind(socket); conn.setSocketTimeout(Timeout.ofMilliseconds(123)); Mockito.verify(socket, Mockito.times(1)).setSoTimeout(123); }
private int fillInputBuffer(final int timeout) throws IOException { final SocketHolder socketHolder = ensureOpen(); final Socket socket = socketHolder.getSocket(); final int oldtimeout = socket.getSoTimeout(); try { socket.setSoTimeout(timeout); return this.inBuffer.fillBuffer(socketHolder.getInputStream()); } finally { socket.setSoTimeout(oldtimeout); } }
@Test public void testCreateEntityInputUndefined() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final ClassicHttpResponse message = new BasicClassicHttpResponse(200, "OK"); final HttpEntity entity = conn.createIncomingEntity(message, conn.inBuffer, inStream, ContentLengthStrategy.UNDEFINED); Assert.assertNotNull(entity); Assert.assertFalse(entity.isChunked()); Assert.assertEquals(-1, entity.getContentLength()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue((content instanceof IdentityInputStream)); }
protected boolean awaitInput(final int timeout) throws IOException { if (this.inBuffer.hasBufferedData()) { return true; } fillInputBuffer(timeout); return this.inBuffer.hasBufferedData(); }
HttpEntity createIncomingEntity( final HttpMessage message, final SessionInputBuffer inBuffer, final InputStream inputStream, final long len) { return new IncomingHttpEntity( createContentInputStream(len, inBuffer, inputStream), len >= 0 ? len : -1, len == ContentLengthStrategy.CHUNKED, message.getFirstHeader(HttpHeaders.CONTENT_TYPE), message.getFirstHeader(HttpHeaders.CONTENT_ENCODING)); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); conn = new BHttpConnectionBase(H1Config.DEFAULT, null, null); }
@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( ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); }
@Test public void testStaleWhenEndOfStream() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); Mockito.when(socket.getInputStream()).thenReturn(inStream); Mockito.when(inStream.read(ArgumentMatchers.<byte []>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) .thenReturn(-1); conn.bind(socket); conn.ensureOpen(); Assert.assertTrue(conn.isStale()); }
@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(new InetSocketAddress( InetAddress.getByAddress(new byte[] {127, 0, 0, 1}), 8888), conn.getLocalAddress()); Assert.assertEquals(new InetSocketAddress( InetAddress.getByAddress(new byte[] {10, 0, 0, 2}), 80), 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.outbuffer.write(0, outStream); Assert.assertTrue(conn.isOpen()); conn.close(); Assert.assertFalse(conn.isOpen()); Mockito.verify(outStream, Mockito.times(1)).write( ArgumentMatchers.<byte[]>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.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( ArgumentMatchers.<byte[]>any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt()); }
@Test public void testGetSocketTimeout() throws Exception { Assert.assertEquals(Timeout.DISABLED, conn.getSocketTimeout()); Mockito.when(socket.getSoTimeout()).thenReturn(345); conn.bind(socket); Assert.assertEquals(Timeout.ofMilliseconds(345), conn.getSocketTimeout()); }