use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method http404Async.
@Test
public void http404Async() throws InterruptedException, IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setResponseCode(404).setBody("Hi"));
final AtomicReference<Response<String>> responseRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
example.getString().enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
responseRef.set(response);
latch.countDown();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
t.printStackTrace();
}
});
assertTrue(latch.await(10, SECONDS));
Response<String> response = responseRef.get();
assertThat(response.isSuccessful()).isFalse();
assertThat(response.code()).isEqualTo(404);
assertThat(response.errorBody().string()).isEqualTo("Hi");
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method requestAfterExecuteReturnsCachedValue.
@Test
public void requestAfterExecuteReturnsCachedValue() 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.execute();
assertThat(writeCount.get()).isEqualTo(1);
call.request();
assertThat(writeCount.get()).isEqualTo(1);
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method http404Sync.
@Test
public void http404Sync() throws IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setResponseCode(404).setBody("Hi"));
Response<String> response = example.getString().execute();
assertThat(response.isSuccessful()).isFalse();
assertThat(response.code()).isEqualTo(404);
assertThat(response.errorBody().string()).isEqualTo("Hi");
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method requestAfterEnqueueReturnsCachedValue.
@Test
public void requestAfterEnqueueReturnsCachedValue() 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();
return "Hello";
}
};
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) {
assertThat(writeCount.get()).isEqualTo(1);
latch.countDown();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
assertTrue(latch.await(10, SECONDS));
call.request();
assertThat(writeCount.get()).isEqualTo(1);
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method requestBeforeEnqueueCreates.
@Test
public void requestBeforeEnqueueCreates() 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();
return "Hello";
}
};
Call<String> call = service.postRequestBody(a);
call.request();
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) {
assertThat(writeCount.get()).isEqualTo(1);
latch.countDown();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
assertTrue(latch.await(10, SECONDS));
}
Aggregations