use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method successfulRequestResponseWhenMimeTypeMissing.
@Test
public void successfulRequestResponseWhenMimeTypeMissing() throws Exception {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi").removeHeader("Content-Type"));
Response<String> response = example.getString().execute();
assertThat(response.body()).isEqualTo("Hi");
}
use of retrofit2.Retrofit in project retrofit by square.
the class RetrofitTest method stringConverterReturningNullResultsInDefault.
@Test
public void stringConverterReturningNullResultsInDefault() {
final AtomicBoolean factoryCalled = new AtomicBoolean();
class MyConverterFactory extends Converter.Factory {
@Override
public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
factoryCalled.set(true);
return null;
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
CallMethod service = retrofit.create(CallMethod.class);
Call<ResponseBody> call = service.queryObject(null);
assertThat(call).isNotNull();
assertThat(factoryCalled.get()).isTrue();
}
use of retrofit2.Retrofit in project retrofit by square.
the class RetrofitTest method argumentCapture.
/** Confirm that Retrofit encodes parameters when the call is executed, and not earlier. */
@Test
public void argumentCapture() throws Exception {
AtomicInteger i = new AtomicInteger();
server.enqueue(new MockResponse().setBody("a"));
server.enqueue(new MockResponse().setBody("b"));
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
MutableParameters mutableParameters = retrofit.create(MutableParameters.class);
i.set(100);
Call<String> call1 = mutableParameters.method(i);
i.set(101);
Response<String> response1 = call1.execute();
i.set(102);
assertEquals("a", response1.body());
assertEquals("/?i=101", server.takeRequest().getPath());
i.set(200);
Call<String> call2 = call1.clone();
i.set(201);
Response<String> response2 = call2.execute();
i.set(202);
assertEquals("b", response2.body());
assertEquals("/?i=201", server.takeRequest().getPath());
}
use of retrofit2.Retrofit in project retrofit by square.
the class RetrofitTest method requestConverterFactoryNoMatchThrows.
@Test
public void requestConverterFactoryNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingConverterFactory nonMatchingFactory = new NonMatchingConverterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory).build();
try {
retrofit.requestBodyConverter(type, annotations, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate RequestBody converter for class java.lang.String.\n" + " Tried:\n" + " * retrofit2.BuiltInConverters\n" + " * retrofit2.helpers.NonMatchingConverterFactory");
}
assertThat(nonMatchingFactory.called).isTrue();
}
use of retrofit2.Retrofit in project retrofit by square.
the class RetrofitTest method callAdapterFactoryNoMatchThrows.
@Test
public void callAdapterFactoryNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingCallAdapterFactory nonMatchingFactory = new NonMatchingCallAdapterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addCallAdapterFactory(nonMatchingFactory).build();
try {
retrofit.callAdapter(type, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate call adapter for class java.lang.String.\n" + " Tried:\n" + " * retrofit2.helpers.NonMatchingCallAdapterFactory\n" + " * retrofit2.DefaultCallAdapterFactory");
}
assertThat(nonMatchingFactory.called).isTrue();
}
Aggregations