Search in sources :

Example 6 with RestOptions

use of com.amplifyframework.api.rest.RestOptions in project amplify-android by aws-amplify.

the class RxApiBindingTest method httpGetEmitsResult.

/**
 * When REST GET behavior emits a result, the Rx binding
 * should emit it, too.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void httpGetEmitsResult() throws InterruptedException {
    RestOptions options = RestOptions.builder().addPath("/api/v1/movies").build();
    // JSONObject would need to bring in Robolectric
    byte[] data = "{\"movies\":[\"Spider Man\"]}".getBytes();
    final int httpOkStatus = 200;
    RestResponse response = new RestResponse(httpOkStatus, Collections.emptyMap(), data);
    doAnswer(invocation -> {
        final int positionOfResponseConsumer = 1;
        Consumer<RestResponse> onResponse = invocation.getArgument(positionOfResponseConsumer);
        onResponse.accept(response);
        return null;
    }).when(delegate).get(eq(options), anyConsumer(), anyConsumer());
    TestObserver<RestResponse> observer = rxApi.get(options).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertValue(response);
    verify(delegate).get(eq(options), anyConsumer(), anyConsumer());
}
Also used : RestResponse(com.amplifyframework.api.rest.RestResponse) RestOptions(com.amplifyframework.api.rest.RestOptions) Test(org.junit.Test)

Example 7 with RestOptions

use of com.amplifyframework.api.rest.RestOptions in project amplify-android by aws-amplify.

the class RxApiBindingTest method httpPostEmitsResult.

/**
 * When the REST POST behavior emits a result, the Rx binding
 * should do the same.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void httpPostEmitsResult() throws InterruptedException {
    // Arrange response from category behavior
    byte[] body = RandomString.string().getBytes();
    RestOptions options = RestOptions.builder().addPath("/api/v1/your_name").addBody(body).build();
    final int httpOkStatus = 200;
    // Re-use body
    RestResponse response = new RestResponse(httpOkStatus, Collections.emptyMap(), body);
    doAnswer(invocation -> {
        // 0 = options, 1 = onResponse, 2 = onFailure
        final int positionOfResponseConsumer = 1;
        Consumer<RestResponse> onResponse = invocation.getArgument(positionOfResponseConsumer);
        onResponse.accept(response);
        return null;
    }).when(delegate).post(eq(options), anyConsumer(), anyConsumer());
    // Act: post via Rx binding
    TestObserver<RestResponse> observer = rxApi.post(options).test();
    // Asset: it worked!
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertValue(response);
    verify(delegate).post(eq(options), anyConsumer(), anyConsumer());
}
Also used : RestResponse(com.amplifyframework.api.rest.RestResponse) RestOptions(com.amplifyframework.api.rest.RestOptions) Test(org.junit.Test)

Example 8 with RestOptions

use of com.amplifyframework.api.rest.RestOptions in project amplify-android by aws-amplify.

the class RestApiInstrumentationTest method getRequestWithIAMFailedAccess.

/**
 * Test whether we can get failed response for access denied.
 * @throws ApiException On failure to obtain a valid response from API endpoint
 */
@Test
@Ignore("Relies on an AWS account which is no longer active.  Resources need to be regenerated.")
public void getRequestWithIAMFailedAccess() throws ApiException {
    final RestOptions options = RestOptions.builder().addPath("/invalidPath").build();
    final RestResponse response = api.get("iamAuthApi", options);
    assertNotNull("Should return non-null data", response.getData());
    assertFalse("Response should be unsuccessful", response.getCode().isSuccessful());
}
Also used : RestResponse(com.amplifyframework.api.rest.RestResponse) RestOptions(com.amplifyframework.api.rest.RestOptions) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with RestOptions

use of com.amplifyframework.api.rest.RestOptions in project amplify-android by aws-amplify.

the class RestApiInstrumentationTest method postRequestWithNoAuth.

/**
 * Test whether we can make POST api Rest call in none auth.
 * @throws ApiException On failure to obtain a valid response from API endpoint
 */
@Test
public void postRequestWithNoAuth() throws ApiException {
    final RestOptions options = RestOptions.builder().addPath("/simplesuccess").addBody("sample body".getBytes()).build();
    final RestResponse response = api.post("nonAuthApi", options);
    assertNotNull("Should return non-null data", response.getData());
    assertTrue("Response should be successful", response.getCode().isSuccessful());
}
Also used : RestResponse(com.amplifyframework.api.rest.RestResponse) RestOptions(com.amplifyframework.api.rest.RestOptions) Test(org.junit.Test)

Aggregations

RestOptions (com.amplifyframework.api.rest.RestOptions)9 RestResponse (com.amplifyframework.api.rest.RestResponse)9 Test (org.junit.Test)9 ApiException (com.amplifyframework.api.ApiException)2 JSONObject (org.json.JSONObject)2 Ignore (org.junit.Ignore)2