use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class BraintreeHttpClientTest method postsErrorWhenPathIsNull.
@Test(timeout = 1000)
public void postsErrorWhenPathIsNull() throws InterruptedException, InvalidArgumentException {
BraintreeHttpClient httpClient = new BraintreeHttpClient(TokenizationKey.fromString(TOKENIZATION_KEY));
final CountDownLatch countDownLatch = new CountDownLatch(2);
httpClient.get(null, new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(IllegalArgumentException.class, exception.getClass());
assertEquals("Path cannot be null", exception.getMessage());
countDownLatch.countDown();
}
});
httpClient.post(null, "{}", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(IllegalArgumentException.class, exception.getClass());
assertEquals("Path cannot be null", exception.getMessage());
countDownLatch.countDown();
}
});
countDownLatch.await();
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class HttpClientTest method assertExceptionIsPosted.
/* helpers */
private void assertExceptionIsPosted(HttpClient httpClient, final Class<? extends Exception> exceptionType, final String exceptionMessage) throws IOException, InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(2);
httpClient.get("/", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(exceptionType, exception.getClass());
if (exceptionMessage != null) {
assertEquals(exceptionMessage, exception.getMessage());
}
countDownLatch.countDown();
}
});
httpClient.post("/", "{}", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(exceptionType, exception.getClass());
if (exceptionMessage != null) {
assertEquals(exceptionMessage, exception.getMessage());
}
countDownLatch.countDown();
}
});
countDownLatch.await();
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class HttpClientTest method successCallbacksHappenOnMainThread.
@Test(timeout = 1000)
public void successCallbacksHappenOnMainThread() throws IOException, InterruptedException {
HttpClient httpClient = clientWithExpectedResponse(200, "");
final CountDownLatch countDownLatch = new CountDownLatch(2);
httpClient.get("/", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
assertEquals(Looper.getMainLooper(), Looper.myLooper());
countDownLatch.countDown();
}
@Override
public void failure(Exception exception) {
fail("Request failed");
}
});
httpClient.post("/", "{}", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
assertEquals(Looper.getMainLooper(), Looper.myLooper());
countDownLatch.countDown();
}
@Override
public void failure(Exception exception) {
fail("Request failed");
}
});
countDownLatch.await();
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class HttpClientTest method failureCallbacksHappenOnMainThread.
@Test(timeout = 1000)
public void failureCallbacksHappenOnMainThread() throws Exception {
HttpClient httpClient = spy(new HttpClient());
doThrow(new IOException()).when(httpClient).parseResponse(any(HttpsURLConnection.class));
final CountDownLatch countDownLatch = new CountDownLatch(2);
httpClient.get("/", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(Looper.getMainLooper(), Looper.myLooper());
countDownLatch.countDown();
}
});
httpClient.post("/", "{}", new HttpResponseCallback() {
@Override
public void success(String responseBody) {
fail("Request was successful");
}
@Override
public void failure(Exception exception) {
assertEquals(Looper.getMainLooper(), Looper.myLooper());
countDownLatch.countDown();
}
});
countDownLatch.await();
}
use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.
the class PayPal method requestOneTimePayment.
private static void requestOneTimePayment(final BraintreeFragment fragment, final PayPalRequest paypalRequest, final boolean isBillingAgreement, final PayPalApprovalHandler handler) {
final HttpResponseCallback callback = new HttpResponseCallback() {
@Override
public void success(String responseBody) {
final PayPalPaymentResource paypalPaymentResource;
try {
paypalPaymentResource = PayPalPaymentResource.fromJson(responseBody);
} catch (JSONException e) {
fragment.postCallback(e);
return;
}
String redirectUrl = Uri.parse(paypalPaymentResource.getRedirectUrl()).buildUpon().appendQueryParameter(USER_ACTION_KEY, paypalRequest.getUserAction()).toString();
Request request;
if (isBillingAgreement) {
request = getBillingAgreementRequest(fragment, redirectUrl);
} else {
request = getCheckoutRequest(fragment, redirectUrl);
}
startPayPal(fragment, request, handler);
}
@Override
public void failure(Exception e) {
fragment.postCallback(e);
}
};
fragment.waitForConfiguration(new ConfigurationListener() {
@Override
public void onConfigurationFetched(Configuration configuration) {
if (!configuration.isPayPalEnabled()) {
fragment.postCallback(new BraintreeException("PayPal is not enabled"));
return;
}
if (!isManifestValid(fragment)) {
fragment.sendAnalyticsEvent("paypal.invalid-manifest");
fragment.postCallback(new BraintreeException("BraintreeBrowserSwitchActivity missing, " + "incorrectly configured in AndroidManifest.xml or another app defines the same browser " + "switch url as this app. See " + "https://developers.braintreepayments.com/guides/client-sdk/android/v2#browser-switch " + "for the correct configuration"));
return;
}
try {
persistPayPalRequest(fragment.getApplicationContext(), paypalRequest);
createPaymentResource(fragment, paypalRequest, isBillingAgreement, callback);
} catch (JSONException | ErrorWithResponse | BraintreeException ex) {
fragment.postCallback(ex);
}
}
});
}
Aggregations