use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project AnkiChinaAndroid by ankichinateam.
the class OKHttpUtil method put.
public static void put(String url, String token, Object arg1, MyCallBack callback) {
Timber.i("start fetch url(put):%s,has token :%s", url, token);
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).callTimeout(30, TimeUnit.SECONDS).build();
final Request request = new Request.Builder().url(url).put(new FormBody.Builder().build()).addHeader("Authorization", "Bearer " + token).addHeader("x-app-version", BuildConfig.VERSION_NAME).addHeader("x-os", "android " + Build.VERSION.SDK_INT).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.onFailure(call, e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Timber.i("http get result:%s", response.body().string());
callback.onResponse(call, token, arg1, response);
}
});
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project recurly-client-java by recurly.
the class BaseClientTest method testUnknownError.
@Test
public void testUnknownError() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
final Response response = MockClient.buildResponse(999, "Unknown", getErrorJson("unknown"));
when(mCall.execute()).thenReturn(response);
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
// asserts that generic api exception is thrown for unknown error
assertThrows(ApiException.class, () -> {
client.getResource("code-aaron");
});
final RecurlyException exception = ExceptionFactory.getExceptionClass(response);
assertTrue(exception.toString().contains("ApiException"));
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project recurly-client-java by recurly.
the class BaseClientTest method testNetworkErrorWithoutResource.
@Test
public void testNetworkErrorWithoutResource() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
when(mCall.execute()).thenThrow(new IOException());
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(NetworkException.class, () -> {
client.removeResource("code-aaron");
});
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project recurly-client-java by recurly.
the class BaseClientTest method testNonJsonError0.
@Test
public void testNonJsonError0() throws IOException {
final Call mCall = mock(Call.class);
Answer answer = (i) -> {
return mCall;
};
Headers headers = new Headers.Builder().build();
MediaType contentType = MediaType.get("text/html; charset=UTF-8");
when(mCall.execute()).thenReturn(MockClient.buildResponse(0, "Not A Real Status", "<html>badness</html>", headers, contentType));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
assertThrows(ApiException.class, () -> {
client.getResource("code-aaron");
});
}
use of com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient in project recurly-client-java by recurly.
the class BaseClientTest method testMakeRequestWithBody.
@Test
public void testMakeRequestWithBody() throws IOException {
final Call mCall = mock(Call.class);
AtomicBoolean postCalled = new AtomicBoolean(false);
AtomicBoolean putCalled = new AtomicBoolean(false);
Answer answer = (i) -> {
Request request = i.getArgument(0);
HttpUrl url = request.url();
switch(request.method()) {
case "POST":
assertEquals("/resources", url.url().getPath());
postCalled.set(true);
break;
case "PUT":
assertEquals("/resources/someId", url.url().getPath());
putCalled.set(true);
break;
default:
// Any other request method is a failure
Assert.fail();
}
return mCall;
};
when(mCall.execute()).thenReturn(MockClient.buildResponse(200, "OK", getResponseJson())).thenReturn(MockClient.buildResponse(200, "OK", getResponseJson()));
OkHttpClient mockOkHttpClient = MockClient.getMockOkHttpClient(answer);
final MockClient client = new MockClient("apiKey", mockOkHttpClient);
final MyRequest newResource = new MyRequest();
newResource.setMyString("aaron");
final MyResource resource = client.createResource(newResource);
assertEquals(MyResource.class, resource.getClass());
assertEquals("aaron", resource.getMyString());
assertTrue(postCalled.get());
final MyResource anotherResource = client.updateResource("someId", newResource);
assertEquals(MyResource.class, anotherResource.getClass());
assertEquals("aaron", anotherResource.getMyString());
assertTrue(putCalled.get());
}
Aggregations