|
| 1 | +/* |
| 2 | + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +import io.ktor.client.plugins.cache.* |
| 6 | +import io.ktor.client.request.* |
| 7 | +import io.ktor.client.statement.* |
| 8 | +import io.ktor.http.* |
| 9 | +import io.ktor.server.response.* |
| 10 | +import io.ktor.server.routing.* |
| 11 | +import io.ktor.server.testing.* |
| 12 | +import kotlinx.coroutines.* |
| 13 | +import kotlin.test.* |
| 14 | +import kotlin.time.Duration.Companion.milliseconds |
| 15 | + |
| 16 | +class HttpCacheTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + fun `should not mix ETags when Authorization header is present`() = testApplication { |
| 20 | + application { |
| 21 | + routing { |
| 22 | + get("/me") { |
| 23 | + val user = call.request.headers["Authorization"]!! |
| 24 | + if (user == "user-a") { |
| 25 | + // Simulate slower network for one of the requests |
| 26 | + delay(100.milliseconds) |
| 27 | + } |
| 28 | + val etag = "etag-of-$user" |
| 29 | + if (call.request.headers["If-None-Match"] == etag) { |
| 30 | + call.respond(HttpStatusCode.NotModified) |
| 31 | + return@get |
| 32 | + } |
| 33 | + call.response.header("Cache-Control", "no-cache") |
| 34 | + call.response.header("ETag", etag) |
| 35 | + call.respondText(user) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + val client = createClient { |
| 41 | + install(HttpCache) { |
| 42 | + isShared = true |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + assertEquals( |
| 47 | + client.get("/me") { |
| 48 | + headers["Authorization"] = "user-a" |
| 49 | + }.bodyAsText(), |
| 50 | + "user-a" |
| 51 | + ) |
| 52 | + withContext(Dispatchers.Default) { |
| 53 | + listOf( |
| 54 | + launch { |
| 55 | + val response = client.get("/me") { |
| 56 | + headers["Authorization"] = "user-a" |
| 57 | + }.bodyAsText() |
| 58 | + |
| 59 | + assertEquals("user-a", response) |
| 60 | + }, |
| 61 | + launch { |
| 62 | + val response = client.get("/me") { |
| 63 | + headers["Authorization"] = "user-b" |
| 64 | + }.bodyAsText() |
| 65 | + |
| 66 | + assertEquals("user-b", response) |
| 67 | + } |
| 68 | + ).joinAll() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `should mix ETags when Authorization header is present and flag enabled`() = testApplication { |
| 74 | + application { |
| 75 | + routing { |
| 76 | + get("/me") { |
| 77 | + val user = call.request.headers["Authorization"]!! |
| 78 | + if (user == "user-a") { |
| 79 | + // Simulate slower network for one of the requests |
| 80 | + delay(100.milliseconds) |
| 81 | + } |
| 82 | + val etag = "etag-of-$user" |
| 83 | + if (call.request.headers["If-None-Match"] == etag) { |
| 84 | + call.respond(HttpStatusCode.NotModified) |
| 85 | + return@get |
| 86 | + } |
| 87 | + call.response.header("Cache-Control", "no-cache") |
| 88 | + call.response.header("ETag", etag) |
| 89 | + call.respondText(user) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + val client = createClient { |
| 95 | + install(HttpCache) { |
| 96 | + isShared = true |
| 97 | + @Suppress("DEPRECATION") |
| 98 | + cacheRequestWithAuth = true |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + assertEquals( |
| 103 | + client.get("/me") { |
| 104 | + headers["Authorization"] = "user-a" |
| 105 | + }.bodyAsText(), |
| 106 | + "user-a" |
| 107 | + ) |
| 108 | + withContext(Dispatchers.Default) { |
| 109 | + listOf( |
| 110 | + launch { |
| 111 | + val response = client.get("/me") { |
| 112 | + headers["Authorization"] = "user-a" |
| 113 | + }.bodyAsText() |
| 114 | + |
| 115 | + assertEquals("user-b", response) |
| 116 | + }, |
| 117 | + launch { |
| 118 | + val response = client.get("/me") { |
| 119 | + headers["Authorization"] = "user-b" |
| 120 | + }.bodyAsText() |
| 121 | + |
| 122 | + assertEquals("user-b", response) |
| 123 | + } |
| 124 | + ).joinAll() |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments