use of retrofit2.Call in project retrofit by square.
the class RequestBuilderTest method formEncodedWithEncodedNameFieldParamMap.
@Test
public void formEncodedWithEncodedNameFieldParamMap() {
class Example {
//
@FormUrlEncoded
//
@POST("/foo")
Call<ResponseBody> method(@FieldMap(encoded = true) Map<String, Object> fieldMap) {
return null;
}
}
Map<String, Object> fieldMap = new LinkedHashMap<>();
fieldMap.put("k%20it", "k%20at");
fieldMap.put("pin%20g", "po%20ng");
Request request = buildRequest(Example.class, fieldMap);
assertBody(request.body(), "k%20it=k%20at&pin%20g=po%20ng");
}
use of retrofit2.Call in project retrofit by square.
the class RequestBuilderTest method buildRequest.
static <T> Request buildRequest(Class<T> cls, Object... args) {
final AtomicReference<Request> requestRef = new AtomicReference<>();
okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {
@Override
public okhttp3.Call newCall(Request request) {
requestRef.set(request);
throw new UnsupportedOperationException("Not implemented");
}
};
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(new ToStringConverterFactory()).callFactory(callFactory).build();
Method method = TestingUtils.onlyMethod(cls);
//noinspection unchecked
ServiceMethod<T, Call<T>> serviceMethod = (ServiceMethod<T, Call<T>>) retrofit.loadServiceMethod(method);
Call<T> okHttpCall = new OkHttpCall<>(serviceMethod, args);
Call<T> call = serviceMethod.callAdapter.adapt(okHttpCall);
try {
call.execute();
throw new AssertionError();
} catch (UnsupportedOperationException ignored) {
return requestRef.get();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new AssertionError(e);
}
}
use of retrofit2.Call in project retrofit by square.
the class RequestBuilderTest method normalPostWithPathParam.
@Test
public void normalPostWithPathParam() {
class Example {
//
@POST("/foo/bar/{ping}/")
Call<ResponseBody> method(@Path("ping") String ping, @Body RequestBody body) {
return null;
}
}
RequestBody body = RequestBody.create(TEXT_PLAIN, "Hi!");
Request request = buildRequest(Example.class, "pong", body);
assertThat(request.method()).isEqualTo("POST");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/pong/");
assertBody(request.body(), "Hi!");
}
use of retrofit2.Call in project retrofit by square.
the class RequestBuilderTest method getWithEncodedPathSegments.
@Test
public void getWithEncodedPathSegments() {
class Example {
//
@GET("/foo/bar/{ping}/")
Call<ResponseBody> method(@Path(value = "ping", encoded = true) String ping) {
return null;
}
}
Request request = buildRequest(Example.class, "baz/pong/more");
assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/baz/pong/more/");
assertThat(request.body()).isNull();
}
use of retrofit2.Call in project retrofit by square.
the class RequestBuilderTest method headerParam.
@Test
public void headerParam() {
class Example {
//
@GET("/foo/bar/")
//
@Headers("ping: pong")
Call<ResponseBody> method(@Header("kit") String kit) {
return null;
}
}
Request request = buildRequest(Example.class, "kat");
assertThat(request.method()).isEqualTo("GET");
okhttp3.Headers headers = request.headers();
assertThat(headers.size()).isEqualTo(2);
assertThat(headers.get("ping")).isEqualTo("pong");
assertThat(headers.get("kit")).isEqualTo("kat");
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
Aggregations