From 4d0c817ba0111600bf75bcb86613f26731d5fdc9 Mon Sep 17 00:00:00 2001 From: Javier Manrique Pellejero Date: Mon, 23 Oct 2023 13:24:07 +0200 Subject: [PATCH] Endpoint path matching enhanced with multiple possible matching patterns (#21) * Endpoint path matching enhanced with multiple possible matching patterns * PatternMatcher replaced with usual regular expression usage --- mock/src/main/java/com/telefonica/mock/MockHelper.kt | 3 +-- mock/src/main/java/com/telefonica/mock/MockedServer.kt | 1 - .../main/java/com/telefonica/mock/ResponseDispatcher.kt | 7 +++---- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/mock/src/main/java/com/telefonica/mock/MockHelper.kt b/mock/src/main/java/com/telefonica/mock/MockHelper.kt index 06bf308..961bf80 100644 --- a/mock/src/main/java/com/telefonica/mock/MockHelper.kt +++ b/mock/src/main/java/com/telefonica/mock/MockHelper.kt @@ -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) { @@ -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)) } diff --git a/mock/src/main/java/com/telefonica/mock/MockedServer.kt b/mock/src/main/java/com/telefonica/mock/MockedServer.kt index 94d99e5..79df7ce 100644 --- a/mock/src/main/java/com/telefonica/mock/MockedServer.kt +++ b/mock/src/main/java/com/telefonica/mock/MockedServer.kt @@ -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 diff --git a/mock/src/main/java/com/telefonica/mock/ResponseDispatcher.kt b/mock/src/main/java/com/telefonica/mock/ResponseDispatcher.kt index 780846c..1a3654e 100644 --- a/mock/src/main/java/com/telefonica/mock/ResponseDispatcher.kt +++ b/mock/src/main/java/com/telefonica/mock/ResponseDispatcher.kt @@ -1,6 +1,5 @@ package com.telefonica.mock -import android.os.PatternMatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.SocketPolicy import java.util.* @@ -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 @@ -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) } \ No newline at end of file