Search in sources :

Example 11 with ToStringConverterFactory

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

Example 12 with ToStringConverterFactory

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

the class RetrofitTest method methodAndParameterAnnotationsPassedToRequestBodyConverter.

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

        @Override
        public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
            parameterAnnotationsRef.set(parameterAnnotations);
            methodAnnotationsRef.set(methodAnnotations);
            return new ToStringConverterFactory().requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
    Annotated annotated = retrofit.create(Annotated.class);
    // Trigger internal setup.
    annotated.bodyParameter(null);
    assertThat(parameterAnnotationsRef.get()).hasAtLeastOneElementOfType(Annotated.Foo.class);
    assertThat(methodAnnotationsRef.get()).hasAtLeastOneElementOfType(POST.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) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 13 with ToStringConverterFactory

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

the class RetrofitTest method customCallAdapter.

@Test
public void customCallAdapter() {
    class GreetingCallAdapterFactory extends CallAdapter.Factory {

        @Override
        public CallAdapter<Object, String> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
            if (getRawType(returnType) != String.class) {
                return null;
            }
            return new CallAdapter<Object, String>() {

                @Override
                public Type responseType() {
                    return String.class;
                }

                @Override
                public String adapt(Call<Object> call) {
                    return "Hi!";
                }
            };
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).addCallAdapterFactory(new GreetingCallAdapterFactory()).build();
    StringService example = retrofit.create(StringService.class);
    assertThat(example.get()).isEqualTo("Hi!");
}
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) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 14 with ToStringConverterFactory

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

the class RetrofitTest method methodAnnotationsPassedToCallAdapter.

@Test
public void methodAnnotationsPassedToCallAdapter() {
    final AtomicReference<Annotation[]> annotationsRef = new AtomicReference<>();
    class MyCallAdapterFactory extends CallAdapter.Factory {

        @Override
        public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
            annotationsRef.set(annotations);
            return null;
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).addCallAdapterFactory(new MyCallAdapterFactory()).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) Test(org.junit.Test)

Example 15 with ToStringConverterFactory

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

the class CallTest method responseBodyBuffers.

@Test
public void responseBodyBuffers() 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));
    Call<ResponseBody> buffered = example.getBody();
    // When buffering we will detect all socket problems before returning the Response.
    try {
        buffered.execute();
        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)

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