### Question:
BindClientContextHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if(msg instanceof FullHttpRequest) { logger.trace("Received HTTP request, determining context"); try { String sessionId = extractSessionId((FullHttpRequest) msg); logger.debug("Extracted Session Id in BindClientContext: {}", sessionId); Client.bind(ctx.channel(), registry.load(sessionId)); BridgeMdcUtil.bindHttpContext(registry, ctx.channel(), (FullHttpRequest) msg); } catch(Exception ex) { logger.warn("Exception while retrieving client session", ex); requestAuthorizer.handleFailedAuth(ctx, (FullHttpRequest) msg); return; } } super.channelRead(ctx, msg); } @Inject  BindClientContextHandler(CookieConfig cookieConfig, ClientFactory registry, @Named("SessionAuthorizer") RequestAuthorizer requestAuthorizer); @Override boolean isSharable(); @Override void channelRead(ChannelHandlerContext ctx, Object msg);  }### Answer:
@Test public void testBindNoHeaders() throws Exception { replay(); DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: handler.channelRead(context, request); ClientFactory factory = ServiceLocator.getInstance(ClientFactory.class); Client client = factory.get(channel); assertNotNull(client); assertFalse(client.isAuthenticated()); verify(); }
@Test public void testBindByCookie() throws Exception { SimpleSession session = new SimpleSession(); session.setId("test"); session.setExpired(false); session.setAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY, true); EasyMock .expect(sessionDao.readSession("test")) .andReturn(session) .anyTimes(); sessionDao.update(session); EasyMock .expectLastCall() .times(1); replay(); DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: DefaultHttpHeaders.addHeader(request, "Cookie", "irisAuthToken=test;"); handler.channelRead(context, request); ClientFactory factory = ServiceLocator.getInstance(ClientFactory.class); Client client = factory.get(channel); assertNotNull(client); assertTrue(client.isAuthenticated()); assertEquals("test", client.getSessionId()); verify(); }
@Test public void testBindByAuthHeader() throws Exception { SimpleSession session = new SimpleSession(); session.setId("test"); session.setExpired(false); session.setAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY, true); EasyMock .expect(sessionDao.readSession("test")) .andReturn(session) .anyTimes(); sessionDao.update(session); EasyMock .expectLastCall() .times(1); replay(); DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: DefaultHttpHeaders.addHeader(request, "Authorization", "test"); handler.channelRead(context, request); ClientFactory factory = ServiceLocator.getInstance(ClientFactory.class); Client client = factory.get(channel); assertNotNull(client); assertTrue(client.isAuthenticated()); assertEquals("test", client.getSessionId()); verify(); }
@Test public void testBindUnknownSession() throws Exception { EasyMock .expect(sessionDao.readSession("test")) .andThrow(new UnknownSessionException()); replay(); DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: DefaultHttpHeaders.addHeader(request, "Cookie", "irisAuthToken=test;"); handler.channelRead(context, request); ClientFactory factory = ServiceLocator.getInstance(ClientFactory.class); Client client = factory.get(channel); assertNotNull(client); assertFalse(client.isAuthenticated()); assertEquals(null, client.getSessionId()); verify(); }