Skip to content

Commit

Permalink
Endpoint path matching enhanced with multiple possible matching patte…
Browse files Browse the repository at this point in the history
…rns (#21)

* Endpoint path matching enhanced with multiple possible matching patterns

* PatternMatcher replaced with usual regular expression usage
  • Loading branch information
jmanriquehiberus authored Oct 23, 2023
1 parent cb91137 commit 4d0c817
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
3 changes: 1 addition & 2 deletions mock/src/main/java/com/telefonica/mock/MockHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.telefonica.mock
import android.content.Context
import com.telefonica.mock.di.DaggerMockComponent
import com.telefonica.mock.di.MockApiModule
import java.net.InetAddress
import javax.inject.Inject

class MockHelper(context: Context) {
Expand Down Expand Up @@ -42,7 +41,7 @@ class MockHelper(context: Context) {
class EnqueuingContext(val mockHelper: MockHelper) {
fun whenever(
path: Path,
method: Method = Method.Get,
method: Method = Method.Get
): MockResponseBuilderWithRequestInfo = MockResponseBuilderWithRequestInfo(mockHelper, RequestInfo(path, method))
}

Expand Down
1 change: 0 additions & 1 deletion mock/src/main/java/com/telefonica/mock/MockedServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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
Expand Down
7 changes: 3 additions & 4 deletions mock/src/main/java/com/telefonica/mock/ResponseDispatcher.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.telefonica.mock

import android.os.PatternMatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.SocketPolicy
import java.util.*
Expand All @@ -15,7 +14,9 @@ class ResponseDispatcher @Inject constructor() {

fun dispatch(recordedMethod: String?, recordedPath: String?): MockResponse {
val responseList = responses
.filterKeys { requestInfo -> requestInfo.method.value == recordedMethod && requestInfo.path.toPattern().match(recordedPath) }
.filterKeys { requestInfo ->
requestInfo.method.value == recordedMethod && !recordedPath.isNullOrEmpty() && Regex(requestInfo.path).matches(recordedPath)
}
.entries
.firstOrNull()
?.value
Expand Down Expand Up @@ -44,6 +45,4 @@ class ResponseDispatcher @Inject constructor() {
val mockListUpdated = (responses[requestInfo] ?: LinkedList()).apply { add(mockedResponse) }
responses[requestInfo] = mockListUpdated
}

private fun Path.toPattern(): PatternMatcher = PatternMatcher(this, PatternMatcher.PATTERN_SIMPLE_GLOB)
}

0 comments on commit 4d0c817

Please sign in to comment.