Search in sources :

Example 11 with Call

use of retrofit2.Call in project retrofit by square.

the class ProtoConverterFactoryTest method deserializeEmpty.

@Test
public void deserializeEmpty() throws IOException {
    server.enqueue(new MockResponse());
    Call<Phone> call = service.get();
    Response<Phone> response = call.execute();
    Phone body = response.body();
    assertThat(body.hasNumber()).isFalse();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Phone(retrofit2.converter.protobuf.PhoneProtos.Phone) Test(org.junit.Test)

Example 12 with Call

use of retrofit2.Call in project retrofit by square.

the class BehaviorDelegate method returning.

// Single-interface proxy creation guarded by parameter safety.
@SuppressWarnings("unchecked")
public <R> T returning(Call<R> call) {
    final Call<R> behaviorCall = new BehaviorCall<>(behavior, executor, call);
    return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class[] { service }, new InvocationHandler() {

        @Override
        public T invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Type returnType = method.getGenericReturnType();
            Annotation[] methodAnnotations = method.getAnnotations();
            CallAdapter<R, T> callAdapter = (CallAdapter<R, T>) retrofit.callAdapter(returnType, methodAnnotations);
            return callAdapter.adapt(behaviorCall);
        }
    });
}
Also used : CallAdapter(retrofit2.CallAdapter) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type)

Example 13 with Call

use of retrofit2.Call in project retrofit by square.

the class CallsTest method deferredThrowExecute.

@Test
public void deferredThrowExecute() throws IOException {
    final IOException failure = new IOException("Hey");
    Call<Object> failing = Calls.defer(new Callable<Call<Object>>() {

        @Override
        public Call<Object> call() throws Exception {
            throw failure;
        }
    });
    try {
        failing.execute();
        fail();
    } catch (IOException e) {
        assertSame(failure, e);
    }
}
Also used : Call(retrofit2.Call) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with Call

use of retrofit2.Call in project retrofit by square.

the class CallTest method requestThrowingBeforeExecuteFailsExecute.

@Test
public void requestThrowingBeforeExecuteFailsExecute() throws IOException {
    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);
    try {
        call.execute();
        fail();
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Broken!");
    }
    assertThat(writeCount.get()).isEqualTo(1);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) Test(org.junit.Test)

Example 15 with Call

use of retrofit2.Call in project retrofit by square.

the class RequestBuilderTest method getWithEncodedPathStillPreventsRequestSplitting.

@Test
public void getWithEncodedPathStillPreventsRequestSplitting() {
    class Example {

        //
        @GET("/foo/bar/{ping}/")
        Call<ResponseBody> method(@Path(value = "ping", encoded = true) String ping) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "baz/\r\npong");
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/baz/pong/");
    assertThat(request.body()).isNull();
}
Also used : Path(retrofit2.http.Path) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)186 Request (okhttp3.Request)169 Test (org.junit.Test)155 Call (okhttp3.Call)116 Response (retrofit2.Response)106 Response (okhttp3.Response)98 Observable (rx.Observable)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 MockResponse (okhttp3.mockwebserver.MockResponse)67 IOException (java.io.IOException)66 RequestBody (okhttp3.RequestBody)50 Callback (okhttp3.Callback)41 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)34 OkHttpClient (okhttp3.OkHttpClient)27 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)27 Buffer (okio.Buffer)19 Call (retrofit2.Call)18 MultipartBody (okhttp3.MultipartBody)17 HttpUrl (okhttp3.HttpUrl)15 Callback (retrofit2.Callback)14