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

Commit 914145d

Browse files
committed
Support regex in paths
1 parent 1c5133d commit 914145d

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,37 @@ class HttpStubber(private val server: WireMockServer) {
3232
}
3333

3434
fun get(url: String, fn: RequestStubber.() -> HttpResponse) {
35-
val builder = WireMock.get(WireMock.urlEqualTo(url))
35+
val builder = WireMock.get(WireMock.urlMatching(url))
3636
stub(builder, fn)
3737
}
3838

3939
fun post(url: String, fn: RequestStubber.() -> HttpResponse) {
40-
val builder = WireMock.post(WireMock.urlEqualTo(url))
40+
val builder = WireMock.post(WireMock.urlMatching(url))
4141
stub(builder, fn)
4242
}
4343

4444
fun patch(url: String, fn: RequestStubber.() -> HttpResponse) {
45-
val builder = WireMock.patch(WireMock.urlEqualTo(url))
45+
val builder = WireMock.patch(WireMock.urlMatching(url))
4646
stub(builder, fn)
4747
}
4848

4949
fun head(url: String, fn: RequestStubber.() -> HttpResponse) {
50-
val builder = WireMock.head(WireMock.urlEqualTo(url))
50+
val builder = WireMock.head(WireMock.urlMatching(url))
5151
stub(builder, fn)
5252
}
5353

5454
fun options(url: String, fn: RequestStubber.() -> HttpResponse) {
55-
val builder = WireMock.options(WireMock.urlEqualTo(url))
55+
val builder = WireMock.options(WireMock.urlMatching(url))
5656
stub(builder, fn)
5757
}
5858

5959
fun put(url: String, fn: RequestStubber.() -> HttpResponse) {
60-
val builder = WireMock.put(WireMock.urlEqualTo(url))
60+
val builder = WireMock.put(WireMock.urlMatching(url))
6161
stub(builder, fn)
6262
}
6363

6464
fun delete(url: String, fn: RequestStubber.() -> HttpResponse) {
65-
val builder = WireMock.delete(WireMock.urlEqualTo(url))
65+
val builder = WireMock.delete(WireMock.urlMatching(url))
6666
stub(builder, fn)
6767
}
6868

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.get
9+
import io.ktor.http.HttpStatusCode
10+
11+
class RegexPathTest : FunSpec() {
12+
init {
13+
14+
val client = HttpClient(Apache)
15+
val server = install(HttpStub) {
16+
resetRequests = Reset.TEST
17+
}
18+
19+
test("with regex path") {
20+
server.mappings {
21+
get("/foo/.*") {
22+
ok()
23+
}
24+
}
25+
val resp = client.get(server.url("/foo/a"))
26+
resp.status shouldBe HttpStatusCode.OK
27+
server.invokedEndpoints() shouldBe listOf("/foo/a")
28+
}
29+
30+
test("regex path with multiple parts") {
31+
server.mappings {
32+
get("/foo/.*/.*") {
33+
ok()
34+
}
35+
}
36+
val resp = client.get(server.url("/foo/a/b"))
37+
resp.status shouldBe HttpStatusCode.OK
38+
server.invokedEndpoints() shouldBe listOf("/foo/a/b")
39+
}
40+
41+
}
42+
}

0 commit comments

Comments
 (0)