Search in sources :

Example 86 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method cancelOkHttpRequest.

@Test
public void cancelOkHttpRequest() throws InterruptedException {
    OkHttpClient client = new OkHttpClient();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).client(client).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    Call<String> call = service.getString();
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    call.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();
        }
    });
    // Cancel the underlying HTTP Call. Should be reflected accurately back in the Retrofit Call.
    client.dispatcher().cancelAll();
    assertThat(call.isCanceled()).isTrue();
    assertTrue(latch.await(10, SECONDS));
    assertThat(failureRef.get()).isInstanceOf(IOException.class).hasMessage("Canceled");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 87 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method executeCallOnce.

@Test
public void executeCallOnce() throws IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    Call<String> call = example.getString();
    call.execute();
    try {
        call.execute();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessage("Already executed.");
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 88 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method requestAfterExecuteThrowingAlsoThrows.

@Test
public void requestAfterExecuteThrowingAlsoThrows() 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.execute();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
    try {
        call.request();
        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 89 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method cancelBeforeEnqueue.

@Test
public void cancelBeforeEnqueue() throws Exception {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    Call<String> call = service.getString();
    call.cancel();
    assertThat(call.isCanceled()).isTrue();
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    call.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));
    assertThat(failureRef.get()).hasMessage("Canceled");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 90 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method http200Async.

@Test
public void http200Async() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("Hi"));
    final AtomicReference<Response<String>> responseRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.getString().enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            responseRef.set(response);
            latch.countDown();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            t.printStackTrace();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    Response<String> response = responseRef.get();
    assertThat(response.isSuccessful()).isTrue();
    assertThat(response.body()).isEqualTo("Hi");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MockResponse(okhttp3.mockwebserver.MockResponse) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Aggregations

Retrofit (retrofit2.Retrofit)81 Test (org.junit.Test)42 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)35 Before (org.junit.Before)34 MockResponse (okhttp3.mockwebserver.MockResponse)30 ResponseBody (okhttp3.ResponseBody)27 Type (java.lang.reflect.Type)25 IOException (java.io.IOException)22 OkHttpClient (okhttp3.OkHttpClient)22 ParameterizedType (java.lang.reflect.ParameterizedType)13 MediaType (okhttp3.MediaType)13 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Gson (com.google.gson.Gson)11 GsonBuilder (com.google.gson.GsonBuilder)11 NonMatchingCallAdapterFactory (retrofit2.helpers.NonMatchingCallAdapterFactory)11 Converter (retrofit2.Converter)10 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)10 Annotation (java.lang.annotation.Annotation)9