@Test
    void testBasicAuthenticationCredentialsCachingByPathPrefix() throws Exception {
        configureServerWithBasicAuth(bootstrap -> bootstrap
                .register("*", new EchoHandler()));
        final HttpHost target = startServer();

        final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy());
        final Queue<HttpResponse> responseQueue = new ConcurrentLinkedQueue<>();

        configureClient(builder -> builder
                .setTargetAuthenticationStrategy(authStrategy)
                .addResponseInterceptorLast((response, entity, context)
                        -> responseQueue.add(BasicResponseBuilder.copy(response).build())));
        final TestClient client = client();

        final CredentialsProvider credentialsProvider = CredentialsProviderBuilder.create()
                .add(target, "test", "test".toCharArray())
                .build();

        final AuthCache authCache = new BasicAuthCache();
        final HttpClientContext context = HttpClientContext.create();
        context.setAuthCache(authCache);
        context.setCredentialsProvider(credentialsProvider);

        for (final String requestPath : new String[]{"/blah/a", "/blah/b?huh", "/blah/c", "/bl%61h/%61"}) {
            final HttpGet httpget = new HttpGet(requestPath);
            client.execute(target, httpget, context, response -> {
                final HttpEntity entity1 = response.getEntity();
                Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
                Assertions.assertNotNull(entity1);
                EntityUtils.consume(entity1);
                return null;
            });
        }

        // There should be only single auth strategy call for all successful message exchanges
        Mockito.verify(authStrategy).select(Mockito.any(), Mockito.any(), Mockito.any());

        assertThat(
                responseQueue.stream().map(HttpResponse::getCode).collect(Collectors.toList()),
                CoreMatchers.equalTo(Arrays.asList(401, 200, 200, 200, 200)));

        responseQueue.clear();
        authCache.clear();
        Mockito.reset(authStrategy);

        for (final String requestPath : new String[]{"/blah/a", "/yada/a", "/blah/blah/", "/buh/a"}) {
            final HttpGet httpget = new HttpGet(requestPath);
            client.execute(target, httpget, context, response -> {
                final HttpEntity entity1 = response.getEntity();
                Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
                Assertions.assertNotNull(entity1);
                EntityUtils.consume(entity1);
                return null;
            });
        }

        // There should be an auth strategy call for all successful message exchanges
        Mockito.verify(authStrategy, Mockito.times(2)).select(Mockito.any(), Mockito.any(), Mockito.any());

        assertThat(
                responseQueue.stream().map(HttpResponse::getCode).collect(Collectors.toList()),
                CoreMatchers.equalTo(Arrays.asList(200, 401, 200, 200, 401, 200)));
    }
Unit tests for automatic client authentication.
httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java