HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> { @Override protected void channelRead0(final ChannelHandlerContext context, final FullHttpRequest request) { String requestPath = request.uri(); String requestBody = request.content().toString(CharsetUtil.UTF_8); HttpMethod method = request.method(); if (!URL_PATTERN.matcher(requestPath).matches()) { response(GSON.toJson(ResponseContentUtil.handleBadRequest("Not support request!")), context, HttpResponseStatus.BAD_REQUEST); return; } if ("/scaling/job/start".equalsIgnoreCase(requestPath) && method.equals(HttpMethod.POST)) { startJob(context, requestBody); return; } if (requestPath.contains("/scaling/job/progress/") && method.equals(HttpMethod.GET)) { getJobProgress(context, requestPath); return; } if ("/scaling/job/list".equalsIgnoreCase(requestPath) && method.equals(HttpMethod.GET)) { listAllJobs(context); return; } if ("/scaling/job/stop".equalsIgnoreCase(requestPath) && method.equals(HttpMethod.POST)) { stopJob(context, requestBody); return; } if (requestPath.contains("/scaling/job/check/") && method.equals(HttpMethod.GET)) { checkJob(context, requestPath); return; } response(GSON.toJson(ResponseContentUtil.handleBadRequest("Not support request!")), context, HttpResponseStatus.BAD_REQUEST); }  @Override void exceptionCaught(final ChannelHandlerContext context, final Throwable cause);  }
@Test public void assertChannelReadProgressFail() { fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/progress/9"); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Can't find scaling job id 9")); }
@Test public void assertChannelReadProgressSuccess() { startScalingJob(); fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/progress/1"); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext, times(2)).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true")); }
@Test public void assertChannelReadStop() { Map<String, Integer> map = new HashMap<>(); map.put("id", 1); ByteBuf byteBuf = Unpooled.copiedBuffer(GSON.toJson(map), CharsetUtil.UTF_8); fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/scaling/job/stop", byteBuf); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true")); }
@Test public void assertChannelReadList() { fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/list"); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true")); }
@Test public void assertChannelReadUnsupportedUrl() { fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/scaling/1"); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Not support request!")); }
@Test public void assertChannelReadUnsupportedMethod() { fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/scaling/job/stop"); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Not support request!")); }