@Test
  public void decodeUtf16EncodedResponse() {
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatus(200);
    httpResponse.setHeader(CONTENT_TYPE, JSON_UTF_8.withCharset(UTF_16).toString());
    httpResponse.setContent("{\"status\":0,\"value\":\"水\"}".getBytes(UTF_16));

    Response response = codec.decode(httpResponse);
    assertEquals("水", response.getValue());
  }
  @Test
  public void encodingAPostWithNoParameters() {
    codec.defineCommand("foo", POST, "/foo/bar");
    Command command = new Command(null, "foo");

    HttpRequest request = codec.encode(command);
    assertThat(request.getMethod(), is(POST));
    assertThat(request.getHeader(CONTENT_TYPE), is(JSON_UTF_8.toString()));
    assertThat(request.getHeader(CONTENT_LENGTH), is("2"));
    assertThat(request.getUri(), is("/foo/bar"));
    assertThat(new String(request.getContent(), UTF_8), is("{}"));
  }
  @Test
  public void decodingUsesUtf8IfNoEncodingSpecified() {
    codec.defineCommand("num", POST, "/one");

    byte[] data = "{\"char\":\"水\"}".getBytes(UTF_8);
    HttpRequest request = new HttpRequest(POST, "/one");
    request.setHeader(CONTENT_TYPE, JSON_UTF_8.withoutParameters().toString());
    request.setHeader(CONTENT_LENGTH, String.valueOf(data.length));
    request.setContent(data);

    Command command = codec.decode(request);
    assertThat((String) command.getParameters().get("char"), is("水"));
  }
  @Test
  public void encodingAPostWithUrlParameters() {
    codec.defineCommand("foo", POST, "/foo/:bar/baz");
    Command command = new Command(null, "foo", ImmutableMap.of("bar", "apples123"));

    String encoding = "{\"bar\":\"apples123\"}";

    HttpRequest request = codec.encode(command);
    assertThat(request.getMethod(), is(POST));
    assertThat(request.getHeader(CONTENT_TYPE), is(JSON_UTF_8.toString()));
    assertThat(request.getHeader(CONTENT_LENGTH), is(String.valueOf(encoding.length())));
    assertThat(request.getUri(), is("/foo/apples123/baz"));
    assertThat(new String(request.getContent(), UTF_8), is(encoding));
  }
  @Test
  public void convertsResponses_failure() throws JSONException {
    Response response = new Response();
    response.setStatus(ErrorCodes.NO_SUCH_ELEMENT);
    response.setValue(ImmutableMap.of("color", "red"));

    HttpResponse converted = codec.encode(response);
    assertThat(converted.getStatus(), is(HTTP_INTERNAL_ERROR));
    assertThat(converted.getHeader(CONTENT_TYPE), is(JSON_UTF_8.toString()));

    Response rebuilt =
        new JsonToBeanConverter()
            .convert(Response.class, new String(converted.getContent(), UTF_8));

    assertEquals(response.getStatus(), rebuilt.getStatus());
    assertEquals(response.getState(), rebuilt.getState());
    assertEquals(response.getSessionId(), rebuilt.getSessionId());
    assertEquals(response.getValue(), rebuilt.getValue());
  }