-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DefaultHeadersTest.kt
137 lines (111 loc) · 3.71 KB
/
DefaultHeadersTest.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
/*
* 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.plugins
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.application.hooks.*
import io.ktor.server.plugins.defaultheaders.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlin.test.*
class DefaultHeadersTest {
@Test
fun addsServerHeaderWithFallbackPackageNameAndVersion() = testApplication {
install(DefaultHeaders)
routing {
get { call.respond("OK") }
}
client.get("/").let { response ->
val actual = response.headers["Server"]
assertNotNull(actual)
assertTrue(actual.startsWith("Ktor/"))
}
}
@Test
fun serverHeaderIsNotModifiedIfPresent() = testApplication {
install(DefaultHeaders) {
header(HttpHeaders.Server, "xserver/1.0")
}
routing {
get { call.respond("OK") }
}
client.get("/").let { response ->
assertEquals("xserver/1.0", response.headers["Server"])
}
}
@Test
fun testSubrouteInstall(): Unit = testApplication {
routing {
route("1") {
install(DefaultHeaders) {}
get { call.respond("response") }
}
get("2") { call.respond("response") }
}
client.get("/1").let { response ->
val actual = response.headers["Server"]
assertNotNull(actual)
assertTrue(actual.startsWith("Ktor/"))
}
client.get("/2").let { response ->
assertNull(response.headers["Server"])
}
}
@Test
fun testDate(): Unit = testApplication {
var now = 1569882841014
install(DefaultHeaders) {
clock = DefaultHeadersConfig.Clock { now }
}
application {
intercept(ApplicationCallPipeline.Call) {
call.respondText("OK")
}
}
assertEquals("Mon, 30 Sep 2019 22:34:01 GMT", client.get("/").headers[HttpHeaders.Date])
now += 999
assertEquals("Mon, 30 Sep 2019 22:34:01 GMT", client.get("/").headers[HttpHeaders.Date])
now++
assertEquals("Mon, 30 Sep 2019 22:34:02 GMT", client.get("/").headers[HttpHeaders.Date])
}
@Test
fun testCustomHeader(): Unit = testApplication {
install(DefaultHeaders) {
header("X-Test", "123")
}
assertEquals("123", client.get("/").headers["X-Test"])
}
@Test
fun testDefaultServerHeader(): Unit = testApplication {
install(DefaultHeaders)
client.get("/").let { response ->
val serverHeader = response.headers[HttpHeaders.Server]
assertNotNull(serverHeader)
assertTrue("Server header invalid: $serverHeader") { "Ktor" in serverHeader }
}
}
@Test
fun testCustomServerHeader(): Unit = testApplication {
install(DefaultHeaders) {
header(HttpHeaders.Server, "MyServer")
}
assertEquals("MyServer", client.get("/").headers[HttpHeaders.Server])
}
@Test
fun testCustomServerHeaderDoesntDuplicate(): Unit = testApplication {
install(
createApplicationPlugin("test") {
on(CallSetup) {
it.response.header(HttpHeaders.Server, "MyServer")
}
}
)
install(DefaultHeaders) {
header(HttpHeaders.Server, "MyServer1")
}
assertEquals(listOf("MyServer"), client.get("/").headers.getAll(HttpHeaders.Server))
}
}