From b070d56741557895699915ec4b0e473b268dd6da Mon Sep 17 00:00:00 2001 From: Daniel Chao Date: Fri, 22 May 2026 10:17:26 -0700 Subject: [PATCH] Remove public modifier on LazyHttpClient and RequestRewritingClient (#1609) This introduces a test helper to expose configured HTTP settings, and makes the underlying classes package-private again. --- .../org/pkl/core/http/LazyHttpClient.java | 3 +- .../pkl/core/http/RequestRewritingClient.java | 17 ++--------- .../org/pkl/core/EvaluatorBuilderTest.kt | 9 +++--- .../src/test/kotlin/org/pkl/core/http/util.kt | 30 +++++++++++++++++++ 4 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 pkl-core/src/test/kotlin/org/pkl/core/http/util.kt diff --git a/pkl-core/src/main/java/org/pkl/core/http/LazyHttpClient.java b/pkl-core/src/main/java/org/pkl/core/http/LazyHttpClient.java index 9a44b58d..dce69d73 100644 --- a/pkl-core/src/main/java/org/pkl/core/http/LazyHttpClient.java +++ b/pkl-core/src/main/java/org/pkl/core/http/LazyHttpClient.java @@ -29,9 +29,8 @@ import org.jspecify.annotations.Nullable; * An {@code HttpClient} decorator that defers creating the underlying HTTP client until the first * send. */ -// visible for testing @ThreadSafe -public final class LazyHttpClient implements HttpClient { +final class LazyHttpClient implements HttpClient { private final Supplier supplier; private final Object lock = new Object(); diff --git a/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java b/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java index 1e8f47f6..068ffe05 100644 --- a/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java +++ b/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java @@ -50,17 +50,16 @@ import org.pkl.core.util.Pair; *

Both {@code User-Agent} header and default request timeout are configurable through {@link * HttpClient.Builder}. */ -// visible for testing @ThreadSafe -public final class RequestRewritingClient implements HttpClient { +final class RequestRewritingClient implements HttpClient { // non-private for testing final String userAgent; final Duration requestTimeout; final int testPort; final HttpClient delegate; - private final Map rewritesMap; + final Map rewritesMap; + final Map>> headers; private final List> rewrites; - private final Map>> headers; private final AtomicBoolean closed = new AtomicBoolean(); @@ -267,14 +266,4 @@ public final class RequestRewritingClient implements HttpClient { "Cannot send request " + request + " because this client has already been closed."); } } - - // visible for testing - public Map getRewritesMap() { - return rewritesMap; - } - - // visible for testing - public Map>> getHeaders() { - return headers; - } } diff --git a/pkl-core/src/test/kotlin/org/pkl/core/EvaluatorBuilderTest.kt b/pkl-core/src/test/kotlin/org/pkl/core/EvaluatorBuilderTest.kt index 67a49fd1..96dfba12 100644 --- a/pkl-core/src/test/kotlin/org/pkl/core/EvaluatorBuilderTest.kt +++ b/pkl-core/src/test/kotlin/org/pkl/core/EvaluatorBuilderTest.kt @@ -21,8 +21,7 @@ import java.time.Duration import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import org.pkl.core.http.LazyHttpClient -import org.pkl.core.http.RequestRewritingClient +import org.pkl.core.http.getConfiguredSettings import org.pkl.core.project.Project import org.pkl.core.resource.TestResourceReader import org.pkl.core.util.IoUtils @@ -97,10 +96,10 @@ class EvaluatorBuilderTest { .isEqualTo(3) // two external readers, one module path assertThat(builder.resourceReaders.find { it.uriScheme == "scheme3" }).isNotNull assertThat(builder.resourceReaders.find { it.uriScheme == "scheme4" }).isNotNull - val client = (builder.httpClient as LazyHttpClient).orCreateClient as RequestRewritingClient - assertThat(client.headers) + val settings = builder.httpClient.getConfiguredSettings() + assertThat(settings.headers) .isEqualTo(mapOf(IoUtils.doubleStarGlob to mapOf("X-Foo" to listOf("Foo")))) - assertThat(client.rewritesMap) + assertThat(settings.rewritesMap) .isEqualTo(mapOf(URI("https://foo.com/") to URI("https://bar.com/"))) } } diff --git a/pkl-core/src/test/kotlin/org/pkl/core/http/util.kt b/pkl-core/src/test/kotlin/org/pkl/core/http/util.kt new file mode 100644 index 00000000..e2ff531b --- /dev/null +++ b/pkl-core/src/test/kotlin/org/pkl/core/http/util.kt @@ -0,0 +1,30 @@ +/* + * Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved. + * + * Licensed 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 + * + * https://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. + */ +package org.pkl.core.http + +import java.net.URI +import java.util.regex.Pattern + +data class HttpSettings( + val headers: Map>>, + val rewritesMap: Map, +) + +fun HttpClient.getConfiguredSettings(): HttpSettings { + this as LazyHttpClient + val requestRewritingClient = this.orCreateClient as RequestRewritingClient + return HttpSettings(requestRewritingClient.headers, requestRewritingClient.rewritesMap) +}