Skip to content

Commit b30c685

Browse files
committed
Headers only write their specifically configured headers
1 parent b1adea2 commit b30c685

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private <T> HttpResponse<T> doSend(
137137
ErrorMessages.create("httpRedirectCannotDowngrade", currentRequestUri, redirectUri));
138138
}
139139
currentRequestUri = rewriteUri(redirectUri);
140-
currentRequest = rewriteRequest(currentRequest, currentRequestUri);
140+
currentRequest = rewriteRequest(request, currentRequestUri);
141141
redirectCount++;
142142
} else {
143143
return response;

pkl-core/src/test/kotlin/org/pkl/core/http/HttpClientTest.kt

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ class HttpClientTest {
140140
@Suppress("JUnitMalformedDeclaration")
141141
@RegisterExtension
142142
val wireMock: WireMockExtension =
143-
WireMockExtension.newInstance()
144-
.configureStaticDsl(true)
145-
.options(
143+
with(WireMockExtension.newInstance()) {
144+
configureStaticDsl(true)
145+
options(
146146
wireMockConfig().apply {
147147
dynamicPort()
148148
dynamicHttpsPort()
@@ -151,7 +151,8 @@ class HttpClientTest {
151151
keystoreType("PKCS12")
152152
}
153153
)
154-
.build()
154+
build()
155+
}
155156

156157
@Test
157158
fun `follows redirects`() {
@@ -218,6 +219,25 @@ class HttpClientTest {
218219
.hasMessageContaining("Cannot follow redirect from 'https:' URL to 'http:' URL")
219220
}
220221

222+
@Test
223+
fun `can upgrade HTTP to HTTPS`() {
224+
stubFor(
225+
get(urlEqualTo("/foo.pkl"))
226+
.willReturn(permanentRedirect("${wireMock.runtimeInfo.httpsBaseUrl}/bar.pkl"))
227+
)
228+
stubFor(get(urlEqualTo("/bar.pkl")).willReturn(ok("hello")))
229+
230+
val client =
231+
HttpClient.builder()
232+
.addCertificates(FileTestUtils.selfSignedCertificatePem)
233+
.addHeaders("**", mapOf("x-foo" to listOf("foo value")))
234+
.build()
235+
val request =
236+
HttpRequest.newBuilder(URI("${wireMock.runtimeInfo.httpBaseUrl}/foo.pkl")).build()
237+
val response = client.send(request, HttpResponse.BodyHandlers.ofString(), NoopChecker)
238+
assertThat(response.body()).isEqualTo("hello")
239+
}
240+
221241
@Test
222242
fun `infinite redirects fail with VmException`() {
223243
stubFor(get(urlEqualTo("/foo.pkl")).willReturn(permanentRedirect("/bar.pkl")))
@@ -269,5 +289,23 @@ class HttpClientTest {
269289
)
270290
)
271291
}
292+
293+
@Test
294+
fun `redirects only carry their specifically configured headers`() {
295+
stubFor(get(urlEqualTo("/foo.pkl")).willReturn(permanentRedirect("/bar.pkl")))
296+
stubFor(get(urlEqualTo("/bar.pkl")).willReturn(ok()))
297+
val request =
298+
HttpRequest.newBuilder(URI("${wireMock.runtimeInfo.httpBaseUrl}/foo.pkl")).build()
299+
val client =
300+
with(HttpClient.builder()) {
301+
addHeaders("**/foo.pkl", mapOf("x-foo" to listOf("foo value")))
302+
addHeaders("**/bar.pkl", mapOf("x-bar" to listOf("bar value")))
303+
build()
304+
}
305+
client.send(request, HttpResponse.BodyHandlers.discarding(), NoopChecker)
306+
verify(getRequestedFor(urlEqualTo("/foo.pkl")).withHeader("x-foo", matching("foo value")))
307+
verify(getRequestedFor(urlEqualTo("/bar.pkl")).withoutHeader("x-foo"))
308+
verify(getRequestedFor(urlEqualTo("/bar.pkl")).withHeader("x-bar", matching("bar value")))
309+
}
272310
}
273311
}

0 commit comments

Comments
 (0)