Use Locale.ROOT when lowercasing rewrite URIs (#1618)

Use Locale.ROOT to apply the lowecase format. For URI scheme and host
locale-neutral casing is the semantically the correct choice. Added a
unit test that sets the default locale to tr-TR and that would fail
without the fix.
This commit is contained in:
Mirko Alicastro
2026-05-26 18:37:42 +02:00
committed by GitHub
parent a1eea47b3f
commit ff319faef3
2 changed files with 22 additions and 2 deletions
@@ -26,6 +26,7 @@ import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
@@ -205,9 +206,9 @@ final class RequestRewritingClient implements HttpClient {
private URI normalizeRewrite(URI uri) { private URI normalizeRewrite(URI uri) {
try { try {
return new URI( return new URI(
uri.getScheme().toLowerCase(), uri.getScheme().toLowerCase(Locale.ROOT),
uri.getUserInfo(), uri.getUserInfo(),
uri.getHost().toLowerCase(), uri.getHost().toLowerCase(Locale.ROOT),
uri.getPort(), uri.getPort(),
uri.getPath(), uri.getPath(),
uri.getQuery(), uri.getQuery(),
@@ -21,6 +21,7 @@ import java.net.http.HttpRequest
import java.net.http.HttpRequest.BodyPublishers import java.net.http.HttpRequest.BodyPublishers
import java.net.http.HttpResponse.BodyHandlers import java.net.http.HttpResponse.BodyHandlers
import java.time.Duration import java.time.Duration
import java.util.Locale
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatList import org.assertj.core.api.Assertions.assertThatList
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@@ -321,6 +322,24 @@ class RequestRewritingClientTest {
.isEqualTo("https://bar.com/bar/baz") .isEqualTo("https://bar.com/bar/baz")
} }
@Test
fun `rewrites URIs - host with capital I under tr_TR locale`() {
// Under tr_TR, "INDEX.COM".toLowerCase() becomes "ındex.com".
val previous = Locale.getDefault()
Locale.setDefault(Locale.forLanguageTag("tr-TR"))
try {
assertThat(
rewrittenRequest(
"https://INDEX.COM/foo/bar",
mapOf(URI("https://index.com/") to URI("https://bar.com/")),
)
)
.isEqualTo("https://bar.com/foo/bar")
} finally {
Locale.setDefault(previous)
}
}
private fun rewrittenRequest(uri: String, rules: Map<URI, URI>): String { private fun rewrittenRequest(uri: String, rules: Map<URI, URI>): String {
val captured = RequestCapturingClient() val captured = RequestCapturingClient()
val client = RequestRewritingClient("Pkl", Duration.ofSeconds(42), -1, captured, rules, mapOf()) val client = RequestRewritingClient("Pkl", Duration.ofSeconds(42), -1, captured, rules, mapOf())