Search in sources :

Example 1 with Converter

use of retrofit2.Converter 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 2 with Converter

use of retrofit2.Converter 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 3 with Converter

use of retrofit2.Converter in project retrofit by square.

the class DeserializeErrorBody method main.

public static void main(String... args) throws IOException {
    // Create a local web server which response with a 404 and JSON body.
    MockWebServer server = new MockWebServer();
    server.start();
    server.enqueue(new MockResponse().setResponseCode(404).setBody("{\"message\":\"Unable to locate resource\"}"));
    // Create our Service instance with a Retrofit pointing at the local web server and Gson.
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(GsonConverterFactory.create()).build();
    Service service = retrofit.create(Service.class);
    Response<User> response = service.getUser().execute();
    // Normally you would check response.isSuccess() here before doing the following, but we know
    // this call will always fail. You could also use response.code() to determine whether to
    // convert the error body and/or which type to use for conversion.
    // Look up a converter for the Error type on the Retrofit instance.
    Converter<ResponseBody, Error> errorConverter = retrofit.responseBodyConverter(Error.class, new Annotation[0]);
    // Convert the error body into our Error type.
    Error error = errorConverter.convert(response.errorBody());
    System.out.println("ERROR: " + error.message);
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Retrofit(retrofit2.Retrofit) MockWebServer(okhttp3.mockwebserver.MockWebServer) ResponseBody(okhttp3.ResponseBody)

Example 4 with Converter

use of retrofit2.Converter in project retrofit by square.

the class RetrofitTest method requestConverterFactorySkippedNoMatchThrows.

@Test
public void requestConverterFactorySkippedNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingConverterFactory nonMatchingFactory1 = new NonMatchingConverterFactory();
    NonMatchingConverterFactory nonMatchingFactory2 = new NonMatchingConverterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory1).addConverterFactory(nonMatchingFactory2).build();
    try {
        retrofit.nextRequestBodyConverter(nonMatchingFactory1, type, annotations, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate RequestBody converter for class java.lang.String.\n" + "  Skipped:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.helpers.NonMatchingConverterFactory\n" + "  Tried:\n" + "   * retrofit2.helpers.NonMatchingConverterFactory");
    }
    assertThat(nonMatchingFactory1.called).isFalse();
    assertThat(nonMatchingFactory2.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 5 with Converter

use of retrofit2.Converter in project retrofit by square.

the class RetrofitTest method methodAnnotationsPassedToResponseBodyConverter.

@Test
public void methodAnnotationsPassedToResponseBodyConverter() {
    final AtomicReference<Annotation[]> annotationsRef = new AtomicReference<>();
    class MyConverterFactory extends Converter.Factory {

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            annotationsRef.set(annotations);
            return new ToStringConverterFactory().responseBodyConverter(type, annotations, retrofit);
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
    Annotated annotated = retrofit.create(Annotated.class);
    // Trigger internal setup.
    annotated.method();
    Annotation[] annotations = annotationsRef.get();
    assertThat(annotations).hasAtLeastOneElementOfType(Annotated.Foo.class);
}
Also used : 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Annotation(java.lang.annotation.Annotation) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 Type (java.lang.reflect.Type)11 ParameterizedType (java.lang.reflect.ParameterizedType)8 MediaType (okhttp3.MediaType)8 ResponseBody (okhttp3.ResponseBody)8 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)8 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)7 Annotation (java.lang.annotation.Annotation)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)4 NonMatchingCallAdapterFactory (retrofit2.helpers.NonMatchingCallAdapterFactory)4 MockResponse (okhttp3.mockwebserver.MockResponse)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 RequestBody (okhttp3.RequestBody)2 Retrofit (retrofit2.Retrofit)2 JsonReader (com.google.gson.stream.JsonReader)1 IOException (java.io.IOException)1 Interceptor (okhttp3.Interceptor)1 OkHttpClient (okhttp3.OkHttpClient)1