Network plugin - support mocks in imported devices (#2040)
Summary: It is not currently possible to create mock routes from imported network logs. This PR will provide that functionality. See this issue for more details: https://github.com/facebook/flipper/issues/1988 ## Changelog Network plugin - create mocks from imported network logs Pull Request resolved: https://github.com/facebook/flipper/pull/2040 Test Plan: Use sample app to create network activity Export network activity Import network activity Create mocks from imported network activity Verify that mocks work using sample app Reviewed By: mweststrate Differential Revision: D26947187 Pulled By: passy fbshipit-source-id: 5e4e0197c49bb7a8227a70e574613381815e6d30
This commit is contained in:
committed by
Facebook GitHub Bot
parent
07defebb22
commit
f61b5a1a86
@@ -44,7 +44,13 @@ import {clipboard} from 'electron';
|
|||||||
import {URL} from 'url';
|
import {URL} from 'url';
|
||||||
import {MockResponseDialog} from './MockResponseDialog';
|
import {MockResponseDialog} from './MockResponseDialog';
|
||||||
import {combineBase64Chunks} from './chunks';
|
import {combineBase64Chunks} from './chunks';
|
||||||
import {PluginClient, createState, usePlugin, useValue} from 'flipper-plugin';
|
import {
|
||||||
|
PluginClient,
|
||||||
|
Device,
|
||||||
|
createState,
|
||||||
|
usePlugin,
|
||||||
|
useValue,
|
||||||
|
} from 'flipper-plugin';
|
||||||
import {remote, OpenDialogOptions} from 'electron';
|
import {remote, OpenDialogOptions} from 'electron';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import electron from 'electron';
|
import electron from 'electron';
|
||||||
@@ -325,8 +331,16 @@ export function plugin(client: PluginClient<Events, Methods>) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function supportsMocks(device: Device): Promise<boolean> {
|
||||||
|
if (device.isArchived) {
|
||||||
|
return Promise.resolve(true);
|
||||||
|
} else {
|
||||||
|
return client.supportsMethod('mockResponses');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
client.supportsMethod('mockResponses').then((result) => {
|
supportsMocks(client.device).then((result) => {
|
||||||
const newRoutes = JSON.parse(
|
const newRoutes = JSON.parse(
|
||||||
localStorage.getItem(LOCALSTORAGE_MOCK_ROUTE_LIST_KEY + client.appId) ||
|
localStorage.getItem(LOCALSTORAGE_MOCK_ROUTE_LIST_KEY + client.appId) ||
|
||||||
'{}',
|
'{}',
|
||||||
@@ -552,21 +566,23 @@ export function plugin(client: PluginClient<Events, Methods>) {
|
|||||||
JSON.stringify(routesValuesArray),
|
JSON.stringify(routesValuesArray),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
if (!client.device.isArchived) {
|
||||||
await client.send('mockResponses', {
|
try {
|
||||||
routes: routesValuesArray
|
await client.send('mockResponses', {
|
||||||
.filter((e) => e.enabled)
|
routes: routesValuesArray
|
||||||
.map((route: Route) => ({
|
.filter((e) => e.enabled)
|
||||||
requestUrl: route.requestUrl,
|
.map((route: Route) => ({
|
||||||
method: route.requestMethod,
|
requestUrl: route.requestUrl,
|
||||||
data: route.responseData,
|
method: route.requestMethod,
|
||||||
headers: [...Object.values(route.responseHeaders)],
|
data: route.responseData,
|
||||||
status: route.responseStatus,
|
headers: [...Object.values(route.responseHeaders)],
|
||||||
enabled: route.enabled,
|
status: route.responseStatus,
|
||||||
})),
|
enabled: route.enabled,
|
||||||
});
|
})),
|
||||||
} catch (e) {
|
});
|
||||||
console.error('Failed to mock responses.', e);
|
} catch (e) {
|
||||||
|
console.error('Failed to mock responses.', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user