Search in sources :

Example 36 with Response

use of retrofit2.Response in project retrofit by square.

the class CallTest method requestThrowingBeforeEnqueueFailsEnqueue.

@Test
public void requestThrowingBeforeEnqueueFailsEnqueue() throws IOException, InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    final AtomicInteger writeCount = new AtomicInteger();
    Object a = new Object() {

        @Override
        public String toString() {
            writeCount.incrementAndGet();
            throw new RuntimeException("Broken!");
        }
    };
    Call<String> call = service.postRequestBody(a);
    try {
        call.request();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            assertThat(t).isExactlyInstanceOf(RuntimeException.class).hasMessage("Broken!");
            assertThat(writeCount.get()).isEqualTo(1);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 37 with Response

use of retrofit2.Response in project retrofit by square.

the class CallTest method requestAfterEnqueueFailingThrows.

@Test
public void requestAfterEnqueueFailingThrows() throws IOException, InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse());
    final AtomicInteger writeCount = new AtomicInteger();
    Object a = new Object() {

        @Override
        public String toString() {
            writeCount.incrementAndGet();
            throw new RuntimeException("Broken!");
        }
    };
    Call<String> call = service.postRequestBody(a);
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            assertThat(t).isExactlyInstanceOf(RuntimeException.class).hasMessage("Broken!");
            assertThat(writeCount.get()).isEqualTo(1);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, SECONDS));
    try {
        call.request();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 38 with Response

use of retrofit2.Response in project retrofit by square.

the class CallTest method cancelRequest.

@Test
public void cancelRequest() throws InterruptedException {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service service = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    Call<String> call = service.getString();
    final AtomicReference<Throwable> failureRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    call.enqueue(new Callback<String>() {

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            throw new AssertionError();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            failureRef.set(t);
            latch.countDown();
        }
    });
    call.cancel();
    assertThat(call.isCanceled()).isTrue();
    assertTrue(latch.await(10, SECONDS));
    assertThat(failureRef.get()).isInstanceOf(IOException.class).hasMessage("Canceled");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 39 with Response

use of retrofit2.Response in project retrofit by square.

the class CallTest method responseBodyStreams.

@Test
public void responseBodyStreams() 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));
    Response<ResponseBody> response = example.getStreamingBody().execute();
    ResponseBody streamedBody = response.body();
    // When streaming we only detect socket problems as the ResponseBody is read.
    try {
        streamedBody.string();
        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)

Example 40 with Response

use of retrofit2.Response in project retrofit by square.

the class CallTest method successfulRequestResponseWhenMimeTypeMissing.

@Test
public void successfulRequestResponseWhenMimeTypeMissing() throws Exception {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
    Service example = retrofit.create(Service.class);
    server.enqueue(new MockResponse().setBody("Hi").removeHeader("Content-Type"));
    Response<String> response = example.getString().execute();
    assertThat(response.body()).isEqualTo("Hi");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Aggregations

Response (okhttp3.Response)474 Request (okhttp3.Request)352 Test (org.junit.Test)237 IOException (java.io.IOException)188 Response (retrofit2.Response)156 ResponseBody (okhttp3.ResponseBody)148 ServiceResponse (com.microsoft.rest.ServiceResponse)114 Call (okhttp3.Call)102 MockResponse (okhttp3.mockwebserver.MockResponse)99 Observable (rx.Observable)98 OkHttpClient (okhttp3.OkHttpClient)72 RequestBody (okhttp3.RequestBody)71 Callback (okhttp3.Callback)43 List (java.util.List)40 TestClients.clientRequest (keywhiz.TestClients.clientRequest)37 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)34 Interceptor (okhttp3.Interceptor)29 HttpUrl (okhttp3.HttpUrl)27 MediaType (okhttp3.MediaType)26 Buffer (okio.Buffer)25