use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method emptyResponse.
@Test
public void emptyResponse() 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("").addHeader("Content-Type", "text/stringy"));
Response<String> response = example.getString().execute();
assertThat(response.body()).isEqualTo("");
ResponseBody rawBody = response.raw().body();
assertThat(rawBody.contentLength()).isEqualTo(0);
assertThat(rawBody.contentType().toString()).isEqualTo("text/stringy");
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method conversionProblemIncomingMaskedByConverterIsUnwrapped.
@Test
public void conversionProblemIncomingMaskedByConverterIsUnwrapped() throws IOException {
// MWS has no way to trigger IOExceptions during the response body so use an interceptor.
OkHttpClient client = //
new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response response = chain.proceed(chain.request());
ResponseBody body = response.body();
BufferedSource source = Okio.buffer(new ForwardingSource(body.source()) {
@Override
public long read(Buffer sink, long byteCount) throws IOException {
throw new IOException("cause");
}
});
body = ResponseBody.create(body.contentType(), body.contentLength(), source);
return response.newBuilder().body(body).build();
}
}).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).client(client).addConverterFactory(new ToStringConverterFactory() {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
try {
return value.string();
} catch (IOException e) {
// Some serialization libraries mask transport problems in runtime exceptions. Bad!
throw new RuntimeException("wrapper", e);
}
}
};
}
}).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
Call<String> call = example.getString();
try {
call.execute();
fail();
} catch (IOException e) {
assertThat(e).hasMessage("cause");
}
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method conversionProblemOutgoingAsync.
@Test
public void conversionProblemOutgoingAsync() throws InterruptedException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory() {
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return new Converter<String, RequestBody>() {
@Override
public RequestBody convert(String value) throws IOException {
throw new UnsupportedOperationException("I am broken!");
}
};
}
}).build();
Service example = retrofit.create(Service.class);
final AtomicReference<Throwable> failureRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
example.postString("Hi").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();
}
});
assertTrue(latch.await(10, SECONDS));
assertThat(failureRef.get()).isInstanceOf(UnsupportedOperationException.class).hasMessage("I am broken!");
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method rawResponseContentTypeAndLengthButNoSource.
@Test
public void rawResponseContentTypeAndLengthButNoSource() 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("Hi").addHeader("Content-Type", "text/greeting"));
Response<String> response = example.getString().execute();
assertThat(response.body()).isEqualTo("Hi");
ResponseBody rawBody = response.raw().body();
assertThat(rawBody.contentLength()).isEqualTo(2);
assertThat(rawBody.contentType().toString()).isEqualTo("text/greeting");
try {
rawBody.source();
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Cannot read raw response body of a converted body.");
}
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method conversionProblemIncomingAsync.
@Test
public void conversionProblemIncomingAsync() throws InterruptedException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory() {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
throw new UnsupportedOperationException("I am broken!");
}
};
}
}).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
final AtomicReference<Throwable> failureRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
example.postString("Hi").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();
}
});
assertTrue(latch.await(10, SECONDS));
assertThat(failureRef.get()).isInstanceOf(UnsupportedOperationException.class).hasMessage("I am broken!");
}
Aggregations