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"));
}
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"));
}
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());
}
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());
}
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();
}));
}
Aggregations