mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-06 22:38:37 +08:00
WebWorkers: Convert NetworkingModule to support web workers
Summary: Converts NetworkingModule to dispatch callbacks to the appropriate ExecutionContext. Reviewed By: lexs Differential Revision: D2932170 fb-gh-sync-id: ac77eec1a176ede4d2257bfd8ddb13153331c8a4 shipit-source-id: ac77eec1a176ede4d2257bfd8ddb13153331c8a4
This commit is contained in:
committed by
Facebook Github Bot 9
parent
532e41105b
commit
9a3f11d3e7
@@ -14,6 +14,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ExecutorToken;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.JavaOnlyArray;
|
||||
@@ -88,13 +89,14 @@ public class NetworkingModuleTest {
|
||||
NetworkingModule networkingModule = new NetworkingModule(null, "", httpClient);
|
||||
|
||||
networkingModule.sendRequest(
|
||||
"GET",
|
||||
"http://somedomain/foo",
|
||||
0,
|
||||
JavaOnlyArray.of(),
|
||||
null,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"GET",
|
||||
"http://somedomain/foo",
|
||||
0,
|
||||
JavaOnlyArray.of(),
|
||||
null,
|
||||
true,
|
||||
0);
|
||||
|
||||
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
|
||||
verify(httpClient).newCall(argumentCaptor.capture());
|
||||
@@ -108,7 +110,7 @@ public class NetworkingModuleTest {
|
||||
public void testFailGetWithInvalidHeadersStruct() throws Exception {
|
||||
RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class);
|
||||
ReactApplicationContext context = mock(ReactApplicationContext.class);
|
||||
when(context.getJSModule(any(Class.class))).thenReturn(emitter);
|
||||
when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter);
|
||||
|
||||
OkHttpClient httpClient = mock(OkHttpClient.class);
|
||||
NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient);
|
||||
@@ -118,13 +120,14 @@ public class NetworkingModuleTest {
|
||||
mockEvents();
|
||||
|
||||
networkingModule.sendRequest(
|
||||
"GET",
|
||||
"http://somedoman/foo",
|
||||
0,
|
||||
JavaOnlyArray.from(invalidHeaders),
|
||||
null,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"GET",
|
||||
"http://somedoman/foo",
|
||||
0,
|
||||
JavaOnlyArray.from(invalidHeaders),
|
||||
null,
|
||||
true,
|
||||
0);
|
||||
|
||||
verifyErrorEmit(emitter, 0);
|
||||
}
|
||||
@@ -133,7 +136,7 @@ public class NetworkingModuleTest {
|
||||
public void testFailPostWithoutContentType() throws Exception {
|
||||
RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class);
|
||||
ReactApplicationContext context = mock(ReactApplicationContext.class);
|
||||
when(context.getJSModule(any(Class.class))).thenReturn(emitter);
|
||||
when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter);
|
||||
|
||||
OkHttpClient httpClient = mock(OkHttpClient.class);
|
||||
NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient);
|
||||
@@ -144,13 +147,14 @@ public class NetworkingModuleTest {
|
||||
mockEvents();
|
||||
|
||||
networkingModule.sendRequest(
|
||||
"POST",
|
||||
"http://somedomain/bar",
|
||||
0,
|
||||
JavaOnlyArray.of(),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"POST",
|
||||
"http://somedomain/bar",
|
||||
0,
|
||||
JavaOnlyArray.of(),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
|
||||
verifyErrorEmit(emitter, 0);
|
||||
}
|
||||
@@ -184,7 +188,7 @@ public class NetworkingModuleTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccessfullPostRequest() throws Exception {
|
||||
public void testSuccessfulPostRequest() throws Exception {
|
||||
OkHttpClient httpClient = mock(OkHttpClient.class);
|
||||
when(httpClient.newCall(any(Request.class))).thenAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
@@ -200,13 +204,14 @@ public class NetworkingModuleTest {
|
||||
body.putString("string", "This is request body");
|
||||
|
||||
networkingModule.sendRequest(
|
||||
"POST",
|
||||
"http://somedomain/bar",
|
||||
0,
|
||||
JavaOnlyArray.of(JavaOnlyArray.of("Content-Type", "text/plain")),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"POST",
|
||||
"http://somedomain/bar",
|
||||
0,
|
||||
JavaOnlyArray.of(JavaOnlyArray.of("Content-Type", "text/plain")),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
|
||||
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
|
||||
verify(httpClient).newCall(argumentCaptor.capture());
|
||||
@@ -237,13 +242,14 @@ public class NetworkingModuleTest {
|
||||
JavaOnlyArray.of("User-Agent", "React test agent/1.0"));
|
||||
|
||||
networkingModule.sendRequest(
|
||||
"GET",
|
||||
"http://someurl/baz",
|
||||
0,
|
||||
JavaOnlyArray.from(headers),
|
||||
null,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"GET",
|
||||
"http://someurl/baz",
|
||||
0,
|
||||
JavaOnlyArray.from(headers),
|
||||
null,
|
||||
true,
|
||||
0);
|
||||
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
|
||||
verify(httpClient).newCall(argumentCaptor.capture());
|
||||
Headers requestHeaders = argumentCaptor.getValue().headers();
|
||||
@@ -284,13 +290,14 @@ public class NetworkingModuleTest {
|
||||
|
||||
NetworkingModule networkingModule = new NetworkingModule(null, "", httpClient);
|
||||
networkingModule.sendRequest(
|
||||
"POST",
|
||||
"http://someurl/uploadFoo",
|
||||
0,
|
||||
new JavaOnlyArray(),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"POST",
|
||||
"http://someurl/uploadFoo",
|
||||
0,
|
||||
new JavaOnlyArray(),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
|
||||
// verify url, method, headers
|
||||
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
|
||||
@@ -342,13 +349,14 @@ public class NetworkingModuleTest {
|
||||
|
||||
NetworkingModule networkingModule = new NetworkingModule(null, "", httpClient);
|
||||
networkingModule.sendRequest(
|
||||
"POST",
|
||||
"http://someurl/uploadFoo",
|
||||
0,
|
||||
JavaOnlyArray.from(headers),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"POST",
|
||||
"http://someurl/uploadFoo",
|
||||
0,
|
||||
JavaOnlyArray.from(headers),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
|
||||
// verify url, method, headers
|
||||
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
|
||||
@@ -437,13 +445,14 @@ public class NetworkingModuleTest {
|
||||
|
||||
NetworkingModule networkingModule = new NetworkingModule(null, "", httpClient);
|
||||
networkingModule.sendRequest(
|
||||
"POST",
|
||||
"http://someurl/uploadFoo",
|
||||
0,
|
||||
JavaOnlyArray.from(headers),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
mock(ExecutorToken.class),
|
||||
"POST",
|
||||
"http://someurl/uploadFoo",
|
||||
0,
|
||||
JavaOnlyArray.from(headers),
|
||||
body,
|
||||
true,
|
||||
0);
|
||||
|
||||
// verify RequestBodyPart for image
|
||||
PowerMockito.verifyStatic(times(1));
|
||||
|
||||
Reference in New Issue
Block a user