52 lines
1.3 KiB
Kotlin
52 lines
1.3 KiB
Kotlin
package utils
|
|
|
|
import io.ktor.http.HttpHeaders
|
|
import io.ktor.http.HttpMethod
|
|
import io.ktor.server.testing.*
|
|
import org.json.JSONObject
|
|
|
|
fun TestApplicationRequest.json(block: (JSONObject) -> Unit) {
|
|
addHeader(HttpHeaders.ContentType, "application/json")
|
|
setBody(JSONObject().apply(block).toString())
|
|
}
|
|
|
|
fun TestApplicationRequest.setToken(token: String) {
|
|
addHeader(HttpHeaders.Authorization, "Bearer $token")
|
|
}
|
|
|
|
fun TestApplicationEngine.post(
|
|
uri: String,
|
|
setup: TestApplicationRequest.() -> Unit = {}
|
|
): TestApplicationResponse = handleRequest {
|
|
this.uri = uri
|
|
this.method = HttpMethod.Post
|
|
setup()
|
|
}.response
|
|
|
|
fun TestApplicationEngine.get(
|
|
uri: String,
|
|
setup: TestApplicationRequest.() -> Unit = {}
|
|
): TestApplicationResponse = handleRequest {
|
|
this.uri = uri
|
|
this.method = HttpMethod.Get
|
|
setup()
|
|
}.response
|
|
|
|
fun TestApplicationEngine.delete(
|
|
uri: String,
|
|
setup: TestApplicationRequest.() -> Unit = {}
|
|
): TestApplicationResponse = handleRequest {
|
|
this.uri = uri
|
|
this.method = HttpMethod.Delete
|
|
setup()
|
|
}.response
|
|
|
|
fun TestApplicationEngine.put(
|
|
uri: String,
|
|
setup: TestApplicationRequest.() -> Unit = {}
|
|
): TestApplicationResponse = handleRequest {
|
|
this.uri = uri
|
|
this.method = HttpMethod.Put
|
|
setup()
|
|
}.response
|