Java Code Examples for com.facebook.react.bridge.JavaOnlyArray#pushMap()

The following examples show how to use com.facebook.react.bridge.JavaOnlyArray#pushMap() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: react-native-navigation   File: JSONParserTest.java    License: MIT License 6 votes vote down vote up
@Test
public void parsesArrays() throws Exception {
    JavaOnlyArray input = new JavaOnlyArray();
    input.pushString("Hello");
    input.pushInt(123);
    input.pushDouble(123.456);
    input.pushBoolean(true);
    input.pushArray(new JavaOnlyArray());
    input.pushMap(new JavaOnlyMap());
    input.pushNull();

    JSONArray result = new JSONParser().parse(input);
    assertThat(result.length()).isEqualTo(6);
    assertThat(result.get(0)).isEqualTo("Hello");
    assertThat(result.get(1)).isEqualTo(123);
    assertThat(result.get(2)).isEqualTo(123.456);
    assertThat(result.get(3)).isEqualTo(true);
    assertThat(result.getJSONArray(4).length()).isZero();
    assertThat(result.getJSONObject(5).keys()).isEmpty();
}
 
Example 2
Source Project: react-native-GPay   File: BlobModuleTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testCreateFromParts() {
  String id = UUID.randomUUID().toString();

  JavaOnlyMap blobData = new JavaOnlyMap();
  blobData.putString("blobId", mBlobId);
  blobData.putInt("offset", 0);
  blobData.putInt("size", mBytes.length);
  JavaOnlyMap blob = new JavaOnlyMap();
  blob.putMap("data", blobData);
  blob.putString("type", "blob");

  String stringData = "i \u2665 dogs";
  byte[] stringBytes = stringData.getBytes(Charset.forName("UTF-8"));
  JavaOnlyMap string = new JavaOnlyMap();
  string.putString("data", stringData);
  string.putString("type", "string");

  JavaOnlyArray parts = new JavaOnlyArray();
  parts.pushMap(blob);
  parts.pushMap(string);

  mBlobModule.createFromParts(parts, id);

  int resultSize = mBytes.length + stringBytes.length;

  byte[] result = mBlobModule.resolve(id, 0, resultSize);

  ByteBuffer buffer = ByteBuffer.allocate(resultSize);
  buffer.put(mBytes);
  buffer.put(stringBytes);

  assertArrayEquals(result, buffer.array());
}
 
Example 3
Source Project: react-native-GPay   File: NetworkingModuleTest.java    License: MIT License 4 votes vote down vote up
@Test
public void testMultipartPostRequestSimple() throws Exception {
  PowerMockito.mockStatic(RequestBodyUtil.class);
  when(RequestBodyUtil.getFileInputStream(any(ReactContext.class), any(String.class)))
      .thenReturn(mock(InputStream.class));
  when(RequestBodyUtil.create(any(MediaType.class), any(InputStream.class)))
      .thenReturn(mock(RequestBody.class));
  when(RequestBodyUtil.createProgressRequest(any(RequestBody.class), any(ProgressListener.class)))
      .thenCallRealMethod();

  JavaOnlyMap body = new JavaOnlyMap();
  JavaOnlyArray formData = new JavaOnlyArray();
  JavaOnlyMap bodyPart = new JavaOnlyMap();
  bodyPart.putString("string", "value");
  bodyPart.putArray(
      "headers",
      JavaOnlyArray.from(
          Arrays.asList(
              JavaOnlyArray.of("content-disposition", "name"))));
  formData.pushMap(bodyPart);
  body.putArray("formData", formData);

  OkHttpClient httpClient = mock(OkHttpClient.class);
  when(httpClient.newCall(any(Request.class))).thenAnswer(
      new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
          Call callMock = mock(Call.class);
          return callMock;
        }
      });
  OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
  when(clientBuilder.build()).thenReturn(httpClient);
  when(httpClient.newBuilder()).thenReturn(clientBuilder);
  NetworkingModule networkingModule =
    new NetworkingModule(mock(ReactApplicationContext.class), "", httpClient);
  networkingModule.sendRequest(
    "POST",
    "http://someurl/uploadFoo",
    0,
    new JavaOnlyArray(),
    body,
    /* responseType */ "text",
    /* useIncrementalUpdates*/ true,
    /* timeout */ 0,
    /* withCredentials */ false);

  // verify url, method, headers
  ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
  verify(httpClient).newCall(argumentCaptor.capture());
  assertThat(argumentCaptor.getValue().url().toString()).isEqualTo("http://someurl/uploadFoo");
  assertThat(argumentCaptor.getValue().method()).isEqualTo("POST");
  assertThat(argumentCaptor.getValue().body().contentType().type()).
      isEqualTo(MultipartBody.FORM.type());
  assertThat(argumentCaptor.getValue().body().contentType().subtype()).
      isEqualTo(MultipartBody.FORM.subtype());
  Headers requestHeaders = argumentCaptor.getValue().headers();
  assertThat(requestHeaders.size()).isEqualTo(1);
}
 
Example 4
Source Project: react-native-GPay   File: NetworkingModuleTest.java    License: MIT License 4 votes vote down vote up
@Test
public void testMultipartPostRequestHeaders() throws Exception {
  PowerMockito.mockStatic(RequestBodyUtil.class);
  when(RequestBodyUtil.getFileInputStream(any(ReactContext.class), any(String.class)))
      .thenReturn(mock(InputStream.class));
  when(RequestBodyUtil.create(any(MediaType.class), any(InputStream.class)))
      .thenReturn(mock(RequestBody.class));
  when(RequestBodyUtil.createProgressRequest(any(RequestBody.class), any(ProgressListener.class)))
      .thenCallRealMethod();

  List<JavaOnlyArray> headers = Arrays.asList(
          JavaOnlyArray.of("Accept", "text/plain"),
          JavaOnlyArray.of("User-Agent", "React test agent/1.0"),
          JavaOnlyArray.of("content-type", "multipart/form-data"));

  JavaOnlyMap body = new JavaOnlyMap();
  JavaOnlyArray formData = new JavaOnlyArray();
  JavaOnlyMap bodyPart = new JavaOnlyMap();
  bodyPart.putString("string", "value");
  bodyPart.putArray(
      "headers",
      JavaOnlyArray.from(
          Arrays.asList(
              JavaOnlyArray.of("content-disposition", "name"))));
  formData.pushMap(bodyPart);
  body.putArray("formData", formData);

  OkHttpClient httpClient = mock(OkHttpClient.class);
  when(httpClient.newCall(any(Request.class))).thenAnswer(
      new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
          Call callMock = mock(Call.class);
          return callMock;
        }
      });
  OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
  when(clientBuilder.build()).thenReturn(httpClient);
  when(httpClient.newBuilder()).thenReturn(clientBuilder);
  NetworkingModule networkingModule =
    new NetworkingModule(mock(ReactApplicationContext.class), "", httpClient);
  networkingModule.sendRequest(
    "POST",
    "http://someurl/uploadFoo",
    0,
    JavaOnlyArray.from(headers),
    body,
    /* responseType */ "text",
    /* useIncrementalUpdates*/ true,
    /* timeout */ 0,
    /* withCredentials */ false);

  // verify url, method, headers
  ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
  verify(httpClient).newCall(argumentCaptor.capture());
  assertThat(argumentCaptor.getValue().url().toString()).isEqualTo("http://someurl/uploadFoo");
  assertThat(argumentCaptor.getValue().method()).isEqualTo("POST");
  assertThat(argumentCaptor.getValue().body().contentType().type()).
      isEqualTo(MultipartBody.FORM.type());
  assertThat(argumentCaptor.getValue().body().contentType().subtype()).
      isEqualTo(MultipartBody.FORM.subtype());
  Headers requestHeaders = argumentCaptor.getValue().headers();
  assertThat(requestHeaders.size()).isEqualTo(3);
  assertThat(requestHeaders.get("Accept")).isEqualTo("text/plain");
  assertThat(requestHeaders.get("User-Agent")).isEqualTo("React test agent/1.0");
  assertThat(requestHeaders.get("content-type")).isEqualTo("multipart/form-data");
}