io.specmatic.conversions.APIKeyInHeaderSecurityScheme.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
package io.specmatic.conversions
import io.specmatic.core.HttpRequest
import io.specmatic.core.HttpRequestPattern
import io.specmatic.core.Resolver
import io.specmatic.core.Result
import io.specmatic.core.pattern.ExactValuePattern
import io.specmatic.core.pattern.Pattern
import io.specmatic.core.pattern.Row
import io.specmatic.core.pattern.StringPattern
import io.specmatic.core.value.StringValue
data class APIKeyInHeaderSecurityScheme(val name: String, private val apiKey:String?) : OpenAPISecurityScheme {
override fun matches(httpRequest: HttpRequest): Result {
return if (httpRequest.headers.containsKey(name)) Result.Success() else Result.Failure("API-key named $name was not present as a header")
}
override fun removeParam(httpRequest: HttpRequest): HttpRequest {
return httpRequest.copy(headers = httpRequest.headers.minus(name))
}
override fun addTo(httpRequest: HttpRequest): HttpRequest {
return httpRequest.copy(headers = httpRequest.headers.plus(name to (apiKey ?: StringPattern().generate(Resolver()).toStringLiteral())))
}
override fun addTo(requestPattern: HttpRequestPattern, row: Row): HttpRequestPattern {
return addToHeaderType(name, row, requestPattern)
}
override fun isInRow(row: Row): Boolean = row.containsField(name)
}