Search in sources :

Example 1 with ToStringConverterFactory

use of retrofit2.helpers.ToStringConverterFactory in project retrofit by square.

the class CallTest method requestThrowingBeforeExecuteFailsExecute.

@Test
public void requestThrowingBeforeExecuteFailsExecute() throws IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    final AtomicInteger writeCount = new AtomicInteger();
    Object a = new Object() {

        @Override
        public String toString() {
            writeCount.incrementAndGet();
            throw new RuntimeException("Broken!");
        }
    };
    Call<String> call = service.postRequestBody(a);
    try {
        call.request();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
    try {
        call.execute();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 2 with ToStringConverterFactory

use of retrofit2.helpers.ToStringConverterFactory in project retrofit by square.

the class RequestBuilderTest method buildRequest.

static <T> Request buildRequest(Class<T> cls, Object... args) {
    final AtomicReference<Request> requestRef = new AtomicReference<>();
    okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {

        @Override
        public okhttp3.Call newCall(Request request) {
            requestRef.set(request);
            throw new UnsupportedOperationException("Not implemented");
        }
    };
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(new ToStringConverterFactory()).callFactory(callFactory).build();
    Method method = TestingUtils.onlyMethod(cls);
    //noinspection unchecked
    ServiceMethod<T, Call<T>> serviceMethod = (ServiceMethod<T, Call<T>>) retrofit.loadServiceMethod(method);
    Call<T> okHttpCall = new OkHttpCall<>(serviceMethod, args);
    Call<T> call = serviceMethod.callAdapter.adapt(okHttpCall);
    try {
        call.execute();
        throw new AssertionError();
    } catch (UnsupportedOperationException ignored) {
        return requestRef.get();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}
Also used : Request(okhttp3.Request) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) IOException(java.io.IOException) POST(retrofit2.http.POST) PUT(retrofit2.http.PUT) GET(retrofit2.http.GET) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory)

Example 3 with ToStringConverterFactory

use of retrofit2.helpers.ToStringConverterFactory in project retrofit by square.

the class CallTest method transportProblemAsync.

@Test
public void transportProblemAsync() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.getString().enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    Throwable failure = failureRef.get();
    assertThat(failure).isInstanceOf(IOException.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 4 with ToStringConverterFactory

use of retrofit2.helpers.ToStringConverterFactory in project retrofit by square.

the class CallTest method requestThrowingBeforeEnqueueFailsEnqueue.

@Test
public void requestThrowingBeforeEnqueueFailsEnqueue() throws IOException, InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    final AtomicInteger writeCount = new AtomicInteger();
    Object a = new Object() {

        @Override
        public String toString() {
            writeCount.incrementAndGet();
            throw new RuntimeException("Broken!");
        }
    };
    Call<String> call = service.postRequestBody(a);
    try {
        call.request();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            assertThat(t).isExactlyInstanceOf(RuntimeException.class).hasMessage("Broken!");
            assertThat(writeCount.get()).isEqualTo(1);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 5 with ToStringConverterFactory

use of retrofit2.helpers.ToStringConverterFactory in project retrofit by square.

the class CallTest method requestBeforeExecuteCreates.

@Test
public void requestBeforeExecuteCreates() throws IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    final AtomicInteger writeCount = new AtomicInteger();
    Object a = new Object() {

        @Override
        public String toString() {
            writeCount.incrementAndGet();
            return "Hello";
        }
    };
    Call<String> call = service.postRequestBody(a);
    call.request();
    assertThat(writeCount.get()).isEqualTo(1);
    call.execute();
    assertThat(writeCount.get()).isEqualTo(1);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Aggregations

ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)30 Test (org.junit.Test)29 MockResponse (okhttp3.mockwebserver.MockResponse)23 CountDownLatch (java.util.concurrent.CountDownLatch)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Type (java.lang.reflect.Type)7 ResponseBody (okhttp3.ResponseBody)7 IOException (java.io.IOException)6 ParameterizedType (java.lang.reflect.ParameterizedType)4 MediaType (okhttp3.MediaType)4 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)4 NonMatchingCallAdapterFactory (retrofit2.helpers.NonMatchingCallAdapterFactory)4 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)4 Annotation (java.lang.annotation.Annotation)2 OkHttpClient (okhttp3.OkHttpClient)2 RequestBody (okhttp3.RequestBody)2 Method (java.lang.reflect.Method)1 Interceptor (okhttp3.Interceptor)1 Request (okhttp3.Request)1