Test openapi schema requestBody
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package integration.steps.`when`
|
||||
|
||||
import integration.steps.then.`And the schema must be valid`
|
||||
import integration.steps.then.`And the schema request body must be valid`
|
||||
import io.ktor.application.ApplicationCall
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpMethod
|
||||
@@ -8,7 +11,7 @@ import io.ktor.server.testing.TestApplicationEngine
|
||||
import io.ktor.server.testing.TestApplicationRequest
|
||||
import io.ktor.server.testing.setBody
|
||||
|
||||
fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall {
|
||||
fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, validate: Boolean = true, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall {
|
||||
return handleRequest(true) {
|
||||
method = HttpMethod.Get
|
||||
if (uri != null) {
|
||||
@@ -16,36 +19,54 @@ fun TestApplicationEngine.`When I send a GET request`(uri: String? = null, setup
|
||||
}
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
setup?.let { it() }
|
||||
}.apply {
|
||||
if (validate) {
|
||||
response.`And the schema must be valid`()
|
||||
requestBody?.let { body ->
|
||||
response.`And the schema request body must be valid`(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`When I send a POST request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
|
||||
fun TestApplicationEngine.`When I send a POST request`(uri: String? = null, validate: Boolean = true, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall {
|
||||
return handleRequest(true) {
|
||||
method = HttpMethod.Post
|
||||
if (uri != null) {
|
||||
this.uri = uri
|
||||
}
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
setup?.let { it() }?.let {
|
||||
setBody(it.trimIndent())
|
||||
addHeader(HttpHeaders.Accept, ContentType.Application.Json.toString())
|
||||
setup?.let { it() }
|
||||
}.apply {
|
||||
if (validate) {
|
||||
response.`And the schema must be valid`()
|
||||
requestBody?.let { body ->
|
||||
response.`And the schema request body must be valid`(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`When I send a PUT request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
|
||||
fun TestApplicationEngine.`When I send a PUT request`(uri: String? = null, validate: Boolean = true, setup: (TestApplicationRequest.() -> Unit)? = null): TestApplicationCall {
|
||||
return handleRequest(true) {
|
||||
method = HttpMethod.Put
|
||||
if (uri != null) {
|
||||
this.uri = uri
|
||||
}
|
||||
addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
|
||||
setup?.let { it() }?.let {
|
||||
setBody(it.trimIndent())
|
||||
setup?.let { it() }
|
||||
}.apply {
|
||||
if (validate) {
|
||||
response.`And the schema must be valid`()
|
||||
requestBody?.let { body ->
|
||||
response.`And the schema request body must be valid`(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun TestApplicationEngine.`When I send a DELETE request`(uri: String? = null, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
|
||||
fun TestApplicationEngine.`When I send a DELETE request`(uri: String? = null, validate: Boolean = true, setup: (TestApplicationRequest.() -> String?)? = null): TestApplicationCall {
|
||||
return handleRequest(true) {
|
||||
method = HttpMethod.Delete
|
||||
if (uri != null) {
|
||||
@@ -55,9 +76,30 @@ fun TestApplicationEngine.`When I send a DELETE request`(uri: String? = null, se
|
||||
setup?.let { it() }?.let {
|
||||
setBody(it.trimIndent())
|
||||
}
|
||||
}.apply {
|
||||
if (validate) {
|
||||
response.`And the schema must be valid`()
|
||||
requestBody?.let { body ->
|
||||
response.`And the schema request body must be valid`(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun TestApplicationRequest.`with body`(body: String) {
|
||||
setBody(body.trimIndent())
|
||||
private val requestBodies: MutableMap<ApplicationCall, String> = mutableMapOf()
|
||||
var TestApplicationCall.requestBody: String?
|
||||
get() = requestBodies[this]
|
||||
set(value) {
|
||||
if (value == null) {
|
||||
requestBodies.remove(this)
|
||||
} else {
|
||||
requestBodies[this] = value
|
||||
}
|
||||
}
|
||||
|
||||
infix fun TestApplicationRequest.`with body`(body: String) {
|
||||
return body.trimIndent().let {
|
||||
setBody(it)
|
||||
(call as TestApplicationCall).requestBody = it
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user