@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 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()); }