Skip to content

Commit

Permalink
Make setup function not suspend (#20)
Browse files Browse the repository at this point in the history
* Make setup function not suspend

* Done Guille comments
  • Loading branch information
DevPabloGarcia authored Sep 11, 2023
1 parent e06d83d commit cb91137
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/telefonica/mocks/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class App : Application() {

if (BuildConfig.DEFAULT_ENVIRONMENT == Environment.DEMO) {
super.onCreate()
mockHelper.setUp(enableSsl = true)
getUserMocksUseCase()
CoroutineScope(Dispatchers.IO).launch {
mockHelper.setUp(enableSsl = true)
getUserMocksUseCase()
initBackendUrl()
}
}
Expand Down
14 changes: 3 additions & 11 deletions mock/src/main/java/com/telefonica/mock/MockHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,12 @@ class MockHelper(context: Context) {

suspend fun getBaseUrl(): String = mockApiClient.getBaseUrl()

suspend fun setUp(
inetAddress: InetAddress = InetAddress.getByName(DEFAULT_HOSTNAME),
fun setUp(
port: Int = 0,
enableSsl: Boolean = false,
) {
mockApiClient.setUp(
address = inetAddress,
enableSsl = enableSsl,
)
mockApiClient.startServer(inetAddress, port)
}

companion object {
const val DEFAULT_HOSTNAME = "localhost"
mockApiClient.setUp(enableSsl = enableSsl)
mockApiClient.startServer(port)
}
}

Expand Down
22 changes: 9 additions & 13 deletions mock/src/main/java/com/telefonica/mock/MockedServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ open class MockedServer @Inject constructor(
)
}

internal suspend fun startServer(inetAddress: InetAddress, port: Int = 0) {
withContext(coroutineDispatcher) {
runCatching {
mockWebServer.start(inetAddress = inetAddress, port = port)
}
}
internal fun startServer(port: Int = 0) {
mockWebServer.start(port = port)
}

fun stopServer() {
Expand All @@ -44,25 +40,25 @@ open class MockedServer @Inject constructor(
responseDispatcher.enqueue(requestInfo, mockedResponse)
}

internal fun setUp(address: InetAddress, enableSsl: Boolean) {
internal fun setUp(enableSsl: Boolean) {
mockWebServer.dispatcher = dispatcher
if (enableSsl) {
enableHttpsFor(mockWebServer, address)
enableHttpsFor(mockWebServer)
}
}

private fun enableHttpsFor(mockWebServer: MockWebServer, address: InetAddress) {
private fun enableHttpsFor(mockWebServer: MockWebServer) {
val serverCertificates = HandshakeCertificates.Builder()
.heldCertificate(buildCertificate(address.canonicalHostName))
.heldCertificate(buildCertificate())
.build()
mockWebServer.useHttps(serverCertificates.sslSocketFactory(), false)
}

private fun buildCertificate(altName: String): HeldCertificate = HeldCertificate.Builder()
.addSubjectAlternativeName(altName)
private fun buildCertificate(): HeldCertificate = HeldCertificate.Builder()
.addSubjectAlternativeName("localhost")
.build()

companion object {
const val RESPONSE_NOT_FOUND_CODE = 404
}
}
}

0 comments on commit cb91137

Please sign in to comment.