Search in sources :

Example 96 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method rawResponseContentTypeAndLengthButNoSource.

@Test
public void rawResponseContentTypeAndLengthButNoSource() 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("Hi").addHeader("Content-Type", "text/greeting"));
    Response<String> response = example.getString().execute();
    assertThat(response.body()).isEqualTo("Hi");
    ResponseBody rawBody = response.raw().body();
    assertThat(rawBody.contentLength()).isEqualTo(2);
    assertThat(rawBody.contentType().toString()).isEqualTo("text/greeting");
    try {
        rawBody.source();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessage("Cannot read raw response body of a converted body.");
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 97 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method conversionProblemIncomingAsync.

@Test
public void conversionProblemIncomingAsync() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory() {

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            return new Converter<ResponseBody, String>() {

                @Override
                public String convert(ResponseBody value) throws IOException {
                    throw new UnsupportedOperationException("I am broken!");
                }
            };
        }
    }).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("Hi"));
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    example.postString("Hi").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()).isInstanceOf(UnsupportedOperationException.class).hasMessage("I am broken!");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 98 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method http404Async.

@Test
public void http404Async() throws InterruptedException, IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setResponseCode(404).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()).isFalse();
    assertThat(response.code()).isEqualTo(404);
    assertThat(response.errorBody().string()).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)

Example 99 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method requestAfterExecuteReturnsCachedValue.

@Test
public void requestAfterExecuteReturnsCachedValue() 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.execute();
    assertThat(writeCount.get()).isEqualTo(1);
    call.request();
    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 100 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class CallTest method http404Sync.

@Test
public void http404Sync() throws IOException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setResponseCode(404).setBody("Hi"));
    Response<String> response = example.getString().execute();
    assertThat(response.isSuccessful()).isFalse();
    assertThat(response.code()).isEqualTo(404);
    assertThat(response.errorBody().string()).isEqualTo("Hi");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Aggregations

Retrofit (retrofit2.Retrofit)76 Test (org.junit.Test)42 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)35 Before (org.junit.Before)34 MockResponse (okhttp3.mockwebserver.MockResponse)30 Type (java.lang.reflect.Type)17 OkHttpClient (okhttp3.OkHttpClient)16 ResponseBody (okhttp3.ResponseBody)16 IOException (java.io.IOException)13 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 NonMatchingCallAdapterFactory (retrofit2.helpers.NonMatchingCallAdapterFactory)11 GsonBuilder (com.google.gson.GsonBuilder)10 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)10 Annotation (java.lang.annotation.Annotation)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Request (okhttp3.Request)7