diff --git a/buildSrc/src/main/kotlin/test/server/tests/Cache.kt b/buildSrc/src/main/kotlin/test/server/tests/Cache.kt index 5a9e22c741f..ae1e2a826bc 100644 --- a/buildSrc/src/main/kotlin/test/server/tests/Cache.kt +++ b/buildSrc/src/main/kotlin/test/server/tests/Cache.kt @@ -114,6 +114,10 @@ internal fun Application.cacheTestServer() { call.response.header(HttpHeaders.CacheControl, "max-age=${Long.MAX_VALUE}000") call.respond(HttpStatusCode.OK) } + get("/invalid-max-age") { + call.response.header(HttpHeaders.CacheControl, "max-age: 120") + call.respond(HttpStatusCode.OK) + } } } } diff --git a/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cache/HttpCacheEntry.kt b/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cache/HttpCacheEntry.kt index 44e5ff38ddd..fc1256a1203 100644 --- a/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cache/HttpCacheEntry.kt +++ b/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cache/HttpCacheEntry.kt @@ -69,7 +69,7 @@ internal fun HttpResponse.cacheExpires(isShared: Boolean, fallback: () -> GMTDat val maxAge = cacheControl.firstOrNull { it.value.startsWith(maxAgeKey) } ?.value?.split("=") - ?.get(1)?.toLongOrNull() + ?.getOrNull(1)?.toLongOrNull() if (maxAge != null) { return requestTime + maxAge * 1000L diff --git a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/plugins/CacheTest.kt b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/plugins/CacheTest.kt index 8e2bd721ac2..c723daff28f 100644 --- a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/plugins/CacheTest.kt +++ b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/plugins/CacheTest.kt @@ -820,6 +820,21 @@ class CacheTest : ClientLoader() { } } + @Test + fun testInvalidMaxAge() = clientTests { + config { + install(HttpCache) + } + + test { client -> + val url = Url("$TEST_SERVER/cache/invalid-max-age") + + client.get(url).apply { + assertEquals(HttpStatusCode.OK, status) + } + } + } + /** * Does delay and ensures that the [GMTDate] measurements report at least * the specified number of [milliseconds].