@Test
    void testCookieMatchingWithVirtualHosts() throws Exception {
        server = ServerBootstrap.bootstrap()
                .register("app.mydomain.fr", "*", (request, response, context) -> {

                    final int n = Integer.parseInt(request.getFirstHeader("X-Request").getValue());
                    switch (n) {
                        case 1:
                            // Assert Host is forwarded from URI
                            Assertions.assertEquals("app.mydomain.fr", request
                                    .getFirstHeader("Host").getValue());

                            response.setCode(HttpStatus.SC_OK);
                            // Respond with Set-Cookie on virtual host domain. This
                            // should be valid.
                            response.addHeader(new BasicHeader("Set-Cookie",
                                    "name1=value1; domain=mydomain.fr; path=/"));
                            break;

                        case 2:
                            // Assert Host is still forwarded from URI
                            Assertions.assertEquals("app.mydomain.fr", request
                                    .getFirstHeader("Host").getValue());

                            // We should get our cookie back.
                            Assertions.assertNotNull(request.getFirstHeader("Cookie"), "We must get a cookie header");
                            response.setCode(HttpStatus.SC_OK);
                            break;

                        case 3:
                            // Assert Host is forwarded from URI
                            Assertions.assertEquals("app.mydomain.fr", request
                                    .getFirstHeader("Host").getValue());

                            response.setCode(HttpStatus.SC_OK);
                            break;
                        default:
                            Assertions.fail("Unexpected value: " + n);
                            break;
                    }
                })
                .create();
        server.start();

        final HttpHost target = new HttpHost("localhost", server.getLocalPort());
        try (final CloseableHttpClient client = HttpClientBuilder.create().build()) {
            final CookieStore cookieStore = new BasicCookieStore();
            final HttpClientContext context = HttpClientContext.create();
            context.setCookieStore(cookieStore);

            // First request : retrieve a domain cookie from remote server.
            final HttpGet request1 = new HttpGet(new URI("http://app.mydomain.fr"));
            request1.addHeader("X-Request", "1");
            client.execute(target, request1, context, response -> {
                Assertions.assertEquals(200, response.getCode());
                EntityUtils.consume(response.getEntity());
                return null;
            });

            // We should have one cookie set on domain.
            final List<Cookie> cookies = cookieStore.getCookies();
            Assertions.assertNotNull(cookies);
            Assertions.assertEquals(1, cookies.size());
            Assertions.assertEquals("name1", cookies.get(0).getName());

            // Second request : send the cookie back.
            final HttpGet request2 = new HttpGet(new URI("http://app.mydomain.fr"));
            request2.addHeader("X-Request", "2");
            client.execute(target, request2, context, response -> {
                Assertions.assertEquals(200, response.getCode());
                EntityUtils.consume(response.getEntity());
                return null;
            });

            // Third request : Host header
            final HttpGet request3 = new HttpGet(new URI("http://app.mydomain.fr"));
            request3.addHeader("X-Request", "3");
            client.execute(target, request3, context, response -> {
                Assertions.assertEquals(200, response.getCode());
                EntityUtils.consume(response.getEntity());
                return null;
            });
        }
    }
This class tests cookie matching when using Virtual Host.
httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestCookieVirtualHost.java