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