OptionalSslHandler extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < SslUtils.SSL_RECORD_HEADER_LENGTH) { return; } if (SslHandler.isEncrypted(in)) { handleSsl(context); } else { handleNonSsl(context); } } OptionalSslHandler(SslContext sslContext);   }
@Test public void handlerRemoved() throws Exception { OptionalSslHandler handler = new OptionalSslHandler(sslContext); final ByteBuf payload = Unpooled.copiedBuffer("plaintext".getBytes()); try { handler.decode(context, payload, null); verify(pipeline).remove(handler); } finally { payload.release(); } }
@Test public void handlerReplaced() throws Exception { final ChannelHandler nonSslHandler = Mockito.mock(ChannelHandler.class); OptionalSslHandler handler = new OptionalSslHandler(sslContext) { @Override protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) { return nonSslHandler; } @Override protected String newNonSslHandlerName() { return HANDLER_NAME; } }; final ByteBuf payload = Unpooled.copiedBuffer("plaintext".getBytes()); try { handler.decode(context, payload, null); verify(pipeline).replace(handler, HANDLER_NAME, nonSslHandler); } finally { payload.release(); } }
@Test public void sslHandlerReplaced() throws Exception { final SslHandler sslHandler = Mockito.mock(SslHandler.class); OptionalSslHandler handler = new OptionalSslHandler(sslContext) { @Override protected SslHandler newSslHandler(ChannelHandlerContext context, SslContext sslContext) { return sslHandler; } @Override protected String newSslHandlerName() { return SSL_HANDLER_NAME; } }; final ByteBuf payload = Unpooled.wrappedBuffer(new byte[] { 22, 3, 1, 0, 5 }); try { handler.decode(context, payload, null); verify(pipeline).replace(handler, SSL_HANDLER_NAME, sslHandler); } finally { payload.release(); } }
@Test public void decodeBuffered() throws Exception { OptionalSslHandler handler = new OptionalSslHandler(sslContext); final ByteBuf payload = Unpooled.wrappedBuffer(new byte[] { 22, 3 }); try { handler.decode(context, payload, null); verifyZeroInteractions(pipeline); } finally { payload.release(); } }