Search in sources :

Example 16 with Retrofit

use of retrofit2.Retrofit 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 17 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class RetrofitTest method stringConverterReturningNullResultsInDefault.

@Test
public void stringConverterReturningNullResultsInDefault() {
    final AtomicBoolean factoryCalled = new AtomicBoolean();
    class MyConverterFactory extends Converter.Factory {

        @Override
        public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            factoryCalled.set(true);
            return null;
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
    CallMethod service = retrofit.create(CallMethod.class);
    Call<ResponseBody> call = service.queryObject(null);
    assertThat(call).isNotNull();
    assertThat(factoryCalled.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 18 with Retrofit

use of retrofit2.Retrofit 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)

Example 19 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class RetrofitTest method requestConverterFactoryNoMatchThrows.

@Test
public void requestConverterFactoryNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingConverterFactory nonMatchingFactory = new NonMatchingConverterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory).build();
    try {
        retrofit.requestBodyConverter(type, annotations, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate RequestBody converter for class java.lang.String.\n" + "  Tried:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.helpers.NonMatchingConverterFactory");
    }
    assertThat(nonMatchingFactory.called).isTrue();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Example 20 with Retrofit

use of retrofit2.Retrofit in project retrofit by square.

the class RetrofitTest method callAdapterFactoryNoMatchThrows.

@Test
public void callAdapterFactoryNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingCallAdapterFactory nonMatchingFactory = new NonMatchingCallAdapterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addCallAdapterFactory(nonMatchingFactory).build();
    try {
        retrofit.callAdapter(type, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate call adapter for class java.lang.String.\n" + "  Tried:\n" + "   * retrofit2.helpers.NonMatchingCallAdapterFactory\n" + "   * retrofit2.DefaultCallAdapterFactory");
    }
    assertThat(nonMatchingFactory.called).isTrue();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) Annotation(java.lang.annotation.Annotation) 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