From c88e769013bfc0b622b86b9e6ff609aa02643c13 Mon Sep 17 00:00:00 2001 From: Harshit Bangar Date: Mon, 6 Jun 2022 12:40:55 -0700 Subject: [PATCH] Prototype: Added support for multiple host in mock (#3762) Summary: Apps usually communicate with multiple hosts to support pops and data centres. The problem currently is that for each host we need a different config in the mock tab in flipper. If the host change due to failover then the mocking stops working. Added support to enter the URL without a host in the flipper. We then check if the Url from the actual network request contains the mock URL without a host. We can change the hint on flipper UI to suggest either entering the complete URL or without the host to allow matching across multiple hosts. Resolve:[3751](https://github.com/facebook/flipper/issues/3751) Let me know if the approach looks good. ## Changelog Pull Request resolved: https://github.com/facebook/flipper/pull/3762 Reviewed By: lblasa Differential Revision: D36780307 Pulled By: passy fbshipit-source-id: 744898fa24d13343132e9a2165750241861245bd --- .../network/FlipperOkhttpInterceptor.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java index 7d09d19bc..9087ff19f 100644 --- a/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java +++ b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/FlipperOkhttpInterceptor.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.UUID; import javax.annotation.Nullable; @@ -192,10 +193,7 @@ public class FlipperOkhttpInterceptor final String method = request.method(); final PartialRequestInfo partialRequest = new PartialRequestInfo(url, method); - if (!mMockResponseMap.containsKey(partialRequest)) { - return null; - } - ResponseInfo mockResponse = mMockResponseMap.get(partialRequest); + ResponseInfo mockResponse = getMockResponse(partialRequest); if (mockResponse == null) { return null; } @@ -219,6 +217,17 @@ public class FlipperOkhttpInterceptor return builder.build(); } + private ResponseInfo getMockResponse(PartialRequestInfo partialRequestInfo) { + for (Map.Entry entry : mMockResponseMap.entrySet()) { + PartialRequestInfo mockRequestInfo = entry.getKey(); + if (partialRequestInfo.first.contains(mockRequestInfo.first) + && Objects.equals(partialRequestInfo.second, mockRequestInfo.second)) { + return entry.getValue(); + } + } + return null; + } + @Nullable private ResponseInfo convertFlipperObjectRouteToResponseInfo(FlipperObject route) { final String data = route.getString("data");