Search in sources :

Example 6 with ToStringConverterFactory

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

the class CallTest method requestAfterEnqueueFailingThrows.

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

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

the class CallTest method cancelRequest.

@Test
public void cancelRequest() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).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();
        }
    });
    call.cancel();
    assertThat(call.isCanceled()).isTrue();
    assertTrue(latch.await(10, SECONDS));
    assertThat(failureRef.get()).isInstanceOf(IOException.class).hasMessage("Canceled");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 8 with ToStringConverterFactory

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

the class CallTest method responseBodyStreams.

@Test
public void responseBodyStreams() throws IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("1234").setSocketPolicy(DISCONNECT_DURING_RESPONSE_BODY));
    Response<ResponseBody> response = example.getStreamingBody().execute();
    ResponseBody streamedBody = response.body();
    // When streaming we only detect socket problems as the ResponseBody is read.
    try {
        streamedBody.string();
        fail();
    } catch (IOException e) {
        assertThat(e).hasMessage("unexpected end of stream");
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 9 with ToStringConverterFactory

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

the class CallTest method successfulRequestResponseWhenMimeTypeMissing.

@Test
public void successfulRequestResponseWhenMimeTypeMissing() throws Exception {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("Hi").removeHeader("Content-Type"));
    Response<String> response = example.getString().execute();
    assertThat(response.body()).isEqualTo("Hi");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 10 with ToStringConverterFactory

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

the class RetrofitTest method argumentCapture.

/** Confirm that Retrofit encodes parameters when the call is executed, and not earlier. */
@Test
public void argumentCapture() throws Exception {
    AtomicInteger i = new AtomicInteger();
    server.enqueue(new MockResponse().setBody("a"));
    server.enqueue(new MockResponse().setBody("b"));
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    MutableParameters mutableParameters = retrofit.create(MutableParameters.class);
    i.set(100);
    Call<String> call1 = mutableParameters.method(i);
    i.set(101);
    Response<String> response1 = call1.execute();
    i.set(102);
    assertEquals("a", response1.body());
    assertEquals("/?i=101", server.takeRequest().getPath());
    i.set(200);
    Call<String> call2 = call1.clone();
    i.set(201);
    Response<String> response2 = call2.execute();
    i.set(202);
    assertEquals("b", response2.body());
    assertEquals("/?i=201", server.takeRequest().getPath());
}
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