HttpFileRequestHandler extends SimpleChannelInboundHandler<HttpFileRequest> { @Override public void exceptionCaught(final ChannelHandlerContext context, final Throwable cause) { if (context.channel().isActive()) { HttpResponseStatus status = INTERNAL_SERVER_ERROR; if (cause instanceof HttpFileRequestException) { status = ((HttpFileRequestException) cause).getStatus(); } sendError(context, cause.getMessage(), status); } } @Inject  HttpFileRequestHandler(final ConfigurationManager configurationManager); @Override void exceptionCaught(final ChannelHandlerContext context, final Throwable cause);  }
@Test public void testExceptionCaughtHttpRequestException() throws Exception { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); Channel channel = createMock(Channel.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(context.channel()).andReturn(channel).atLeastOnce(); expect(channel.isActive()).andReturn(true).atLeastOnce(); expect(channel.writeAndFlush(isA(Object.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); replay(context, channel, configurationManager); new HttpFileRequestHandler(configurationManager).exceptionCaught(context, new HttpFileRequestException("message", NOT_FOUND)); verify(context, channel, configurationManager); }
@Test public void testExceptionCaughtIoException() throws Exception { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); Channel channel = createMock(Channel.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(context.channel()).andReturn(channel).atLeastOnce(); expect(channel.isActive()).andReturn(true).atLeastOnce(); expect(channel.writeAndFlush(isA(Object.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce(); replay(context, channel, configurationManager); new HttpFileRequestHandler(configurationManager).exceptionCaught(context, new IOException()); verify(context, channel, configurationManager); }
@Test public void testExceptionCaughtHttpRequestChannelInactive() throws Exception { ConfigurationManager configurationManager = createMock(ConfigurationManager.class); ChannelHandlerContext context = createMock(ChannelHandlerContext.class); Channel channel = createMock(Channel.class); expect(configurationManager.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60); expect(context.channel()).andReturn(channel).atLeastOnce(); expect(channel.isActive()).andReturn(false).atLeastOnce(); replay(context, channel, configurationManager); new HttpFileRequestHandler(configurationManager).exceptionCaught(context, new HttpFileRequestException("message", NOT_FOUND)); verify(context, channel, configurationManager); }