massdosage commented on code in PR #589: URL: https://github.com/apache/httpcomponents-client/pull/589#discussion_r1800751636
########## httpclient5-testing/src/test/java/org/apache/hc/client5/testing/compatibility/ContainerImages.java: ########## @@ -0,0 +1,119 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.hc.client5.testing.compatibility; + +import java.nio.charset.StandardCharsets; +import java.util.Random; + +import org.apache.hc.client5.http.utils.ByteArrayBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.images.builder.ImageFromDockerfile; +import org.testcontainers.images.builder.Transferable; + +public final class ContainerImages { + + private static final Logger LOG = LoggerFactory.getLogger(ContainerImages.class); + + public static String WEB_SERVER = "test-httpd"; Review Comment: Should these be `final` since they're UPPER_CASE constants? ########## httpclient5-testing/src/test/java/org/apache/hc/client5/testing/compatibility/async/HttpAsyncClientCompatibilityTest.java: ########## @@ -0,0 +1,174 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.hc.client5.testing.compatibility.async; + +import java.util.concurrent.Future; + +import org.apache.hc.client5.http.ContextBuilder; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; +import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; +import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.Credentials; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; +import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; +import org.apache.hc.client5.http.protocol.HttpClientContext; +import org.apache.hc.client5.testing.extension.async.HttpAsyncClientResource; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.HttpVersion; +import org.apache.hc.core5.http2.HttpVersionPolicy; +import org.apache.hc.core5.util.Timeout; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public abstract class HttpAsyncClientCompatibilityTest { + + static final Timeout TIMEOUT = Timeout.ofSeconds(5); + + private final HttpVersionPolicy versionPolicy; + private final HttpHost target; + @RegisterExtension + private final HttpAsyncClientResource clientResource; + private final BasicCredentialsProvider credentialsProvider; + + public HttpAsyncClientCompatibilityTest( + final HttpVersionPolicy versionPolicy, + final HttpHost target, + final HttpHost proxy, + final Credentials proxyCreds) throws Exception { + this.versionPolicy = versionPolicy; + this.target = target; + this.clientResource = new HttpAsyncClientResource(versionPolicy); + this.clientResource.configure(builder -> builder.setProxy(proxy)); + this.credentialsProvider = new BasicCredentialsProvider(); + if (proxy != null && proxyCreds != null) { + this.credentialsProvider.setCredentials(new AuthScope(proxy), proxyCreds); + } + } + + CloseableHttpAsyncClient client() { + return clientResource.client(); + } + + HttpClientContext context() { + return ContextBuilder.create() + .useCredentialsProvider(credentialsProvider) + .build(); + } + + void addCredentials(final AuthScope authScope, final Credentials credentials) { + credentialsProvider.setCredentials(authScope, credentials); + } + + void assertProtocolVersion(final HttpClientContext context) { + switch (versionPolicy) { + case FORCE_HTTP_1: + Assertions.assertEquals(HttpVersion.HTTP_1_1, context.getProtocolVersion()); + break; + case FORCE_HTTP_2: + case NEGOTIATE: + Assertions.assertEquals(HttpVersion.HTTP_2, context.getProtocolVersion()); + break; Review Comment: Do we want a `default` case that throws an exception in case something unexpected happens? ########## httpclient5-testing/src/test/java/org/apache/hc/client5/testing/compatibility/sync/CachingHttpClientCompatibilityTest.java: ########## @@ -0,0 +1,156 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.hc.client5.testing.compatibility.sync; + +import org.apache.hc.client5.http.cache.CacheResponseStatus; +import org.apache.hc.client5.http.cache.HttpCacheContext; +import org.apache.hc.client5.http.cache.HttpCacheEntry; +import org.apache.hc.client5.http.cache.RequestCacheControl; +import org.apache.hc.client5.http.cache.ResponseCacheControl; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpOptions; +import org.apache.hc.client5.http.impl.cache.CacheConfig; +import org.apache.hc.client5.http.impl.cache.HeapResourceFactory; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.testing.extension.sync.CachingHttpClientResource; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +public abstract class CachingHttpClientCompatibilityTest { + + private final HttpHost target; + @RegisterExtension + private final CachingHttpClientResource clientResource; + + public CachingHttpClientCompatibilityTest(final HttpHost target) throws Exception { + this.target = target; + this.clientResource = new CachingHttpClientResource(); + this.clientResource.configure(builder -> builder + .setCacheConfig(CacheConfig.custom() + .setMaxObjectSize(10240 * 16) + .build()) + .setResourceFactory(HeapResourceFactory.INSTANCE)); + } + + CloseableHttpClient client() { + return clientResource.client(); + } + + @Test + void test_options_ping() throws Exception { + final CloseableHttpClient client = client(); + final HttpCacheContext context = HttpCacheContext.create(); + final HttpOptions options = new HttpOptions("*"); + try (ClassicHttpResponse response = client.executeOpen(target, options, context)) { + Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); + EntityUtils.consume(response.getEntity()); + } + } + + @Test + void test_get_from_cache() throws Exception { + final CloseableHttpClient client = client(); + final String[] resources1 = {"/111", "/222"}; + for (final String r : resources1) { + final HttpCacheContext context1 = HttpCacheContext.create(); + final HttpGet httpGet1 = new HttpGet(r); + try (ClassicHttpResponse response = client.executeOpen(target, httpGet1, context1)) { + Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); + Assertions.assertEquals(CacheResponseStatus.CACHE_MISS, context1.getCacheResponseStatus()); + final ResponseCacheControl responseCacheControl = context1.getResponseCacheControl(); + Assertions.assertNotNull(responseCacheControl); + Assertions.assertEquals(600, responseCacheControl.getMaxAge()); + final HttpCacheEntry cacheEntry = context1.getCacheEntry(); + Assertions.assertNotNull(cacheEntry); + EntityUtils.consume(response.getEntity()); + } + final HttpCacheContext context2 = HttpCacheContext.create(); + final HttpGet httpGet2 = new HttpGet(r); + try (ClassicHttpResponse response = client.executeOpen(target, httpGet2, context2)) { + Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); + Assertions.assertEquals(CacheResponseStatus.CACHE_HIT, context2.getCacheResponseStatus()); + final ResponseCacheControl responseCacheControl = context2.getResponseCacheControl(); + Assertions.assertNotNull(responseCacheControl); + Assertions.assertEquals(600, responseCacheControl.getMaxAge()); + final HttpCacheEntry cacheEntry = context2.getCacheEntry(); + Assertions.assertNotNull(cacheEntry); + Assertions.assertSame(cacheEntry, context1.getCacheEntry()); + EntityUtils.consume(response.getEntity()); + } + + Thread.sleep(2000); Review Comment: What's this waiting for? Is there something we could check using something like `awaitility` instead of hardcoding a `sleep` which can lead to unpredictable test behaviour. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org