-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing a bug that was causing that a response with a different method (…
…#19) * Fixing a bug that was causing that a response with a different method was returned. * Commited by mistake
- Loading branch information
1 parent
c5882e1
commit e06d83d
Showing
6 changed files
with
130 additions
and
125 deletions.
There are no files selected for viewing
116 changes: 0 additions & 116 deletions
116
mock/src/main/java/com/telefonica/mock/MockApiClient.kt
This file was deleted.
Oops, something went wrong.
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,68 @@ | ||
package com.telefonica.mock | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import okhttp3.mockwebserver.Dispatcher | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import okhttp3.mockwebserver.RecordedRequest | ||
import java.net.InetAddress | ||
import okhttp3.tls.HandshakeCertificates | ||
import okhttp3.tls.HeldCertificate | ||
import javax.inject.Inject | ||
|
||
open class MockedServer @Inject constructor( | ||
private val coroutineDispatcher: CoroutineDispatcher, | ||
private val mockWebServer: MockWebServer, | ||
private val responseDispatcher: ResponseDispatcher, | ||
) { | ||
|
||
private val dispatcher = object : Dispatcher() { | ||
override fun dispatch(request: RecordedRequest): MockResponse = responseDispatcher.dispatch( | ||
recordedMethod = request.method, | ||
recordedPath = request.path | ||
) | ||
} | ||
|
||
internal suspend fun startServer(inetAddress: InetAddress, port: Int = 0) { | ||
withContext(coroutineDispatcher) { | ||
runCatching { | ||
mockWebServer.start(inetAddress = inetAddress, port = port) | ||
} | ||
} | ||
} | ||
|
||
fun stopServer() { | ||
mockWebServer.shutdown() | ||
} | ||
|
||
suspend fun getBaseUrl(): String = withContext(coroutineDispatcher) { | ||
mockWebServer.url("/").toString() | ||
} | ||
|
||
internal fun enqueue(requestInfo: RequestInfo, mockedResponse: MockedResponse) { | ||
responseDispatcher.enqueue(requestInfo, mockedResponse) | ||
} | ||
|
||
internal fun setUp(address: InetAddress, enableSsl: Boolean) { | ||
mockWebServer.dispatcher = dispatcher | ||
if (enableSsl) { | ||
enableHttpsFor(mockWebServer, address) | ||
} | ||
} | ||
|
||
private fun enableHttpsFor(mockWebServer: MockWebServer, address: InetAddress) { | ||
val serverCertificates = HandshakeCertificates.Builder() | ||
.heldCertificate(buildCertificate(address.canonicalHostName)) | ||
.build() | ||
mockWebServer.useHttps(serverCertificates.sslSocketFactory(), false) | ||
} | ||
|
||
private fun buildCertificate(altName: String): HeldCertificate = HeldCertificate.Builder() | ||
.addSubjectAlternativeName(altName) | ||
.build() | ||
|
||
companion object { | ||
const val RESPONSE_NOT_FOUND_CODE = 404 | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
mock/src/main/java/com/telefonica/mock/ResponseDispatcher.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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.telefonica.mock | ||
|
||
import android.os.PatternMatcher | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.SocketPolicy | ||
import java.util.* | ||
import java.util.concurrent.* | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class ResponseDispatcher @Inject constructor() { | ||
|
||
private val responses: MutableMap<RequestInfo, LinkedList<MockedResponse>> = mutableMapOf() | ||
|
||
fun dispatch(recordedMethod: String?, recordedPath: String?): MockResponse { | ||
val responseList = responses | ||
.filterKeys { requestInfo -> requestInfo.method.value == recordedMethod && requestInfo.path.toPattern().match(recordedPath) } | ||
.entries | ||
.firstOrNull() | ||
?.value | ||
|
||
return when { | ||
responseList.isNullOrEmpty() -> MockResponse().setResponseCode(MockedServer.RESPONSE_NOT_FOUND_CODE) | ||
else -> { | ||
val response = responseList.poll() | ||
responseList.add(response) | ||
when (response) { | ||
is MockedApiResponse -> MockResponse() | ||
.setResponseCode(response.httpResponseCode) | ||
.setBodyDelay(response.delayInMillis, TimeUnit.MILLISECONDS) | ||
.setBody(response.body) | ||
is MockedBufferedResponse -> MockResponse() | ||
.setResponseCode(response.httpResponseCode) | ||
.setBodyDelay(response.delayInMillis, TimeUnit.MILLISECONDS) | ||
.setBody(response.buffer) | ||
.setSocketPolicy(SocketPolicy.DISCONNECT_AT_END) | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal fun enqueue(requestInfo: RequestInfo, mockedResponse: MockedResponse) { | ||
val mockListUpdated = (responses[requestInfo] ?: LinkedList()).apply { add(mockedResponse) } | ||
responses[requestInfo] = mockListUpdated | ||
} | ||
|
||
private fun Path.toPattern(): PatternMatcher = PatternMatcher(this, PatternMatcher.PATTERN_SIMPLE_GLOB) | ||
} |
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