Add HTTP Status Code to mock response for Route (#1441)

Summary:
Mock requests can currently only return an HTTP Status Code of 200.  This is not sufficient to take full advantage of the mocking feature.  It would be more useful to specify a status code for the response.  Then not only can the original request be tested, but failure of the call could be tested as well.

This is described in Issue https://github.com/facebook/flipper/issues/1423 [https://github.com/facebook/flipper/issues/1423]

Changelog: Allow user to change response code for a mock request

Pull Request resolved: https://github.com/facebook/flipper/pull/1441

Test Plan:
Here is a video demonstrating the change
[Uploading response-status-code.mp4.zip…]

Here is a screenshot showing where the new HTTP status code is entered for the mock request:
![image](https://user-images.githubusercontent.com/337874/89370139-a1c1c500-d6a5-11ea-9a55-e5e99ba37af5.png)

This screenshot shows a mock response with an alternate return code:
![image](https://user-images.githubusercontent.com/337874/89370265-ecdbd800-d6a5-11ea-82e7-10afbd5dd939.png)

Reviewed By: jknoxville

Differential Revision: D22977811

Pulled By: passy

fbshipit-source-id: c1662dd02abeb4546c80a416ed87f8e0dadbf96a
This commit is contained in:
James Harmon
2020-08-07 09:25:38 -07:00
committed by Facebook GitHub Bot
parent 5f8ba09534
commit 6989fa608d
4 changed files with 18 additions and 1 deletions

View File

@@ -228,10 +228,12 @@ public class FlipperOkhttpInterceptor
if (TextUtils.isEmpty(requestUrl) || TextUtils.isEmpty(method)) { if (TextUtils.isEmpty(requestUrl) || TextUtils.isEmpty(method)) {
return null; return null;
} }
final int statusCode = route.getInt("status");
final ResponseInfo mockResponse = new ResponseInfo(); final ResponseInfo mockResponse = new ResponseInfo();
mockResponse.body = data.getBytes(); mockResponse.body = data.getBytes();
mockResponse.statusCode = HttpURLConnection.HTTP_OK; mockResponse.statusCode = HttpURLConnection.HTTP_OK;
mockResponse.statusCode = statusCode;
mockResponse.statusReason = "OK"; mockResponse.statusReason = "OK";
if (headersArray != null) { if (headersArray != null) {
final List<NetworkReporter.Header> headers = new ArrayList<>(); final List<NetworkReporter.Header> headers = new ArrayList<>();

View File

@@ -239,7 +239,7 @@ export function MockResponseDetails({id, route, isDuplicated}: Props) {
); );
const [nextHeaderId, setNextHeaderId] = useState(0); const [nextHeaderId, setNextHeaderId] = useState(0);
const {requestUrl, requestMethod, responseData} = route; const {requestUrl, requestMethod, responseData, responseStatus} = route;
return ( return (
<Container> <Container>
<FlexRow style={{width: '100%'}}> <FlexRow style={{width: '100%'}}>
@@ -264,6 +264,18 @@ export function MockResponseDetails({id, route, isDuplicated}: Props) {
} }
/> />
</FlexRow> </FlexRow>
<FlexRow style={{width: '20%'}}>
<StyledInput
type="text"
placeholder="STATUS"
value={responseStatus}
onChange={(event) =>
networkRouteManager.modifyRoute(id, {
responseStatus: event.target.value,
})
}
/>
</FlexRow>
{isDuplicated && ( {isDuplicated && (
<Warning> <Warning>
<Glyph name="caution-triangle" color={colors.yellow} /> <Glyph name="caution-triangle" color={colors.yellow} />

View File

@@ -235,6 +235,7 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
requestMethod: 'GET', requestMethod: 'GET',
responseData: '', responseData: '',
responseHeaders: {}, responseHeaders: {},
responseStatus: '200',
}; };
draftState.nextRouteId = nextRouteId + 1; draftState.nextRouteId = nextRouteId + 1;
}), }),
@@ -354,6 +355,7 @@ export default class extends FlipperPlugin<State, any, PersistedState> {
method: route.requestMethod, method: route.requestMethod,
data: route.responseData, data: route.responseData,
headers: [...Object.values(route.responseHeaders)], headers: [...Object.values(route.responseHeaders)],
status: route.responseStatus,
})), })),
}); });
} }

View File

@@ -60,4 +60,5 @@ export type Route = {
requestMethod: string; requestMethod: string;
responseData: string; responseData: string;
responseHeaders: {[id: string]: Header}; responseHeaders: {[id: string]: Header};
responseStatus: string;
}; };