HttpCommandDecoder { public static CommandContext decode(HttpRequest request) { CommandContext commandContext = null; if (request != null) { QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); String path = queryStringDecoder.path(); String[] array = path.split("/"); if (array.length == 2) { String name = array[1]; if (request.getMethod() == HttpMethod.GET) { if (queryStringDecoder.parameters().isEmpty()) { commandContext = CommandContextFactory.newInstance(name); commandContext.setHttp(true); } else { List<String> valueList = new ArrayList<String>(); for (List<String> values : queryStringDecoder.parameters().values()) { valueList.addAll(values); } commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[]{}),true); } } else if (request.getMethod() == HttpMethod.POST) { HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request); List<String> valueList = new ArrayList<String>(); for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) { if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) { Attribute attribute = (Attribute) interfaceHttpData; try { valueList.add(attribute.getValue()); } catch (IOException ex) { throw new RuntimeException(ex); } } } if (valueList.isEmpty()) { commandContext = CommandContextFactory.newInstance(name); commandContext.setHttp(true); } else { commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[]{}),true); } } } } return commandContext; }  static CommandContext decode(HttpRequest request);  }
@Test public void decodeGet() throws Exception { HttpRequest request = mock(HttpRequest.class); when(request.getUri()).thenReturn("localhost:80/test"); when(request.getMethod()).thenReturn(HttpMethod.GET); CommandContext context = HttpCommandDecoder.decode(request); assertThat(context.getCommandName(), equalTo("test")); assertThat(context.isHttp(), is(true)); when(request.getUri()).thenReturn("localhost:80/test?a=b&c=d"); context = HttpCommandDecoder.decode(request); assertThat(context.getArgs(), arrayContaining("b", "d")); }
@Test public void decodePost() throws Exception { FullHttpRequest request = mock(FullHttpRequest.class); when(request.getUri()).thenReturn("localhost:80/test"); when(request.getMethod()).thenReturn(HttpMethod.POST); when(request.headers()).thenReturn(HttpHeaders.EMPTY_HEADERS); ByteBuf buf = Unpooled.copiedBuffer("a=b&c=d", StandardCharsets.UTF_8); when(request.content()).thenReturn(buf); CommandContext context = HttpCommandDecoder.decode(request); assertThat(context.getCommandName(), equalTo("test")); assertThat(context.isHttp(), is(true)); assertThat(context.getArgs(), arrayContaining("b", "d")); }