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

Commit d8e5c62

Browse files
committed
Specify regex using Regex type
1 parent 914145d commit d8e5c62

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,73 @@ class HttpStubber(private val server: WireMockServer) {
3131
server.stubFor(builder.willReturn(resp.toReturnBuilder()))
3232
}
3333

34+
fun get(url: Regex, fn: RequestStubber.() -> HttpResponse) {
35+
val builder = WireMock.get(WireMock.urlMatching(url.pattern))
36+
stub(builder, fn)
37+
}
38+
3439
fun get(url: String, fn: RequestStubber.() -> HttpResponse) {
35-
val builder = WireMock.get(WireMock.urlMatching(url))
40+
val builder = WireMock.get(WireMock.urlEqualTo(url))
41+
stub(builder, fn)
42+
}
43+
44+
fun post(url: Regex, fn: RequestStubber.() -> HttpResponse) {
45+
val builder = WireMock.post(WireMock.urlMatching(url.pattern))
3646
stub(builder, fn)
3747
}
3848

3949
fun post(url: String, fn: RequestStubber.() -> HttpResponse) {
40-
val builder = WireMock.post(WireMock.urlMatching(url))
50+
val builder = WireMock.post(WireMock.urlEqualTo(url))
51+
stub(builder, fn)
52+
}
53+
54+
fun patch(url: Regex, fn: RequestStubber.() -> HttpResponse) {
55+
val builder = WireMock.patch(WireMock.urlMatching(url.pattern))
4156
stub(builder, fn)
4257
}
4358

4459
fun patch(url: String, fn: RequestStubber.() -> HttpResponse) {
45-
val builder = WireMock.patch(WireMock.urlMatching(url))
60+
val builder = WireMock.patch(WireMock.urlEqualTo(url))
61+
stub(builder, fn)
62+
}
63+
64+
fun head(url: Regex, fn: RequestStubber.() -> HttpResponse) {
65+
val builder = WireMock.head(WireMock.urlMatching(url.pattern))
4666
stub(builder, fn)
4767
}
4868

4969
fun head(url: String, fn: RequestStubber.() -> HttpResponse) {
50-
val builder = WireMock.head(WireMock.urlMatching(url))
70+
val builder = WireMock.head(WireMock.urlEqualTo(url))
71+
stub(builder, fn)
72+
}
73+
74+
fun options(url: Regex, fn: RequestStubber.() -> HttpResponse) {
75+
val builder = WireMock.options(WireMock.urlMatching(url.pattern))
5176
stub(builder, fn)
5277
}
5378

5479
fun options(url: String, fn: RequestStubber.() -> HttpResponse) {
55-
val builder = WireMock.options(WireMock.urlMatching(url))
80+
val builder = WireMock.options(WireMock.urlEqualTo(url))
81+
stub(builder, fn)
82+
}
83+
84+
fun put(url: Regex, fn: RequestStubber.() -> HttpResponse) {
85+
val builder = WireMock.put(WireMock.urlMatching(url.pattern))
5686
stub(builder, fn)
5787
}
5888

5989
fun put(url: String, fn: RequestStubber.() -> HttpResponse) {
60-
val builder = WireMock.put(WireMock.urlMatching(url))
90+
val builder = WireMock.put(WireMock.urlEqualTo(url))
91+
stub(builder, fn)
92+
}
93+
94+
fun delete(url: Regex, fn: RequestStubber.() -> HttpResponse) {
95+
val builder = WireMock.delete(WireMock.urlMatching(url.pattern))
6196
stub(builder, fn)
6297
}
6398

6499
fun delete(url: String, fn: RequestStubber.() -> HttpResponse) {
65-
val builder = WireMock.delete(WireMock.urlMatching(url))
100+
val builder = WireMock.delete(WireMock.urlEqualTo(url))
66101
stub(builder, fn)
67102
}
68103

src/test/kotlin/io/kotest/extensions/httpstub/RegexPathTest.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.kotest.matchers.shouldBe
66
import io.ktor.client.HttpClient
77
import io.ktor.client.engine.apache.Apache
88
import io.ktor.client.request.get
9+
import io.ktor.client.request.post
910
import io.ktor.http.HttpStatusCode
1011

1112
class RegexPathTest : FunSpec() {
@@ -18,7 +19,7 @@ class RegexPathTest : FunSpec() {
1819

1920
test("with regex path") {
2021
server.mappings {
21-
get("/foo/.*") {
22+
get("/foo/.*".toRegex()) {
2223
ok()
2324
}
2425
}
@@ -27,13 +28,29 @@ class RegexPathTest : FunSpec() {
2728
server.invokedEndpoints() shouldBe listOf("/foo/a")
2829
}
2930

31+
test("regex special characters should work when not using regex") {
32+
server.mappings {
33+
get("/foo/*") {
34+
ok()
35+
}
36+
post("/bar?q=a") {
37+
ok()
38+
}
39+
}
40+
val resp1 = client.get(server.url("/foo/*"))
41+
val resp2 = client.post(server.url("/bar?q=a"))
42+
resp1.status shouldBe HttpStatusCode.OK
43+
resp2.status shouldBe HttpStatusCode.OK
44+
server.invokedEndpoints() shouldBe listOf("/foo/*", "/bar?q=a")
45+
}
46+
3047
test("regex path with multiple parts") {
3148
server.mappings {
32-
get("/foo/.*/.*") {
49+
post("/foo/.*/.*".toRegex()) {
3350
ok()
3451
}
3552
}
36-
val resp = client.get(server.url("/foo/a/b"))
53+
val resp = client.post(server.url("/foo/a/b"))
3754
resp.status shouldBe HttpStatusCode.OK
3855
server.invokedEndpoints() shouldBe listOf("/foo/a/b")
3956
}

0 commit comments

Comments
 (0)