(Client) Network Response Mocking Logic for Android Clients (Original PR) (#488)

Summary:
Add logic on client side

# How it works (from the code)
1. Server side sends request url and method to response data and headers to client side
   1.1. This will happen every time server update **any** mock response (add, edit, and remove)
2. Client stores those in map
3. For every network request,
   3.1. Check if there is a matching url and method
   3.2. If so, create a new response with the data and headers and drop the request
   3.3. If not, proceed and send the request and wait for a response

`addNetworkInterceptor` is changed to `addInterceptor` to allow short-circuit and proceed without fetching anything. More info can be found at https://square.github.io/okhttp/interceptors/

Note:
- This is an original PR.
- The content below is from original PR

Add network response mocking for Network plugin. See discussion [here](https://github.com/facebook/flipper/issues/475)

## Changelog
- Add Network response mocking, currently support Android clients only
- Change the Android example app to use `addInterceptor()` instead of `addNetworkInterceptor()`
Pull Request resolved: https://github.com/facebook/flipper/pull/488

Test Plan:
{F231673798}

![60549983-187ce800-9d59-11e9-8f7a-4b1b6402653d](https://user-images.githubusercontent.com/410850/61124971-0c242800-a4db-11e9-8e11-8a0a45bbb621.gif)

- Connect an Android device
- Tap on Network plugin
- Click on the Mock button
- Click on Add Route button, and specify the URL
- Edit the mock data in the text area
- Optionally, click the Headers tab to edit the headers data
- Click close button to close the dialog
- Send some network data in your application. You should be able to see the mock data appears in the Network table in those rows highlighted in yellow

Reviewed By: passy

Differential Revision: D16580291

Pulled By: cekkaewnumchai

fbshipit-source-id: fc391f5e7efebc6f51a72b00d16263e009e1fdb0
This commit is contained in:
Qichuan (Sean) ZHANG
2020-03-23 21:57:08 -07:00
committed by Facebook GitHub Bot
parent 4ea1497387
commit d27e45d7bb
2 changed files with 147 additions and 4 deletions

View File

@@ -36,7 +36,7 @@ public final class FlipperInitializer {
final DescriptorMapping descriptorMapping = DescriptorMapping.withDefaults();
final NetworkFlipperPlugin networkPlugin = new NetworkFlipperPlugin();
final FlipperOkhttpInterceptor interceptor = new FlipperOkhttpInterceptor(networkPlugin);
final FlipperOkhttpInterceptor interceptor = new FlipperOkhttpInterceptor(networkPlugin, true);
// Normally, you would want to make this dependent on a BuildConfig flag, but
// for this demo application we can safely assume that you always want to debug.
@@ -60,7 +60,7 @@ public final class FlipperInitializer {
final OkHttpClient okHttpClient =
new OkHttpClient.Builder()
.addNetworkInterceptor(interceptor)
.addInterceptor(interceptor)
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.MINUTES)