-
Notifications
You must be signed in to change notification settings - Fork 1k
/
TestApplication.kt
270 lines (240 loc) · 8.04 KB
/
TestApplication.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("DEPRECATION")
package io.ktor.server.testing
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.routing.*
import io.ktor.server.testing.client.*
import io.ktor.test.dispatcher.*
import io.ktor.util.*
import io.ktor.util.pipeline.*
import kotlinx.coroutines.*
/**
* A client attached to [TestApplication].
*/
@KtorDsl
public interface ClientProvider {
/**
* Returns a client with the default configuration.
* @see [testApplication]
*/
public val client: HttpClient
/**
* Creates a client with a custom configuration.
* For example, to send JSON data in a test POST/PUT request, you can install the `ContentNegotiation` plugin:
* ```kotlin
* fun testPostCustomer() = testApplication {
* val client = createClient {
* install(ContentNegotiation) {
* json()
* }
* }
* }
* ```
* @see [testApplication]
*/
@KtorDsl
public fun createClient(block: HttpClientConfig<out HttpClientEngineConfig>.() -> Unit): HttpClient
}
/**
* A configured instance of a test application running locally.
* @see [testApplication]
*/
public class TestApplication internal constructor(
private val builder: ApplicationTestBuilder
) : ClientProvider by builder {
internal val engine by lazy { builder.engine }
public fun stop() {
builder.engine.stop(0, 0)
builder.externalServices.externalApplications.values.forEach { it.stop() }
client.close()
}
}
/**
* Creates an instance of [TestApplication] configured with the builder [block].
* Make sure to call [TestApplication.stop] after your tests.
* @see [testApplication]
*/
@KtorDsl
public fun TestApplication(
block: TestApplicationBuilder.() -> Unit
): TestApplication {
val builder = ApplicationTestBuilder()
val testApplication = TestApplication(builder)
builder.block()
return testApplication
}
/**
* Registers mocks for external services.
*/
@KtorDsl
public class ExternalServicesBuilder internal constructor(private val testApplicationBuilder: TestApplicationBuilder) {
@Deprecated(message = "This constructor will be removed from public API", level = DeprecationLevel.HIDDEN)
public constructor() : this(TestApplicationBuilder())
private val externalApplicationBuilders = mutableMapOf<String, () -> TestApplication>()
internal val externalApplications: Map<String, TestApplication> by lazy {
externalApplicationBuilders.mapValues { it.value.invoke() }
}
/**
* Registers a mock for an external service specified by [hosts] and configured with [block].
* @see [testApplication]
*/
@KtorDsl
public fun hosts(vararg hosts: String, block: Application.() -> Unit) {
check(hosts.isNotEmpty()) { "hosts can not be empty" }
hosts.forEach {
val protocolWithAuthority = Url(it).protocolWithAuthority
externalApplicationBuilders[protocolWithAuthority] = {
TestApplication {
environment(this@ExternalServicesBuilder.testApplicationBuilder.environmentBuilder)
applicationModules.add(block)
}
}
}
}
}
/**
* A builder for [TestApplication].
*/
@KtorDsl
public open class TestApplicationBuilder {
private var built = false
internal val externalServices = ExternalServicesBuilder(this)
internal val applicationModules = mutableListOf<Application.() -> Unit>()
internal var environmentBuilder: ApplicationEngineEnvironmentBuilder.() -> Unit = {}
internal val job = Job()
internal val environment by lazy {
built = true
createTestEnvironment {
modules.addAll(this@TestApplicationBuilder.applicationModules)
developmentMode = true
val oldConfig = config
this@TestApplicationBuilder.environmentBuilder(this)
if (config == oldConfig) { // the user did not set config. load the default one
config = DefaultTestConfig()
}
parentCoroutineContext += this@TestApplicationBuilder.job
}
}
internal val engine by lazy {
TestApplicationEngine(environment)
}
/**
* Builds mocks for external services using [ExternalServicesBuilder].
* @see [testApplication]
*/
@KtorDsl
public fun externalServices(block: ExternalServicesBuilder.() -> Unit) {
checkNotBuilt()
externalServices.block()
}
/**
* Builds an environment using [block].
* @see [testApplication]
*/
@KtorDsl
public fun environment(block: ApplicationEngineEnvironmentBuilder.() -> Unit) {
checkNotBuilt()
environmentBuilder = block
}
/**
* Adds a module to [TestApplication].
* @see [testApplication]
*/
@KtorDsl
public fun application(block: Application.() -> Unit) {
checkNotBuilt()
applicationModules.add(block)
}
/**
* Installs a [plugin] into [TestApplication]
*/
@Suppress("UNCHECKED_CAST")
@KtorDsl
public fun <P : Pipeline<*, ApplicationCall>, B : Any, F : Any> install(
plugin: Plugin<P, B, F>,
configure: B.() -> Unit = {}
) {
checkNotBuilt()
applicationModules.add { install(plugin as Plugin<ApplicationCallPipeline, B, F>, configure) }
}
/**
* Installs routing into [TestApplication]
*/
@KtorDsl
public fun routing(configuration: Routing.() -> Unit) {
checkNotBuilt()
applicationModules.add { routing(configuration) }
}
private fun checkNotBuilt() {
check(!built) {
"The test application has already been built. Make sure you configure the application " +
"before accessing the client for the first time."
}
}
}
/**
* A builder for a test that uses [TestApplication].
*/
@KtorDsl
public class ApplicationTestBuilder : TestApplicationBuilder(), ClientProvider {
override val client by lazy { createClient { } }
@KtorDsl
override fun createClient(
block: HttpClientConfig<out HttpClientEngineConfig>.() -> Unit
): HttpClient = HttpClient(DelegatingTestClientEngine) {
engine {
parentJob = this@ApplicationTestBuilder.job
appEngineProvider = { this@ApplicationTestBuilder.engine }
externalApplicationsProvider = { this@ApplicationTestBuilder.externalServices.externalApplications }
}
block()
}
}
/**
* Creates a test using [TestApplication].
* To test a server Ktor application, do the following:
* 1. Use [testApplication] function to set up a configured instance of a test application running locally.
* 2. Use the [HttpClient] instance inside a test application to make a request to your server,
* receive a response, and make assertions.
*
* Suppose, you have the following route that accepts GET requests made to the `/` path
* and responds with a plain text response:
* ```kotlin
* routing {
* get("/") {
* call.respondText("Hello, world!")
* }
* }
* ```
*
* A test for this route will look as follows:
* ```kotlin
* @Test
* fun testRoot() = testApplication {
* val response = client.get("/")
* assertEquals(HttpStatusCode.OK, response.status)
* assertEquals("Hello, world!", response.bodyAsText())
* }
* ```
*
* _Note: If you have the `application.conf` file in the `resources` folder,
* [testApplication] loads all modules and properties specified in the configuration file automatically._
*
* You can learn more from [Testing](https://ktor.io/docs/testing.html).
*/
@KtorDsl
public fun testApplication(
block: suspend ApplicationTestBuilder.() -> Unit
) {
val builder = ApplicationTestBuilder()
.apply { runBlocking { block() } }
val testApplication = TestApplication(builder)
testApplication.engine.start()
testApplication.stop()
}