NettyHttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> { @Override protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest httpRequest) throws Exception { if(BAD_REQUEST.equals(httpRequest.getUri())){ sendResponse(ctx, buildDefaultResponse("bad request!", HttpResponseStatus.BAD_REQUEST)); return; } if(ROOT_PATH.equals(httpRequest.getUri()) || STATUS_PATH.equals(httpRequest.getUri())){ if(isSwitchOpen()){ sendResponse(ctx, buildDefaultResponse("ok!", HttpResponseStatus.OK)); }else{ sendResponse(ctx, buildErrorResponse("service not available!")); } return; } httpRequest.content().retain(); if (threadPoolExecutor == null) { processHttpRequest(ctx, httpRequest); } else { try{ threadPoolExecutor.execute(new Runnable() { @Override public void run() { processHttpRequest(ctx, httpRequest); } }); }catch(Exception e){ LoggerUtil.error("request is rejected by threadpool!", e); httpRequest.content().release(); sendResponse(ctx, buildErrorResponse("request is rejected by threadpool!")); } } } NettyHttpRequestHandler(Channel serverChannel);  NettyHttpRequestHandler(Channel serverChannel, MessageHandler messageHandler);  NettyHttpRequestHandler(Channel serverChannel, MessageHandler messageHandler, ThreadPoolExecutor threadPoolExecutor); MessageHandler getMessageHandler(); void setMessageHandler(MessageHandler messageHandler); static final String BAD_REQUEST; static final String ROOT_PATH; static final String STATUS_PATH; }
@Test public void testChannelRead0() throws Exception { final MessageHandler messageHandler = mockery.mock(MessageHandler.class); final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class); final FullHttpResponse response = mockery.mock(FullHttpResponse.class); mockery.checking(new Expectations() { { allowing(ctx).write(with(any(FullHttpResponse.class))); will(new CustomAction("verify") { @Override public Object invoke(Invocation invocation) throws Throwable { FullHttpResponse actualResponse = (FullHttpResponse) invocation.getParameter(0); assertNotNull(actualResponse); assertEquals(response, actualResponse); return null; } }); allowing(ctx).flush(); will(returnValue(null)); allowing(ctx).close(); will(returnValue(null)); atLeast(1).of(messageHandler).handle(with(any(Channel.class)), with(any(Object.class))); will(returnValue(response)); allowing(response).headers(); will(returnValue(new DefaultHttpHeaders())); } }); FullHttpRequest httpRequest = buildHttpRequest("anyPath"); NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler); handler.channelRead0(ctx, httpRequest); }
@Test public void testServerStatus() throws Exception { final MessageHandler messageHandler = mockery.mock(MessageHandler.class); final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class); mockery.checking(new Expectations() { { allowing(ctx).write(with(any(FullHttpResponse.class))); will(new CustomAction("verify") { @Override public Object invoke(Invocation invocation) throws Throwable { verifyStatus((FullHttpResponse) invocation.getParameter(0)); return null; } }); allowing(ctx).flush(); will(returnValue(null)); allowing(ctx).close(); will(returnValue(null)); allowing(messageHandler).handle(with(any(Channel.class)), with(any(Object.class))); will(returnValue(null)); } }); FullHttpRequest httpRequest = buildHttpRequest(NettyHttpRequestHandler.ROOT_PATH); NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler); MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, false); handler.channelRead0(ctx, httpRequest); MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); handler.channelRead0(ctx, httpRequest); }