Middlewares { public static Middleware<AsyncHandler<?>, AsyncHandler<Response<ByteString>>> serialize( Serializer serializer) { return inner -> inner .map(Middlewares::ensureResponse) .flatMapSync(resp -> ctx -> serializePayload(serializer, 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 serializerShouldSerialize() throws Exception { Object response = new Object(); ByteString serializedPayload = ByteString.encodeUtf8("hi there"); when(serializer.serialize(any(Request.class), eq(response))) .thenReturn(Payloads.create(serializedPayload)); serializationFuture.complete(response); assertThat(getResult(Middlewares.serialize(serializer).apply(serializationDelegate)).payload(), equalTo(Optional.of(serializedPayload))); }
@Test public void serializerShouldSerializeResponses() throws Exception { Object response = new Object(); ByteString serializedPayload = ByteString.encodeUtf8("hi there"); when(serializer.serialize(any(Request.class), eq(response))) .thenReturn(Payloads.create(serializedPayload)); serializationFuture.complete(Response.forPayload(response)); assertThat(getResult(Middlewares.serialize(serializer).apply(serializationDelegate)).payload(), equalTo(Optional.of(serializedPayload))); }
@Test public void serializerShouldCopyHeadersFromResponses() throws Exception { Object response = new Object(); ByteString serializedPayload = ByteString.encodeUtf8("hi there"); when(serializer.serialize(any(Request.class), eq(response))) .thenReturn(Payloads.create(serializedPayload)); serializationFuture.complete(Response.forPayload(response).withHeader("X-Foo", "Fie")); Optional<String> header = getResult(Middlewares.serialize(serializer).apply(serializationDelegate)) .header("X-Foo"); assertThat(header.get(), equalTo("Fie")); }
@Test public void serializerShouldCopyStatusCodeFromResponses() throws Exception { Object response = new Object(); ByteString serializedPayload = ByteString.encodeUtf8("hi there"); when(serializer.serialize(any(Request.class), eq(response))) .thenReturn(Payloads.create(serializedPayload)); serializationFuture.complete(Response.of(CREATED, response)); assertThat(getResult( Middlewares.serialize(serializer).apply(serializationDelegate)).status(), equalTo(CREATED)); }
@Test public void serializerShouldSetContentTypeIfPresent() throws Exception { Object response = new Object(); ByteString serializedPayload = ByteString.encodeUtf8("hi there"); when(serializer.serialize(any(Request.class), eq(response))) .thenReturn(Payloads.create(serializedPayload, "coool stuff")); serializationFuture.complete(response); String contentType = getResult(Middlewares.serialize(serializer).apply(serializationDelegate)) .header("Content-Type").get(); assertThat(contentType, equalTo("coool stuff")); }
@Test public void serializerShouldNotSetContentTypeIfAbsent() throws Exception { Object response = new Object(); ByteString serializedPayload = ByteString.encodeUtf8("hi there"); when(serializer.serialize(any(Request.class), eq(response))) .thenReturn(Payloads.create(serializedPayload, Optional.<String>empty())); serializationFuture.complete(response); Optional<String> contentType = getResult(Middlewares.serialize(serializer).apply(serializationDelegate)) .header("Content-Type"); assertThat(contentType, is(Optional.empty())); }
@Test public void serializerShouldNotSerializeNull() throws Exception { serializationFuture.complete(Response.forStatus(Status.BAD_REQUEST)); Response<ByteString> response = getResult(Middlewares.serialize(serializer).apply(serializationDelegate)); assertThat(response.status(), equalTo(Status.BAD_REQUEST)); verify(serializer, never()).serialize(any(Request.class), any()); }