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
This commit is contained in:
Harshit Bangar
2022-06-06 12:40:55 -07:00
committed by Facebook GitHub Bot
parent b7977a954a
commit c88e769013

View File

@@ -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<PartialRequestInfo, ResponseInfo> 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");