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);
}
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);
}
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!");
}
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);
}
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");
}
}
Aggregations