ru.fix.kbdd.example.MockServer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kbdd-example Show documentation
Show all versions of kbdd-example Show documentation
https://github.com/ru-fix/kbdd
package ru.fix.kbdd.example
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import ru.fix.corounit.allure.Step
import ru.fix.kbdd.json.Json
import ru.fix.kbdd.json.json
import ru.fix.kbdd.rest.Rest
import ru.fix.stdlib.socket.SocketChecker
class MockServer {
private val server = WireMockServer(WireMockConfiguration.options().port(SocketChecker.getAvailableRandomPort()))
private val mapper = jacksonObjectMapper()
init {
server.stubFor(WireMock.post(WireMock.urlEqualTo("/book-flight"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(mapper.writeValueAsString(json {
"result" % true
"price" % 142
}))
)
)
server.stubFor(WireMock.post(WireMock.urlEqualTo("/withdraw"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(mapper.writeValueAsString(json {
"result" % "success"
"bonusMiles" % 2
}))
)
)
server.stubFor(WireMock.post(WireMock.urlEqualTo("/available"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/xml")
.withBody("""
Jan
01
""".trimIndent())
)
)
}
fun start() {
server.start()
}
fun stop() {
server.stop()
}
fun baseUrl() = server.baseUrl()
@Step
suspend fun `Given server for url answers json`(url: String, response: String) {
server.stubFor(WireMock.any(WireMock.urlPathEqualTo(url))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(response))
)
}
@Step
suspend fun `Given server for url answers xml`(url: String, response: String) {
server.stubFor(WireMock.any(WireMock.urlPathEqualTo(url))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/xml")
.withBody(response))
)
}
@Step
suspend fun `Given server for url answers cookie`(url: String, response: String, cookie: String) {
server.stubFor(WireMock.any(WireMock.urlPathEqualTo(url))
.willReturn(WireMock.aResponse()
.withHeader("Set-Cookie","SESSION=$cookie")
.withHeader("Content-Type", "application/json")
.withBody(response))
)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy