From 6989fa608d97a33fdba5c1933d984c430ecb075d Mon Sep 17 00:00:00 2001 From: James Harmon Date: Fri, 7 Aug 2020 09:25:38 -0700 Subject: [PATCH] Add HTTP Status Code to mock response for Route (#1441) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../plugins/network/FlipperOkhttpInterceptor.java | 2 ++ desktop/plugins/network/MockResponseDetails.tsx | 14 +++++++++++++- desktop/plugins/network/index.tsx | 2 ++ desktop/plugins/network/types.tsx | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) 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 7b30d7ef7..6f66c9959 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 @@ -228,10 +228,12 @@ public class FlipperOkhttpInterceptor if (TextUtils.isEmpty(requestUrl) || TextUtils.isEmpty(method)) { return null; } + final int statusCode = route.getInt("status"); final ResponseInfo mockResponse = new ResponseInfo(); mockResponse.body = data.getBytes(); mockResponse.statusCode = HttpURLConnection.HTTP_OK; + mockResponse.statusCode = statusCode; mockResponse.statusReason = "OK"; if (headersArray != null) { final List headers = new ArrayList<>(); diff --git a/desktop/plugins/network/MockResponseDetails.tsx b/desktop/plugins/network/MockResponseDetails.tsx index d800c4495..166e222d5 100644 --- a/desktop/plugins/network/MockResponseDetails.tsx +++ b/desktop/plugins/network/MockResponseDetails.tsx @@ -239,7 +239,7 @@ export function MockResponseDetails({id, route, isDuplicated}: Props) { ); const [nextHeaderId, setNextHeaderId] = useState(0); - const {requestUrl, requestMethod, responseData} = route; + const {requestUrl, requestMethod, responseData, responseStatus} = route; return ( @@ -264,6 +264,18 @@ export function MockResponseDetails({id, route, isDuplicated}: Props) { } /> + + + networkRouteManager.modifyRoute(id, { + responseStatus: event.target.value, + }) + } + /> + {isDuplicated && ( diff --git a/desktop/plugins/network/index.tsx b/desktop/plugins/network/index.tsx index 71d454e6e..871d13736 100644 --- a/desktop/plugins/network/index.tsx +++ b/desktop/plugins/network/index.tsx @@ -235,6 +235,7 @@ export default class extends FlipperPlugin { requestMethod: 'GET', responseData: '', responseHeaders: {}, + responseStatus: '200', }; draftState.nextRouteId = nextRouteId + 1; }), @@ -354,6 +355,7 @@ export default class extends FlipperPlugin { method: route.requestMethod, data: route.responseData, headers: [...Object.values(route.responseHeaders)], + status: route.responseStatus, })), }); } diff --git a/desktop/plugins/network/types.tsx b/desktop/plugins/network/types.tsx index 7622b539b..836af26eb 100644 --- a/desktop/plugins/network/types.tsx +++ b/desktop/plugins/network/types.tsx @@ -60,4 +60,5 @@ export type Route = { requestMethod: string; responseData: string; responseHeaders: {[id: string]: Header}; + responseStatus: string; };