Search in sources :

Example 6 with RestResponse

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

the class RxApiBindingTest method httpGetEmitsFailure.

/**
 * When the REST GET behavior emits a failure, the Rx binding should
 * emit that same failure as well.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void httpGetEmitsFailure() throws InterruptedException {
    RestOptions options = RestOptions.builder().addPath("/api/v1/movies").build();
    ApiException expectedFailure = new ApiException("Expected", "Failure");
    doAnswer(invocation -> {
        final int positionOfFailureConsumer = 2;
        Consumer<ApiException> onFailure = invocation.getArgument(positionOfFailureConsumer);
        onFailure.accept(expectedFailure);
        return null;
    }).when(delegate).get(eq(options), anyConsumer(), anyConsumer());
    TestObserver<RestResponse> observer = rxApi.get(options).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).get(eq(options), anyConsumer(), anyConsumer());
}
Also used : RestResponse(com.amplifyframework.api.rest.RestResponse) RestOptions(com.amplifyframework.api.rest.RestOptions) ApiException(com.amplifyframework.api.ApiException) Test(org.junit.Test)

Example 7 with RestResponse

use of com.amplifyframework.api.rest.RestResponse 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 8 with RestResponse

use of com.amplifyframework.api.rest.RestResponse 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 9 with RestResponse

use of com.amplifyframework.api.rest.RestResponse 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 10 with RestResponse

use of com.amplifyframework.api.rest.RestResponse 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

RestResponse (com.amplifyframework.api.rest.RestResponse)11 Test (org.junit.Test)11 RestOptions (com.amplifyframework.api.rest.RestOptions)9 ApiException (com.amplifyframework.api.ApiException)4 JSONObject (org.json.JSONObject)3 RestOperationRequest (com.amplifyframework.api.rest.RestOperationRequest)2 HashMap (java.util.HashMap)2 Ignore (org.junit.Ignore)2 HttpMethod (com.amplifyframework.api.rest.HttpMethod)1 Await (com.amplifyframework.testutils.Await)1 IOException (java.io.IOException)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 HttpUrl (okhttp3.HttpUrl)1 OkHttpClient (okhttp3.OkHttpClient)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 JSONException (org.json.JSONException)1 After (org.junit.After)1