use of okhttp3.ResponseBody in project retrofit by square.
the class RetrofitTest method callbackExecutorUsedForFailure.
@Test
public void callbackExecutorUsedForFailure() throws InterruptedException {
Executor executor = spy(new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
});
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).callbackExecutor(executor).build();
CallMethod service = retrofit.create(CallMethod.class);
Call<ResponseBody> call = service.getResponseBody();
server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START));
final CountDownLatch latch = new CountDownLatch(1);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
throw new AssertionError();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
latch.countDown();
}
});
assertTrue(latch.await(2, TimeUnit.SECONDS));
verify(executor).execute(any(Runnable.class));
verifyNoMoreInteractions(executor);
}
use of okhttp3.ResponseBody 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();
}
use of okhttp3.ResponseBody 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();
}
use of okhttp3.ResponseBody in project retrofit by square.
the class ResponseTest method errorWithRawResponse.
@Test
public void errorWithRawResponse() {
ResponseBody errorBody = ResponseBody.create(null, "Broken!");
Response<?> response = Response.error(errorBody, errorResponse);
assertThat(response.raw()).isSameAs(errorResponse);
assertThat(response.code()).isEqualTo(400);
assertThat(response.message()).isEqualTo("Broken!");
assertThat(response.headers().size()).isZero();
assertThat(response.isSuccessful()).isFalse();
assertThat(response.body()).isNull();
assertThat(response.errorBody()).isSameAs(errorBody);
}
use of okhttp3.ResponseBody in project retrofit by square.
the class RetrofitTest method responseConverterFactoryQueried.
@Test
public void responseConverterFactoryQueried() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
Converter<ResponseBody, ?> expectedAdapter = mock(Converter.class);
Converter.Factory factory = mock(Converter.Factory.class);
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(factory).build();
doReturn(expectedAdapter).when(factory).responseBodyConverter(type, annotations, retrofit);
Converter<ResponseBody, ?> actualAdapter = retrofit.responseBodyConverter(type, annotations);
assertThat(actualAdapter).isSameAs(expectedAdapter);
verify(factory).responseBodyConverter(type, annotations, retrofit);
verifyNoMoreInteractions(factory);
}
Aggregations