/
CachingHeadersTest.kt
171 lines (156 loc) · 5.02 KB
/
CachingHeadersTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.tests.server.plugins
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlin.test.*
class CachingHeadersTest {
@Test
fun testNoPluginInstalled(): Unit = test(
configure = {},
test = { response ->
assertEquals(null, response.headers[HttpHeaders.CacheControl])
}
)
@Test
fun testByPass(): Unit = test(
configure = {
install(CachingHeaders)
},
test = { response ->
assertEquals("no-cache", response.headers[HttpHeaders.CacheControl])
}
)
@Test
fun addNoStore(): Unit = test(
configure = {
install(CachingHeaders) {
options { _, _ -> CachingOptions(CacheControl.NoStore(CacheControl.Visibility.Private)) }
}
},
test = { response ->
assertEquals("no-cache, private, no-store", response.headers[HttpHeaders.CacheControl])
}
)
@Test
fun testAddMaxAgeAndNoStore(): Unit = test(
configure = {
install(CachingHeaders) {
options { _, _ -> CachingOptions(CacheControl.NoStore(CacheControl.Visibility.Private)) }
options { _, _ -> CachingOptions(CacheControl.MaxAge(15)) }
}
},
test = { response ->
assertEquals(
"no-cache, no-store, max-age=15, private",
response.headers[HttpHeaders.CacheControl]
)
}
)
object Immutable: CacheControl(null) {
override fun toString(): String = "immutable"
}
@Test
fun testCustomCacheControl(): Unit = test(
configure = {
install(CachingHeaders) {
options { _, _ -> CachingOptions(CacheControl.NoStore(CacheControl.Visibility.Private)) }
options { _, _ -> CachingOptions(Immutable) }
}
},
test = { response ->
assertEquals(
"no-cache, private, no-store, immutable",
response.headers[HttpHeaders.CacheControl]
)
}
)
@Test
fun testSetInCall() = testApplication {
install(CachingHeaders)
routing {
get("/") {
call.caching = CachingOptions(CacheControl.NoCache(null))
call.respondText("test")
}
}
client.get("/").let { response ->
assertEquals("no-cache", response.headers[HttpHeaders.CacheControl])
}
}
@Test
fun testSetInCallAndContent() = testApplication {
install(CachingHeaders)
routing {
get("/") {
call.caching = CachingOptions(CacheControl.NoCache(null))
call.respondText("test") {
caching = CachingOptions(CacheControl.MaxAge(15))
}
}
}
client.get("/").let { response ->
assertEquals("no-cache, max-age=15", response.headers[HttpHeaders.CacheControl])
}
}
@Test
fun testSubrouteInstall() = testApplication {
application {
routing {
route("/1") {
install(CachingHeaders) {
options { _, _ -> CachingOptions(CacheControl.NoStore(CacheControl.Visibility.Private)) }
options { _, _ -> CachingOptions(CacheControl.MaxAge(15)) }
}
get {
call.respondText("test") {
caching = CachingOptions(CacheControl.NoCache(null))
}
}
}
get("/2") {
call.respondText("test") {
caching = CachingOptions(CacheControl.NoCache(null))
}
}
}
}
client.get("/1").let {
assertEquals(
"no-cache, no-store, max-age=15, private",
it.headers[HttpHeaders.CacheControl]
)
}
client.get("/2").let {
assertNull(it.headers[HttpHeaders.CacheControl])
}
}
private fun test(
configure: Application.() -> Unit,
test: (HttpResponse) -> Unit
): Unit = testApplication {
application {
configure(this)
routing {
get("/") {
call.respondText("test") {
caching = CachingOptions(CacheControl.NoCache(null))
}
}
}
}
client.get("/").let {
assertTrue(it.status.isSuccess())
assertEquals("test", it.bodyAsText().trim())
test(it)
}
}
}