Skip to content

Commit

Permalink
Backed out changeset 4fe617216293
Browse files Browse the repository at this point in the history
Reviewed By: fred2028

Differential Revision: D6539205

fbshipit-source-id: c97d4f3dbd457f59968991b043d7106e551effad
  • Loading branch information
RSNara authored and facebook-github-bot committed Dec 12, 2017
1 parent 60dc9be commit cb49877
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -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";
Expand All @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object>() {
@Override
Expand All @@ -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",
Expand Down

3 comments on commit cb49877

@janicduplessis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RSNara Why was this reverted?

@fengyibo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ? I am waiting for #16541 in stable version of RN !

@RSNara
Copy link
Contributor Author

@RSNara RSNara commented on cb49877 Dec 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janicduplessis. I just commented on PR 16541 explaining why.

Please sign in to comment.