-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
60 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
[versions] | ||
wiremock = "3.6.0" | ||
kotest = "5.5.4" | ||
fuel = "2.3.1" | ||
|
||
[libraries] | ||
kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" } | ||
kotest-api = { module = "io.kotest:kotest-framework-api", version.ref = "kotest" } | ||
kotest-runner = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" } | ||
|
||
fuel = { module = "com.github.kittinunf.fuel:fuel", version.ref = "fuel" } | ||
|
||
wiremock = { module = "org.wiremock:wiremock-standalone", version.ref = "wiremock" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.kotest.extensions.wiremock | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.client.WireMock.get | ||
import com.github.tomakehurst.wiremock.client.WireMock.ok | ||
import com.github.tomakehurst.wiremock.client.WireMock.stubFor | ||
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo | ||
import com.github.tomakehurst.wiremock.stubbing.StubMapping | ||
|
||
fun stubOk(url: String) { | ||
stubFor( | ||
get(urlEqualTo(url)) | ||
.willReturn(ok()) | ||
) | ||
} | ||
|
||
fun WireMockServer.stubOk(url: String): StubMapping = stubFor( | ||
get(urlEqualTo(url)) | ||
) |
29 changes: 10 additions & 19 deletions
29
src/test/kotlin/io/kotest/extensions/wiremock/WiremockListenerPerProjectTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,22 @@ | ||
package io.kotest.extensions.wiremock | ||
|
||
import com.github.tomakehurst.wiremock.client.WireMock.get | ||
import com.github.tomakehurst.wiremock.client.WireMock.ok | ||
import com.github.tomakehurst.wiremock.client.WireMock.stubFor | ||
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo | ||
import com.github.kittinunf.fuel.httpGet | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
@Suppress("BlockingMethodInNonBlockingContext") | ||
class WiremockListenerPerProjectTest : FunSpec({ | ||
test("should have started wiremock server") { | ||
stubFor( | ||
get(urlEqualTo("/test")) | ||
.willReturn(ok()) | ||
) | ||
val connection = URL("http://localhost:9001/test").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
stubOk("/test") | ||
|
||
val (request, response, result) = "http://localhost:9001/test".httpGet().response() | ||
response.statusCode shouldBe 200 | ||
} | ||
|
||
test("should have started wiremock server for second test as well") { | ||
stubFor( | ||
get(urlEqualTo("/second-test")) | ||
.willReturn(ok()) | ||
) | ||
val connection = URL("http://localhost:9001/second-test").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
stubOk("/second-test") | ||
val (request, response, result) = "http://localhost:9001/second-test".httpGet().response() | ||
|
||
response.statusCode shouldBe 200 | ||
} | ||
}) | ||
|
56 changes: 9 additions & 47 deletions
56
src/test/kotlin/io/kotest/extensions/wiremock/WiremockListenerPerSpecTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,28 @@ | ||
package io.kotest.extensions.wiremock | ||
|
||
import com.github.kittinunf.fuel.httpGet | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.client.WireMock | ||
import io.kotest.assertions.throwables.shouldNotThrowAny | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.listeners.TestListener | ||
import io.kotest.core.spec.Spec | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.test.TestCase | ||
import io.kotest.core.test.TestResult | ||
import io.kotest.matchers.shouldBe | ||
import java.net.ConnectException | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
@Suppress("BlockingMethodInNonBlockingContext") | ||
class WiremockListenerPerSpecTest : FunSpec({ | ||
val wireMockServer = WireMockServer(9000) | ||
|
||
listener(WireMockListener.perSpec(wireMockServer)) | ||
|
||
listener(object : TestListener { | ||
override suspend fun afterTest(testCase: TestCase, result: TestResult) { | ||
wireMockServer.stubFor( | ||
WireMock.get(WireMock.urlEqualTo("/after-test")) | ||
.willReturn(WireMock.ok()) | ||
) | ||
shouldNotThrowAny { | ||
val connection = URL("http://localhost:9000/after-test").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
} | ||
} | ||
|
||
override suspend fun afterSpec(spec: Spec) { | ||
wireMockServer.stubFor( | ||
WireMock.get(WireMock.urlEqualTo("/after-spec")) | ||
.willReturn(WireMock.ok()) | ||
) | ||
|
||
shouldThrow<ConnectException> { | ||
val connection = URL("http://localhost:9000/after-spec").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
} | ||
} | ||
}) | ||
test("should have started wiremock server") { | ||
wireMockServer.stubOk("/test") | ||
|
||
val (request, response, result) = "http://localhost:9000/test".httpGet().response() | ||
|
||
test("should have started wiremock server") { | ||
wireMockServer.stubFor( | ||
WireMock.get(WireMock.urlEqualTo("/test")) | ||
.willReturn(WireMock.ok()) | ||
) | ||
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
response.statusCode shouldBe 200 | ||
} | ||
|
||
test("should have started wiremock server for second test as well") { | ||
wireMockServer.stubFor( | ||
WireMock.get(WireMock.urlEqualTo("/second-test")) | ||
.willReturn(WireMock.ok()) | ||
) | ||
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
wireMockServer.stubOk("/second-test") | ||
|
||
val (request, response, result) = "http://localhost:9000/second-test".httpGet().response() | ||
response.statusCode shouldBe 200 | ||
} | ||
}) |
42 changes: 13 additions & 29 deletions
42
src/test/kotlin/io/kotest/extensions/wiremock/WiremockListenerPerTestTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,31 @@ | ||
package io.kotest.extensions.wiremock | ||
|
||
import com.github.kittinunf.fuel.core.isSuccessful | ||
import com.github.kittinunf.fuel.httpGet | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.client.WireMock.* | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.listeners.TestListener | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.test.TestCase | ||
import io.kotest.core.test.TestResult | ||
import io.kotest.matchers.shouldBe | ||
import java.net.ConnectException | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
@Suppress("BlockingMethodInNonBlockingContext") | ||
class WiremockListenerPerTestTest : FunSpec({ | ||
val wireMockServer = WireMockServer(9000) | ||
|
||
listener(WireMockListener.perTest(wireMockServer)) | ||
listener(object : TestListener { | ||
override suspend fun afterTest(testCase: TestCase, result: TestResult) { | ||
shouldThrow<ConnectException> { | ||
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection | ||
connection.responseCode | ||
} | ||
} | ||
}) | ||
|
||
afterSpec { | ||
val (request, response, result) = "http://localhost:9000/test".httpGet().response() | ||
response.isSuccessful shouldBe false | ||
} | ||
|
||
test("should have started wiremock server") { | ||
wireMockServer.stubFor( | ||
get(urlEqualTo("/test")) | ||
.willReturn(ok()) | ||
) | ||
val connection = URL("http://localhost:9000/test").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
wireMockServer.stubOk("/test") | ||
|
||
val (request, response, result) = "http://localhost:9000/test".httpGet().response() | ||
response.statusCode shouldBe 200 | ||
} | ||
|
||
test("should have started wiremock server for second test as well") { | ||
wireMockServer.stubFor( | ||
get(urlEqualTo("/second-test")) | ||
.willReturn(ok()) | ||
) | ||
val connection = URL("http://localhost:9000/second-test").openConnection() as HttpURLConnection | ||
connection.responseCode shouldBe 200 | ||
wireMockServer.stubOk("/second-test") | ||
val (request, response, result) = "http://localhost:9000/second-test".httpGet().response() | ||
response.statusCode shouldBe 200 | ||
} | ||
}) |