@Test public void testFailGetWithInvalidHeadersStruct() throws Exception { RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class); ReactApplicationContext context = mock(ReactApplicationContext.class); when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter); OkHttpClient httpClient = mock(OkHttpClient.class); NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient); List<JavaOnlyArray> invalidHeaders = Arrays.asList(JavaOnlyArray.of("foo")); mockEvents(); networkingModule.sendRequest( mock(ExecutorToken.class), "GET", "http://somedoman/foo", 0, JavaOnlyArray.from(invalidHeaders), null, true, 0); verifyErrorEmit(emitter, 0); }
@Test public void testFailPostWithoutContentType() throws Exception { RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class); ReactApplicationContext context = mock(ReactApplicationContext.class); when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter); OkHttpClient httpClient = mock(OkHttpClient.class); NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient); JavaOnlyMap body = new JavaOnlyMap(); body.putString("string", "This is request body"); mockEvents(); networkingModule.sendRequest( mock(ExecutorToken.class), "POST", "http://somedomain/bar", 0, JavaOnlyArray.of(), body, true, 0); verifyErrorEmit(emitter, 0); }
/** Emits a JS event with the provided name and data if the rect context is initialized */ static void maybeEmitEvent(ReactContext context, String name, Object data) { if (context == null) { throw new IllegalArgumentException( String.format("reactContext is null (calling event: %s)", name)); } if (context.hasActiveCatalystInstance()) { try { context.getJSModule(RCTDeviceEventEmitter.class).emit(name, data); } catch (RuntimeException e) { // the JS bundle hasn't finished executing, so this call is going to be lost. // In the future, we could maybe set something up to queue the call, and then pass them through once // the bundle has finished getting parsed, but for now I am going to just swallow the error. } } }
/** * Override in activity to handle barcode detector updates * * @param barcodes - a list of detected barcodes */ private void onBarcodesUpdated(List<Barcode> barcodes){ if (barcodes.size() > 0) { Barcode result = barcodes.get(0); WritableMap event = Arguments.createMap(); Log.d(TAG, "barcode scanned is " + result.toString()); event.putString("data", result.rawValue); event.putString("type", Integer.toString(result.format)); ReactContext reactContext = (ReactContext) getContext(); reactContext .getJSModule(RCTDeviceEventEmitter.class) .emit("scannerBarcodeRead", event); } }
private void readWithProgress( RCTDeviceEventEmitter eventEmitter, int requestId, ResponseBody responseBody) throws IOException { long totalBytesRead = -1; long contentLength = -1; try { ProgressResponseBody progressResponseBody = (ProgressResponseBody) responseBody; totalBytesRead = progressResponseBody.totalBytesRead(); contentLength = progressResponseBody.contentLength(); } catch (ClassCastException e) { // Ignore } Reader reader = responseBody.charStream(); try { char[] buffer = new char[MAX_CHUNK_SIZE_BETWEEN_FLUSHES]; int read; while ((read = reader.read(buffer)) != -1) { ResponseUtil.onIncrementalDataReceived( eventEmitter, requestId, new String(buffer, 0, read), totalBytesRead, contentLength); } } finally { reader.close(); } }
public static void onDataSend( RCTDeviceEventEmitter eventEmitter, int requestId, long progress, long total) { WritableArray args = Arguments.createArray(); args.pushInt(requestId); args.pushInt((int) progress); args.pushInt((int) total); eventEmitter.emit("didSendNetworkData", args); }
public static void onIncrementalDataReceived( RCTDeviceEventEmitter eventEmitter, int requestId, String data, long progress, long total) { WritableArray args = Arguments.createArray(); args.pushInt(requestId); args.pushString(data); args.pushInt((int) progress); args.pushInt((int) total); eventEmitter.emit("didReceiveNetworkIncrementalData", args); }
public static void onDataReceivedProgress( RCTDeviceEventEmitter eventEmitter, int requestId, long progress, long total) { WritableArray args = Arguments.createArray(); args.pushInt(requestId); args.pushInt((int) progress); args.pushInt((int) total); eventEmitter.emit("didReceiveNetworkDataProgress", args); }
public static void onDataReceived( RCTDeviceEventEmitter eventEmitter, int requestId, String data) { WritableArray args = Arguments.createArray(); args.pushInt(requestId); args.pushString(data); eventEmitter.emit("didReceiveNetworkData", args); }
public static void onRequestError( RCTDeviceEventEmitter eventEmitter, int requestId, String error, IOException e) { WritableArray args = Arguments.createArray(); args.pushInt(requestId); args.pushString(error); if ((e != null) && (e.getClass() == SocketTimeoutException.class)) { args.pushBoolean(true); // last argument is a time out boolean } eventEmitter.emit("didCompleteNetworkResponse", args); }
public static void onRequestSuccess(RCTDeviceEventEmitter eventEmitter, int requestId) { WritableArray args = Arguments.createArray(); args.pushInt(requestId); args.pushNull(); eventEmitter.emit("didCompleteNetworkResponse", args); }
public static void onResponseReceived( RCTDeviceEventEmitter eventEmitter, int requestId, int statusCode, WritableMap headers, String url) { WritableArray args = Arguments.createArray(); args.pushInt(requestId); args.pushInt(statusCode); args.pushMap(headers); args.pushString(url); eventEmitter.emit("didReceiveNetworkResponse", args); }
@Test public void testFailGetWithInvalidHeadersStruct() throws Exception { RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class); ReactApplicationContext context = mock(ReactApplicationContext.class); when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter); OkHttpClient httpClient = mock(OkHttpClient.class); OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class); when(clientBuilder.build()).thenReturn(httpClient); when(httpClient.newBuilder()).thenReturn(clientBuilder); NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient); List<JavaOnlyArray> invalidHeaders = Arrays.asList(JavaOnlyArray.of("foo")); mockEvents(); networkingModule.sendRequest( mock(ExecutorToken.class), "GET", "http://somedoman/foo", /* requestId */ 0, /* headers */ JavaOnlyArray.from(invalidHeaders), /* body */ null, /* responseType */ "text", /* useIncrementalUpdates*/ true, /* timeout */ 0, /* withCredentials */ false); verifyErrorEmit(emitter, 0); }
@Test public void testFailPostWithoutContentType() throws Exception { RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class); ReactApplicationContext context = mock(ReactApplicationContext.class); when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter); OkHttpClient httpClient = mock(OkHttpClient.class); OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class); when(clientBuilder.build()).thenReturn(httpClient); when(httpClient.newBuilder()).thenReturn(clientBuilder); NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient); JavaOnlyMap body = new JavaOnlyMap(); body.putString("string", "This is request body"); mockEvents(); networkingModule.sendRequest( mock(ExecutorToken.class), "POST", "http://somedomain/bar", 0, JavaOnlyArray.of(), body, /* responseType */ "text", /* useIncrementalUpdates*/ true, /* timeout */ 0, /* withCredentials */ false); verifyErrorEmit(emitter, 0); }
private static void verifyErrorEmit(RCTDeviceEventEmitter emitter, int requestId) { ArgumentCaptor<WritableArray> captor = ArgumentCaptor.forClass(WritableArray.class); verify(emitter).emit(eq("didCompleteNetworkResponse"), captor.capture()); WritableArray array = captor.getValue(); assertThat(array.getInt(0)).isEqualTo(requestId); assertThat(array.getString(1)).isNotNull(); }
@Test public void testFailGetWithInvalidHeadersStruct() throws Exception { RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class); ReactApplicationContext context = mock(ReactApplicationContext.class); when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter); OkHttpClient httpClient = mock(OkHttpClient.class); OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class); when(clientBuilder.build()).thenReturn(httpClient); when(httpClient.newBuilder()).thenReturn(clientBuilder); NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient); List<JavaOnlyArray> invalidHeaders = Arrays.asList(JavaOnlyArray.of("foo")); mockEvents(); networkingModule.sendRequest( mock(ExecutorToken.class), "GET", "http://somedoman/foo", /* requestId */ 0, /* headers */ JavaOnlyArray.from(invalidHeaders), /* body */ null, /* responseType */ "text", /* useIncrementalUpdates*/ true, /* timeout */ 0); verifyErrorEmit(emitter, 0); }
@Test public void testFailPostWithoutContentType() throws Exception { RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class); ReactApplicationContext context = mock(ReactApplicationContext.class); when(context.getJSModule(any(ExecutorToken.class), any(Class.class))).thenReturn(emitter); OkHttpClient httpClient = mock(OkHttpClient.class); OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class); when(clientBuilder.build()).thenReturn(httpClient); when(httpClient.newBuilder()).thenReturn(clientBuilder); NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient); JavaOnlyMap body = new JavaOnlyMap(); body.putString("string", "This is request body"); mockEvents(); networkingModule.sendRequest( mock(ExecutorToken.class), "POST", "http://somedomain/bar", 0, JavaOnlyArray.of(), body, /* responseType */ "text", /* useIncrementalUpdates*/ true, /* timeout */ 0); verifyErrorEmit(emitter, 0); }