Search in sources :

Example 1 with RestOptions

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

the class RestApiInstrumentationTest method getRequestWithApiKey.

/**
 * Test whether we can make api Rest call in api key as auth type.
 * @throws JSONException If JSON parsing of arranged data fails
 * @throws ApiException On failure to obtain a valid response from API endpoint
 */
@Test
public void getRequestWithApiKey() throws JSONException, ApiException {
    final RestOptions options = RestOptions.builder().addPath("/simplesuccessapikey").build();
    final RestResponse response = api.get("apiKeyApi", options);
    final JSONObject resultJSON = response.getData().asJSONObject();
    final JSONObject contextJSON = resultJSON.getJSONObject("context");
    assertNotNull("Should contain an object called context", contextJSON);
    assertEquals("Should return the right value", "GET", contextJSON.getString("http-method"));
    assertEquals("Should return the right value", "/simplesuccessapikey", contextJSON.getString("resource-path"));
}
Also used : JSONObject(org.json.JSONObject) RestResponse(com.amplifyframework.api.rest.RestResponse) RestOptions(com.amplifyframework.api.rest.RestOptions) Test(org.junit.Test)

Example 2 with RestOptions

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

the class RestApiInstrumentationTest method getRequestWithNoAuth.

/**
 * Test whether we can make api Rest call in none auth.
 * @throws JSONException If JSON parsing of arranged data fails
 * @throws ApiException On failure to obtain a valid response from API endpoint
 */
@Test
public void getRequestWithNoAuth() throws JSONException, ApiException {
    final RestOptions options = RestOptions.builder().addPath("/simplesuccess").build();
    final RestResponse response = api.get("nonAuthApi", options);
    final JSONObject resultJSON = response.getData().asJSONObject();
    final JSONObject contextJSON = resultJSON.getJSONObject("context");
    assertNotNull("Should contain an object called context", contextJSON);
    assertEquals("Should return the right value", "GET", contextJSON.getString("http-method"));
    assertEquals("Should return the right value", "/simplesuccess", contextJSON.getString("resource-path"));
}
Also used : JSONObject(org.json.JSONObject) RestResponse(com.amplifyframework.api.rest.RestResponse) RestOptions(com.amplifyframework.api.rest.RestOptions) Test(org.junit.Test)

Example 3 with RestOptions

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

the class RestApiInstrumentationTest method getRequestWithIAM.

/**
 * Test whether we can make api Rest call in IAM as auth type.
 * @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 getRequestWithIAM() throws ApiException {
    final RestOptions options = RestOptions.builder().addPath("/items").addQueryParameters(Collections.singletonMap("key", "value")).build();
    final RestResponse response = api.get("iamAuthApi", 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with RestOptions

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

the class RxApiBindingTest method httpPostEmitsFailure.

/**
 * When the REST POST behavior emits a failure, the Rx binding
 * should do the same.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void httpPostEmitsFailure() throws InterruptedException {
    byte[] body = RandomString.string().getBytes();
    RestOptions options = RestOptions.builder().addBody(body).addPath("/some/path").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).post(eq(options), anyConsumer(), anyConsumer());
    // Act: post via the Rx binding
    TestObserver<RestResponse> observer = rxApi.post(options).test();
    // Assert: failure bubbles through
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).post(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 5 with RestOptions

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

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