use of retrofit2.Call in project retrofit by square.
the class CallTest method requestBeforeExecuteCreates.
@Test
public void requestBeforeExecuteCreates() 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();
return "Hello";
}
};
Call<String> call = service.postRequestBody(a);
call.request();
assertThat(writeCount.get()).isEqualTo(1);
call.execute();
assertThat(writeCount.get()).isEqualTo(1);
}
use of retrofit2.Call 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);
}
use of retrofit2.Call 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");
}
use of retrofit2.Call in project retrofit by square.
the class RequestBuilderTest method getWithEncodedQueryParam.
@Test
public void getWithEncodedQueryParam() {
class Example {
//
@GET("/foo/bar/")
Call<ResponseBody> method(@Query(value = "pi%20ng", encoded = true) String ping) {
return null;
}
}
Request request = buildRequest(Example.class, "p%20o%20n%20g");
assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/?pi%20ng=p%20o%20n%20g");
assertThat(request.body()).isNull();
}
use of retrofit2.Call in project retrofit by square.
the class RequestBuilderTest method getWithUrlAbsoluteSameHost.
@Test
public void getWithUrlAbsoluteSameHost() {
class Example {
@GET
Call<ResponseBody> method(@Url String url) {
return null;
}
}
Request request = buildRequest(Example.class, "http://example.com/foo/bar/");
assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
Aggregations