Search in sources :

Example 81 with OkHttpClient

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);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException)

Example 82 with OkHttpClient

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"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Response(okhttp3.Response) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 83 with OkHttpClient

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");
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 84 with OkHttpClient

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");
    });
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) Headers(okhttp3.Headers) MediaType(okhttp3.MediaType) MockClient(com.recurly.v3.fixtures.MockClient) Test(org.junit.jupiter.api.Test)

Example 85 with OkHttpClient

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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) NotFoundException(com.recurly.v3.exception.NotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FixtureConstants(com.recurly.v3.fixtures.FixtureConstants) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockQueryParams(com.recurly.v3.fixtures.MockQueryParams) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) MockClient(com.recurly.v3.fixtures.MockClient) ApiException(com.recurly.v3.ApiException) Map(java.util.Map) Response(okhttp3.Response) Call(okhttp3.Call) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MediaType(okhttp3.MediaType) Request(okhttp3.Request) TransactionException(com.recurly.v3.exception.TransactionException) InvalidApiKeyException(com.recurly.v3.exception.InvalidApiKeyException) MyResource(com.recurly.v3.fixtures.MyResource) DateTime(org.joda.time.DateTime) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) OkHttpClient(okhttp3.OkHttpClient) ValidationException(com.recurly.v3.exception.ValidationException) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) Assert(org.junit.Assert) InternalServerException(com.recurly.v3.exception.InternalServerException) Collections(java.util.Collections) ExceptionFactory(com.recurly.v3.exception.ExceptionFactory) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) Call(okhttp3.Call) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Answer(org.mockito.stubbing.Answer) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) MyRequest(com.recurly.v3.fixtures.MyRequest) MyRequest(com.recurly.v3.fixtures.MyRequest) HttpUrl(okhttp3.HttpUrl) MockClient(com.recurly.v3.fixtures.MockClient) MyResource(com.recurly.v3.fixtures.MyResource) Test(org.junit.jupiter.api.Test)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)1944 Request (okhttp3.Request)1024 Response (okhttp3.Response)880 IOException (java.io.IOException)567 Test (org.junit.Test)365 Call (okhttp3.Call)290 RequestBody (okhttp3.RequestBody)222 Test (org.junit.jupiter.api.Test)145 Retrofit (retrofit2.Retrofit)138 File (java.io.File)132 HttpUrl (okhttp3.HttpUrl)131 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)128 Callback (okhttp3.Callback)117 JSONObject (org.json.JSONObject)110 ArrayList (java.util.ArrayList)106 ResponseBody (okhttp3.ResponseBody)105 Gson (com.google.gson.Gson)98 MediaType (okhttp3.MediaType)98 List (java.util.List)92 Map (java.util.Map)85