ok2c commented on code in PR #961: URL: https://github.com/apache/httpcomponents-client/pull/961#discussion_r4664169686
########## httpclient5-testing/src/test/java/org/apache/hc/client5/testing/caching/sync/TestCachingClientVaryCookieAuthorizationNotCached.java: ########## @@ -0,0 +1,212 @@ +/* + * ==================================================================== + * 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.caching.sync; + +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.hc.client5.http.auth.AuthCache; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.cache.HttpCacheContext; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.cookie.BasicCookieStore; +import org.apache.hc.client5.http.impl.auth.BasicAuthCache; +import org.apache.hc.client5.http.impl.auth.BasicScheme; +import org.apache.hc.client5.http.impl.cache.HeapResourceFactory; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.cookie.BasicClientCookie; +import org.apache.hc.client5.testing.extension.sync.CachingHttpClientResource; +import org.apache.hc.client5.testing.extension.sync.TestServerResources; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.URIScheme; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; +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; + +/** + * A response whose {@code Vary} header names {@code Cookie} or {@code Authorization} must never be + * cached at all: the cache sits above the point in the execution chain where these two headers are + * added to the request, so it can never select the correct variant for them. Every request for such + * a resource, including repeated requests from the very same session or credentials, must reach the + * origin; none of it may be served from a stored variant. + */ +class TestCachingClientVaryCookieAuthorizationNotCached { + + static final Timeout TIMEOUT = Timeout.ofMinutes(1); + + @RegisterExtension + private final TestServerResources serverResources; + @RegisterExtension + private final CachingHttpClientResource clientResource; + + private final AtomicInteger aliceHits = new AtomicInteger(); + private final AtomicInteger bobHits = new AtomicInteger(); + private final AtomicInteger anonymousHits = new AtomicInteger(); + + TestCachingClientVaryCookieAuthorizationNotCached() throws Exception { + this.serverResources = new TestServerResources(URIScheme.HTTP, TIMEOUT); + this.clientResource = new CachingHttpClientResource(); + this.clientResource.configure(builder -> builder + .setResourceFactory(HeapResourceFactory.INSTANCE)); + } + + HttpHost startServer() throws Exception { + final InetSocketAddress address = serverResources.server().start(); + return new HttpHost(serverResources.scheme().id, "localhost", address.getPort()); + } + + private HttpCacheContext sessionContext(final HttpHost target, final String sessionCookieValue) { + final BasicCookieStore cookieStore = new BasicCookieStore(); + final BasicClientCookie cookie = new BasicClientCookie("session", sessionCookieValue); + cookie.setDomain(target.getHostName()); + cookie.setPath("/"); + cookieStore.addCookie(cookie); + final HttpCacheContext context = HttpCacheContext.create(); + context.setCookieStore(cookieStore); + return context; + } + + private HttpCacheContext preemptiveAuthContext(final HttpHost target, final String user) { + final BasicScheme basicScheme = new BasicScheme(); + basicScheme.initPreemptive(new UsernamePasswordCredentials(user, "secret".toCharArray())); + final AuthCache authCache = new BasicAuthCache(); + authCache.put(target, basicScheme); + final HttpCacheContext context = HttpCacheContext.create(); + context.setAuthCache(authCache); + return context; + } + + private static String decodeBasicUser(final String authorizationHeaderValue) { + final String credentials = new String( + Base64.getDecoder().decode(authorizationHeaderValue.substring("Basic ".length())), + StandardCharsets.UTF_8); + return credentials.substring(0, credentials.indexOf(':')); + } + + private String fetch(final CloseableHttpClient client, final HttpHost target, final HttpCacheContext context) throws Exception { + return client.execute(target, new HttpGet("/resource"), context, response -> { + Assertions.assertEquals(HttpStatus.SC_OK, response.getCode()); + return EntityUtils.toString(response.getEntity()); Review Comment: @arturobernalg Please avoid using deprecated methods. Could you please fold all existing cache integration tests from `TestCaching*ClientAuthentication` into a single class, like `TestCaching*Integration` and try to keep their resource wiring consistent if possible? Otherwise looks good. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
