@Test
    void testConnectionCloseAfterAuthenticationSuccess() throws Exception {
        configureServer(bootstrap -> bootstrap
                .setExchangeHandlerDecorator(requestHandler ->
                        new AuthenticatingDecorator(requestHandler, new BasicTestAuthenticator("test:test", "test realm")) {

                            @Override
                            protected void customizeUnauthorizedResponse(final ClassicHttpResponse unauthorized) {
                                unauthorized.addHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
                            }

                        }
                )
                .register("*", new EchoHandler()));

        final HttpHost target = startServer();

        final TestClient client = client();

        final HttpClientContext context = HttpClientContext.create();
        final CredentialsProvider credsProvider = CredentialsProviderBuilder.create()
                .add(target, "test", "test".toCharArray())
                .build();
        context.setCredentialsProvider(credsProvider);

        for (int i = 0; i < 2; i++) {
            final HttpGet httpget = new HttpGet("/");

            client.execute(target, httpget, context, response -> {
                EntityUtils.consume(response.getEntity());
                Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
                return null;
            });
        }
    }
Unit tests for automatic client authentication.
httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java