@Test
public void testAuthedEntityProvidesIdToken() throws Exception {

  final EntityMiddleware em = EntityMiddleware.forCodec(JacksonEntityCodec.forMapper(Json.OBJECT_MAPPER));
  final RequestContext requestContext = mock(RequestContext.class);
  final Request request = Request.forUri("/", "PUT")
      .withPayload(ByteString.encodeUtf8("\"hello \""))
      .withHeader(HttpHeaders.AUTHORIZATION, "Bearer s3cr3tp455w0rd");
  when(requestContext.request()).thenReturn(request);
  final String email = "foo@bar.net";
  when(authenticator.authenticate(any())).thenReturn(() -> Optional.of(idToken));
  when(idToken.getPayload()).thenReturn(idTokenPayload);
  when(idTokenPayload.getEmail()).thenReturn(email);
  final Response<ByteString> response = Middlewares.authedEntity(authenticator, em.response(String.class))
      .apply(ac -> rc -> payload -> Response.forPayload(payload + ac.user().orElseThrow().getPayload().getEmail()))
      .invoke(requestContext);
  assertThat(Json.deserialize(response.payload().orElseThrow(), String.class), is("hello " + email));
}