コード例 #1
0
  @Test
  public void codecRoundTrip() {
    codec.defineCommand("buy", POST, "/:sessionId/fruit/:fruit/size/:size");

    Command original =
        new Command(
            new SessionId("session123"),
            "buy",
            ImmutableMap.of("fruit", "apple", "size", "large", "color", "red", "rotten", "false"));
    HttpRequest request = codec.encode(original);
    Command decoded = codec.decode(request);

    assertThat(decoded.getName(), is(original.getName()));
    assertThat(decoded.getSessionId(), is(original.getSessionId()));
    assertThat(decoded.getParameters(), is((Map) original.getParameters()));
  }
コード例 #2
0
  @Test
  public void canExtractSessionIdFromPathParameters() throws URISyntaxException {
    HttpRequest request = new HttpRequest(GET, "/foo/bar/baz");
    codec.defineCommand("foo", GET, "/foo/:sessionId/baz");

    Command decoded = codec.decode(request);
    assertThat(decoded.getSessionId(), is(new SessionId("bar")));
  }
コード例 #3
0
  @Test
  public void removesSessionIdFromParameterMap() throws URISyntaxException {
    HttpRequest request = new HttpRequest(GET, "/foo/bar/baz");
    codec.defineCommand("foo", GET, "/foo/:sessionId/baz");

    Command decoded = codec.decode(request);
    assertThat(decoded.getSessionId(), is(new SessionId("bar")));
    assertThat(decoded.getParameters().isEmpty(), is(true));
  }
コード例 #4
0
  @Test
  public void canExtractSessionIdFromRequestBody() throws JSONException, URISyntaxException {
    String data = new JSONObject().put("sessionId", "sessionX").toString();
    HttpRequest request = new HttpRequest(POST, "/foo/bar/baz");
    request.setContent(data.getBytes(UTF_8));
    codec.defineCommand("foo", POST, "/foo/bar/baz");

    Command decoded = codec.decode(request);
    assertThat(decoded.getSessionId(), is(new SessionId("sessionX")));
  }
コード例 #5
0
  @Test
  public void canDecodeCommandWithNoParameters() throws URISyntaxException {
    HttpRequest request = new HttpRequest(GET, "/foo/bar/baz");
    codec.defineCommand("foo", GET, "/foo/bar/baz");

    Command decoded = codec.decode(request);
    assertThat(decoded.getName(), is("foo"));
    assertThat(decoded.getSessionId(), is(nullValue()));
    assertThat(decoded.getParameters().isEmpty(), is(true));
  }