Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
run.smt.ktest.rest.api.SimpleRequests.kt Maven / Gradle / Ivy
package run.smt.ktest.rest.api
import com.fasterxml.jackson.databind.JavaType
import io.restassured.response.Response
import io.restassured.specification.RequestSender
import org.hamcrest.CustomTypeSafeMatcher
import run.smt.ktest.json.TypeDSL
import run.smt.ktest.json.type
import kotlin.reflect.KClass
/**
* COPY-PASTE avoidance
*/
private inline fun SimpleRequests.request(clazz: KClass, parameters: Array, ignoreStatusCode: Boolean, query: RequestSender.() -> Response): T {
return request(parameters, ignoreStatusCode, query).`as`(clazz)
}
private inline fun SimpleRequests.request(clazz: JavaType, parameters: Array, ignoreStatusCode: Boolean, query: RequestSender.() -> Response): T {
return request(parameters, ignoreStatusCode, query).`as`(clazz)
}
private inline fun SimpleRequests.request(parameters: Array, ignoreStatusCode: Boolean, query: RequestSender.() -> Response): Response {
return request(parameters.asSequence())
.expect()
.apply {
if (!ignoreStatusCode) {
statusCode(object : CustomTypeSafeMatcher("status code is successful") {
override fun matchesSafely(statusCode: Int?) = statusCode != null && statusCode >= 200 && statusCode < 400
})
}
}
.`when`()
.query().andReturn()
}
private fun extractPathParams(parameters: Array): Map =
parameters.asSequence().filterIsInstance().associate { it.name to it.value }
/**
* Part of REST DSL for performing simple requests with automatic deserialization to requested type + check for status code
* to be successful
*/
interface SimpleRequests : Deserialization, ComplexQueriesBuilder {
// Plain reflection responses
fun String.GET(clazz: KClass, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { get(this@GET, extractPathParams(parameters)) }
fun String.POST(clazz: KClass, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { post(this@POST, extractPathParams(parameters)) }
fun String.PUT(clazz: KClass, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { put(this@PUT, extractPathParams(parameters)) }
fun String.HEAD(clazz: KClass, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { head(this@HEAD, extractPathParams(parameters)) }
fun String.OPTIONS(clazz: KClass, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { options(this@OPTIONS, extractPathParams(parameters)) }
fun String.PATCH(clazz: KClass, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { patch(this@PATCH, extractPathParams(parameters)) }
fun String.DELETE(clazz: KClass, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { delete(this@DELETE, extractPathParams(parameters)) }
// Jackson's JavaTypes
fun String.GET(clazz: JavaType, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { get(this@GET, extractPathParams(parameters)) }
fun String.POST(clazz: JavaType, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { post(this@POST, extractPathParams(parameters)) }
fun String.PUT(clazz: JavaType, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { put(this@PUT, extractPathParams(parameters)) }
fun String.HEAD(clazz: JavaType, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { head(this@HEAD, extractPathParams(parameters)) }
fun String.OPTIONS(clazz: JavaType, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { options(this@OPTIONS, extractPathParams(parameters)) }
fun String.PATCH(clazz: JavaType, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { patch(this@PATCH, extractPathParams(parameters)) }
fun String.DELETE(clazz: JavaType, vararg parameters: RequestElement, ignoreStatusCode: Boolean = false): T =
request(clazz, parameters, ignoreStatusCode) { delete(this@DELETE, extractPathParams(parameters)) }
// kTest JSON TypeDSLs
fun String.GET(vararg parameters: RequestElement, ignoreStatusCode: Boolean = false, builder: TypeDSL): T =
GET(clazz = type(builder), parameters = *parameters, ignoreStatusCode = ignoreStatusCode)
fun String.POST(vararg parameters: RequestElement, ignoreStatusCode: Boolean = false, builder: TypeDSL): T =
POST(clazz = type(builder), parameters = *parameters, ignoreStatusCode = ignoreStatusCode)
fun String.PUT(vararg parameters: RequestElement, ignoreStatusCode: Boolean = false, builder: TypeDSL): T =
PUT(clazz = type(builder), parameters = *parameters, ignoreStatusCode = ignoreStatusCode)
fun String.HEAD(vararg parameters: RequestElement, ignoreStatusCode: Boolean = false, builder: TypeDSL): T =
HEAD(clazz = type(builder), parameters = *parameters, ignoreStatusCode = ignoreStatusCode)
fun String.OPTIONS(vararg parameters: RequestElement, ignoreStatusCode: Boolean = false, builder: TypeDSL): T =
OPTIONS(clazz = type(builder), parameters = *parameters, ignoreStatusCode = ignoreStatusCode)
fun String.PATCH(vararg parameters: RequestElement, ignoreStatusCode: Boolean = false, builder: TypeDSL): T =
PATCH(clazz = type(builder), parameters = *parameters, ignoreStatusCode = ignoreStatusCode)
fun String.DELETE(vararg parameters: RequestElement, ignoreStatusCode: Boolean = false, builder: TypeDSL): T =
DELETE(clazz = type(builder), parameters = *parameters, ignoreStatusCode = ignoreStatusCode)
}