Middlewares { public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> clientValidator( Supplier<Set<String>> supplier) { return innerHandler -> requestContext -> { if (requestContext.request().header("User-Agent") .map(header -> supplier.get().contains(header)) .orElse(false)) { return completedFuture(Response.forStatus(Status.NOT_ACCEPTABLE.withReasonPhrase( "blacklisted client version, please upgrade"))); } else { return innerHandler.invoke(requestContext); } }; } private  Middlewares(); static Middleware<SyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> json(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> clientValidator(
      Supplier<Set<String>> supplier); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> exceptionAndRequestIdHandler(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer(
      Tracer tracer, String service); static Middleware<Requested<Authenticated<Response<T>>>, SyncHandler<Response<T>>> authed(
      RequestAuthenticator authenticator); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware,
      Middleware<Requested<Authenticated<Response<ByteString>>>, SyncHandler<Response<ByteString>>> authed); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      RequestAuthenticator authenticator,
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> authenticator(
      RequestAuthenticator authenticator);  }
@Test public void testValidClientNoBlacklist() throws Exception { final Supplier<Set<String>> supplier = Set::of; RequestContext requestContext = mockRequestContext(true); Response<Object> response = awaitResponse(Middlewares.clientValidator(supplier) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.OK))); }
@Test public void testValidClientEmptyBlacklist() throws Exception { final Supplier<Set<String>> supplier = Set::of; RequestContext requestContext = mockRequestContext(true); Response<Object> response = awaitResponse(Middlewares.clientValidator(supplier) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.OK))); }
@Test public void testValidClient() throws Exception { final Supplier<Set<String>> supplier = () -> Set.of("Styx CLI 0.1.0"); RequestContext requestContext = mockRequestContext(true); Response<Object> response = awaitResponse(Middlewares.clientValidator(supplier) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.OK))); }
@Test public void testValidClientNoHeader() throws Exception { final Supplier<Set<String>> supplier = () -> Set.of("Styx CLI 0.1.0"); RequestContext requestContext = mockRequestContext(false); Response<Object> response = awaitResponse(Middlewares.clientValidator(supplier) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.OK))); }
@Test public void testInvalidClient() throws Exception { final Supplier<Set<String>> supplier = () -> Set.of("Styx CLI 0.1.1"); RequestContext requestContext = mockRequestContext(true); Response<Object> response = awaitResponse(Middlewares.clientValidator(supplier) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(belongsToFamily(Status.Family.CLIENT_ERROR))); }