Problem
When using JavaNetCookieJar with a CookieHandler/CookieManager that returns a Cookie header containing a quoted value with trailing space, OkHttp throws:
java.lang.IllegalArgumentException: value is not trimmed
at okhttp3.Cookie$Builder.value(Cookie.kt:...)
at okhttp3.java.net.cookiejar.JavaNetCookieJar.decodeHeaderAsJavaNetCookies(JavaNetCookieJar.kt:...)
...
Example problematic header:
Cookie: token="abc123 "
After unquoting, this becomes abc123 (with a trailing space). Cookie.Builder.value enforces value.trim() == value, so decodeHeaderAsJavaNetCookies currently crashes instead of tolerating the benign whitespace.
Minimal failing test
This test goes into okhttp/src/jvmTest/kotlin/okhttp3/CookiesTest.kt and fails without the fix:
@Test
fun cookieHandlerWithQuotedValueAndTrailingSpace() {
server.enqueue(MockResponse())
val serverUrl = urlWithIpAddress(server, "/")
val androidCookieHandler: CookieHandler =
object : CookieHandler() {
override fun get(
uri: URI,
map: Map<String, List<String>>,
) = mapOf(
"Cookie" to
listOf(
"token=\"abc123 \"",
),
)
override fun put(
uri: URI,
map: Map<String, List<String>>,
) {
}
}
client =
client
.newBuilder()
.cookieJar(JavaNetCookieJar(androidCookieHandler))
.build()
get(serverUrl)
val request = server.takeRequest()
assertThat(request.headers["Cookie"]).isEqualTo("token=abc123")
assertThat(request.headers["Quux"]).isNull()
}
Problem
When using JavaNetCookieJar with a CookieHandler/CookieManager that returns a Cookie header containing a quoted value with trailing space, OkHttp throws:
Example problematic header:
Cookie: token="abc123 "
After unquoting, this becomes abc123 (with a trailing space). Cookie.Builder.value enforces value.trim() == value, so decodeHeaderAsJavaNetCookies currently crashes instead of tolerating the benign whitespace.
Minimal failing test
This test goes into okhttp/src/jvmTest/kotlin/okhttp3/CookiesTest.kt and fails without the fix: