Issue#13 Use any available port for mockwebserver in core tests

This commit is contained in:
Gergely Hegedus 2022-01-26 22:35:27 +02:00
parent 8d33f77cec
commit 9bdcaddb0c
2 changed files with 4 additions and 7 deletions

View file

@ -20,13 +20,13 @@ class MockServerScenarioSetup internal constructor(
var clientCertificates: HandshakeCertificates? = null var clientCertificates: HandshakeCertificates? = null
private set private set
fun start(useHttps: Boolean) { fun start(useHttps: Boolean): String {
val mockWebServer = MockWebServer().also { this.mockWebServer = it } val mockWebServer = MockWebServer().also { this.mockWebServer = it }
if (useHttps) { if (useHttps) {
clientCertificates = mockWebServer.useHttps() clientCertificates = mockWebServer.useHttps()
} }
mockWebServer.dispatcher = networkDispatcher mockWebServer.dispatcher = networkDispatcher
mockWebServer.start(InetAddress.getLocalHost(), PORT) return mockWebServer.url("/").toString()
} }
/** /**
@ -69,9 +69,6 @@ class MockServerScenarioSetup internal constructor(
} }
companion object { companion object {
const val PORT: Int = 7335
val HTTP_BASE_URL get() = "http://${InetAddress.getLocalHost().canonicalHostName}"
val HTTPS_BASE_URL get() = "https://localhost"
private fun MockWebServer.useHttps(): HandshakeCertificates { private fun MockWebServer.useHttps(): HandshakeCertificates {
val localhost = InetAddress.getByName("localhost").canonicalHostName val localhost = InetAddress.getByName("localhost").canonicalHostName

View file

@ -7,12 +7,12 @@ import org.junit.jupiter.api.extension.ExtensionContext
class MockServerScenarioSetupExtensions : BeforeEachCallback, AfterEachCallback { class MockServerScenarioSetupExtensions : BeforeEachCallback, AfterEachCallback {
val url: String = "${MockServerScenarioSetup.HTTP_BASE_URL}:${MockServerScenarioSetup.PORT}/" lateinit var url: String
lateinit var mockServerScenarioSetup: MockServerScenarioSetup lateinit var mockServerScenarioSetup: MockServerScenarioSetup
override fun beforeEach(context: ExtensionContext?) { override fun beforeEach(context: ExtensionContext?) {
mockServerScenarioSetup = MockServerScenarioSetup() mockServerScenarioSetup = MockServerScenarioSetup()
mockServerScenarioSetup.start(false) url = mockServerScenarioSetup.start(false)
} }
override fun afterEach(context: ExtensionContext?) { override fun afterEach(context: ExtensionContext?) {