Backed out changeset 4fe617216293

Reviewed By: fred2028

Differential Revision: D6539205

fbshipit-source-id: c97d4f3dbd457f59968991b043d7106e551effad
This commit is contained in:
Ramanpreet Nara 2017-12-11 17:57:56 -08:00 committed by Facebook Github Bot
parent 60dc9bed00
commit cb49877a25
2 changed files with 30 additions and 42 deletions

View File

@ -235,9 +235,8 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
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 final class NetworkingModule extends ReactContextBaseJavaModule {
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 final class NetworkingModule extends ReactContextBaseJavaModule {
}
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 final class NetworkingModule extends ReactContextBaseJavaModule {
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 final class NetworkingModule extends ReactContextBaseJavaModule {
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 final class NetworkingModule extends ReactContextBaseJavaModule {
});
}
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,

View File

@ -200,10 +200,6 @@ public class NetworkingModuleTest {
@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<Object>() {
@Override
@ -215,13 +211,12 @@ public class NetworkingModuleTest {
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",