Search in sources :

Example 1 with RestResponse

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

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

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

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

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

the class AWSRestOperationTest method noErrorEmittedIfOperationIsCancelled.

/**
 * If the user calls {@link AWSRestOperation#cancel()}, then the operation
 * will not fire any callback. This behavior is consistent with iOS's REST operation.
 */
@Test
public void noErrorEmittedIfOperationIsCancelled() {
    long timeToWaitForResponse = 300L;
    RestOperationRequest request = new RestOperationRequest(HttpMethod.GET, baseUrl.uri().getPath(), emptyMap(), emptyMap());
    assertTimedOut(() -> Await.<RestResponse, ApiException>result(timeToWaitForResponse, (onResult, onError) -> {
        AWSRestOperation operation = new AWSRestOperation(request, baseUrl.url().toString(), client, onResult, onError);
        operation.start();
        operation.cancel();
    }));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ApiException(com.amplifyframework.api.ApiException) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) RestResponse(com.amplifyframework.api.rest.RestResponse) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) HttpMethod(com.amplifyframework.api.rest.HttpMethod) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) OkHttpClient(okhttp3.OkHttpClient) Await(com.amplifyframework.testutils.Await) After(org.junit.After) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) MockResponse(okhttp3.mockwebserver.MockResponse) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) RestOperationRequest(com.amplifyframework.api.rest.RestOperationRequest) 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