Search in sources :

Example 51 with ResponseBody

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);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ResponseBody(okhttp3.ResponseBody) Executor(java.util.concurrent.Executor) Test(org.junit.Test)

Example 52 with ResponseBody

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();
}
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 53 with ResponseBody

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();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Retrofit(retrofit2.Retrofit) MockWebServer(okhttp3.mockwebserver.MockWebServer) ResponseBody(okhttp3.ResponseBody)

Example 54 with ResponseBody

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);
}
Also used : ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 55 with ResponseBody

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);
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)584 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)332 DateTime (org.joda.time.DateTime)332 ServiceCall (com.microsoft.rest.ServiceCall)140 Test (org.junit.Test)123 Request (okhttp3.Request)112 Observable (rx.Observable)97 Response (retrofit2.Response)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 PagedList (com.microsoft.azure.PagedList)80 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)78 List (java.util.List)64 IOException (java.io.IOException)33 Response (okhttp3.Response)33 Buffer (okio.Buffer)33 RequestBody (okhttp3.RequestBody)31 PageImpl (com.microsoft.azure.batch.protocol.models.PageImpl)26 MockResponse (okhttp3.mockwebserver.MockResponse)24 InputStream (java.io.InputStream)19 MultipartBody (okhttp3.MultipartBody)16