From cb49877a25bac9e72de85f6703eba7f07a308061 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Mon, 11 Dec 2017 17:57:56 -0800 Subject: [PATCH] Backed out changeset 4fe617216293 Reviewed By: fred2028 Differential Revision: D6539205 fbshipit-source-id: c97d4f3dbd457f59968991b043d7106e551effad --- .../modules/network/NetworkingModule.java | 63 +++++++++---------- .../modules/network/NetworkingModuleTest.java | 9 +-- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java index 58fd605459397c..297cd1ef4ff2fc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java @@ -235,9 +235,8 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) { String contentEncoding = requestHeaders.get(CONTENT_ENCODING_HEADER_NAME); requestBuilder.headers(requestHeaders); - RequestBody requestBody; if (data == null) { - requestBody = RequestBodyUtil.getEmptyBody(method); + requestBuilder.method(method, RequestBodyUtil.getEmptyBody(method)); } else if (data.hasKey(REQUEST_BODY_KEY_STRING)) { if (contentType == null) { ResponseUtil.onRequestError( @@ -250,13 +249,14 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) { String body = data.getString(REQUEST_BODY_KEY_STRING); MediaType contentMediaType = MediaType.parse(contentType); if (RequestBodyUtil.isGzipEncoding(contentEncoding)) { - requestBody = RequestBodyUtil.createGzip(contentMediaType, body); + RequestBody requestBody = RequestBodyUtil.createGzip(contentMediaType, body); if (requestBody == null) { ResponseUtil.onRequestError(eventEmitter, requestId, "Failed to gzip request body", null); return; } + requestBuilder.method(method, requestBody); } else { - requestBody = RequestBody.create(contentMediaType, body); + requestBuilder.method(method, RequestBody.create(contentMediaType, body)); } } else if (data.hasKey(REQUEST_BODY_KEY_BASE64)) { if (contentType == null) { @@ -269,7 +269,9 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) { } String base64String = data.getString(REQUEST_BODY_KEY_BASE64); MediaType contentMediaType = MediaType.parse(contentType); - requestBody = RequestBody.create(contentMediaType, ByteString.decodeBase64(base64String)); + requestBuilder.method( + method, + RequestBody.create(contentMediaType, ByteString.decodeBase64(base64String))); } else if (data.hasKey(REQUEST_BODY_KEY_URI)) { if (contentType == null) { ResponseUtil.onRequestError( @@ -290,7 +292,9 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) { null); return; } - requestBody = RequestBodyUtil.create(MediaType.parse(contentType), fileInputStream); + requestBuilder.method( + method, + RequestBodyUtil.create(MediaType.parse(contentType), fileInputStream)); } else if (data.hasKey(REQUEST_BODY_KEY_FORMDATA)) { if (contentType == null) { contentType = "multipart/form-data"; @@ -301,16 +305,28 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) { if (multipartBuilder == null) { return; } - requestBody = multipartBuilder.build(); + + requestBuilder.method( + method, + RequestBodyUtil.createProgressRequest( + multipartBuilder.build(), + new ProgressListener() { + long last = System.nanoTime(); + + @Override + public void onProgress(long bytesWritten, long contentLength, boolean done) { + long now = System.nanoTime(); + if (done || shouldDispatch(now, last)) { + ResponseUtil.onDataSend(eventEmitter, requestId, bytesWritten, contentLength); + last = now; + } + } + })); } else { // Nothing in data payload, at least nothing we could understand anyway. - requestBody = RequestBodyUtil.getEmptyBody(method); + requestBuilder.method(method, RequestBodyUtil.getEmptyBody(method)); } - requestBuilder.method( - method, - wrapRequestBodyWithProgressEmitter(requestBody, eventEmitter, requestId)); - addRequest(requestId); client.newCall(requestBuilder.build()).enqueue( new Callback() { @@ -378,29 +394,6 @@ public void onResponse(Call call, Response response) throws IOException { }); } - private RequestBody wrapRequestBodyWithProgressEmitter( - final RequestBody requestBody, - final RCTDeviceEventEmitter eventEmitter, - final int requestId) { - if(requestBody == null) { - return null; - } - return RequestBodyUtil.createProgressRequest( - requestBody, - new ProgressListener() { - long last = System.nanoTime(); - - @Override - public void onProgress(long bytesWritten, long contentLength, boolean done) { - long now = System.nanoTime(); - if (done || shouldDispatch(now, last)) { - ResponseUtil.onDataSend(eventEmitter, requestId, bytesWritten, contentLength); - last = now; - } - } - }); - } - private void readWithProgress( RCTDeviceEventEmitter eventEmitter, int requestId, diff --git a/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java b/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java index 8ad39cd16e2070..7b512bc252a5ed 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java @@ -200,10 +200,6 @@ public WritableMap answer(InvocationOnMock invocation) throws Throwable { @Test public void testSuccessfulPostRequest() throws Exception { - RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class); - ReactApplicationContext context = mock(ReactApplicationContext.class); - when(context.getJSModule(any(Class.class))).thenReturn(emitter); - OkHttpClient httpClient = mock(OkHttpClient.class); when(httpClient.newCall(any(Request.class))).thenAnswer(new Answer() { @Override @@ -215,13 +211,12 @@ public Object answer(InvocationOnMock invocation) throws Throwable { OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class); when(clientBuilder.build()).thenReturn(httpClient); when(httpClient.newBuilder()).thenReturn(clientBuilder); - NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient); + NetworkingModule networkingModule = + new NetworkingModule(mock(ReactApplicationContext.class), "", httpClient); JavaOnlyMap body = new JavaOnlyMap(); body.putString("string", "This is request body"); - mockEvents(); - networkingModule.sendRequest( "POST", "http://somedomain/bar",