Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit e819646

Browse files
authored
Merge pull request #9 from kotest/add-request-body
add ability to differentiate requests by body
2 parents 30f41e1 + d8757d9 commit e819646

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/main/kotlin/io/kotest/extensions/httpstub/dsl.kt

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package io.kotest.extensions.httpstub
33
import com.github.tomakehurst.wiremock.WireMockServer
44
import com.github.tomakehurst.wiremock.client.MappingBuilder
55
import com.github.tomakehurst.wiremock.client.WireMock
6+
import com.github.tomakehurst.wiremock.client.WireMock.binaryEqualTo
7+
import com.github.tomakehurst.wiremock.client.WireMock.equalToJson
68
import com.github.tomakehurst.wiremock.matching.EqualToPattern
79
import io.ktor.http.ContentType
810
import io.ktor.http.HttpStatusCode
@@ -16,10 +18,14 @@ fun mappings(configure: HttpStubber.() -> Unit): HttpStubber.() -> Unit {
1618
class RequestStubber {
1719

1820
val headers = mutableMapOf<String, String>()
21+
var requestBody: String? = null
1922

2023
fun header(name: String, value: String) {
2124
headers[name] = value
2225
}
26+
fun body(json: String) {
27+
requestBody = json
28+
}
2329
}
2430

2531
class HttpStubber(private val server: WireMockServer) {
@@ -28,6 +34,9 @@ class HttpStubber(private val server: WireMockServer) {
2834
val rs = RequestStubber()
2935
val resp = rs.fn()
3036
rs.headers.forEach { (name, value) -> builder.withHeader(name, EqualToPattern(value)) }
37+
if (rs.requestBody != null) {
38+
builder.withRequestBody(equalToJson(rs.requestBody))
39+
}
3140
server.stubFor(builder.willReturn(resp.toReturnBuilder()))
3241
}
3342

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.kotest.extensions.httpstub
2+
3+
import io.kotest.core.extensions.install
4+
import io.kotest.core.spec.style.FunSpec
5+
import io.kotest.matchers.shouldBe
6+
import io.ktor.client.HttpClient
7+
import io.ktor.client.engine.apache.Apache
8+
import io.ktor.client.request.post
9+
import io.ktor.client.request.setBody
10+
import io.ktor.client.statement.bodyAsText
11+
import io.ktor.http.HttpStatusCode
12+
13+
class RequestBodyTest : FunSpec() {
14+
init {
15+
16+
val client = HttpClient(Apache)
17+
val server = install(HttpStub)
18+
19+
test("differentiate requests with request body") {
20+
server.mappings {
21+
post("/foo") {
22+
body("""{"test": true}""")
23+
HttpResponse(HttpStatusCode.OK, "true")
24+
}
25+
26+
post("/foo") {
27+
body("""{"test": false}""")
28+
HttpResponse(HttpStatusCode.OK, "false")
29+
}
30+
}
31+
32+
val resp = client.post(server.url("/foo")) {
33+
setBody("""{"test": true}""")
34+
}
35+
resp.status shouldBe HttpStatusCode.OK
36+
resp.bodyAsText() shouldBe "true"
37+
38+
val resp2 = client.post(server.url("/foo")) {
39+
setBody("""{"test": false}""")
40+
}
41+
resp2.status shouldBe HttpStatusCode.OK
42+
resp2.bodyAsText() shouldBe "false"
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)