Middlewares { public static AsyncHandler<Response<ByteString>> httpPayloadSemantics( AsyncHandler<Response<ByteString>> inner) { return inner.flatMapSync(resp -> ctx -> applyHttpPayloadSemantics(ctx.request(), resp)); } private  Middlewares(); static AsyncHandler<Response<ByteString>> autoSerialize(AsyncHandler<T> inner); static AsyncHandler<Response<ByteString>> httpPayloadSemantics(
      AsyncHandler<Response<ByteString>> inner); static Middleware<AsyncHandler<?>, AsyncHandler<Response<?>>> replyContentType(
      String contentType); static Middleware<AsyncHandler<?>, AsyncHandler<Response<ByteString>>> serialize(
      Serializer serializer); static Middleware<AsyncHandler<?>, AsyncHandler<Response<ByteString>>> apolloDefaults();  }
@Test public void httpShouldSetContentLengthFor200() throws Exception { future.complete(Response.of(Status.OK, ByteString.of((byte) 14, (byte) 19))); String header = getResult(Middlewares.httpPayloadSemantics(delegate)).header( "Content-Length").get(); assertThat(Integer.parseInt(header), equalTo(2)); }
@Test public void httpShouldAppendPayloadFor200() throws Exception { ByteString payload = ByteString.of((byte) 14, (byte) 19); future.complete(Response.of(Status.OK, payload)); assertThat(getResult(Middlewares.httpPayloadSemantics(delegate)).payload(), equalTo(Optional.of(payload))); }
@Test public void httpShouldNotSetContentLengthOrAppendPayloadForInvalidStatusCodes() throws Exception { List<StatusType> invalid = ImmutableList.of(Status.createForCode(100), NO_CONTENT, NOT_MODIFIED); for (StatusType status : invalid) { CompletableFuture<Response<ByteString>> future = new CompletableFuture<>(); delegate = requestContext -> future; future.complete(Response.of(status, ByteString.of((byte) 14, (byte) 19))); Response<ByteString> result = getResult(Middlewares.httpPayloadSemantics(delegate)); Optional<String> header = result.header("Content-Length"); assertThat("no content-length for " + status, header, is(Optional.empty())); assertThat("no payload for " + status, result.payload(), is(Optional.<ByteString>empty())); } }
@Test public void httpShouldSetContentLengthForHeadAnd200() throws Exception { when(request.method()).thenReturn("HEAD"); future.complete(Response.of(Status.OK, ByteString.of((byte) 14, (byte) 19))); String header = getResult(Middlewares.httpPayloadSemantics(delegate)).header( "Content-Length").get(); assertThat(Integer.parseInt(header), equalTo(2)); }
@Test public void httpShouldNotAppendPayloadForHeadAnd200() throws Exception { when(request.method()).thenReturn("HEAD"); future.complete(Response.of(Status.OK, ByteString.of((byte) 14, (byte) 19))); assertThat(getResult(Middlewares.httpPayloadSemantics(delegate)).payload(), is(Optional.<ByteString>empty())); }