Search in sources :

Example 16 with Call

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");
}
Also used : FieldMap(retrofit2.http.FieldMap) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) HashMap(java.util.HashMap) HeaderMap(retrofit2.http.HeaderMap) FieldMap(retrofit2.http.FieldMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) QueryMap(retrofit2.http.QueryMap) ResponseBody(okhttp3.ResponseBody) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 17 with Call

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);
    }
}
Also used : Request(okhttp3.Request) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) IOException(java.io.IOException) POST(retrofit2.http.POST) PUT(retrofit2.http.PUT) GET(retrofit2.http.GET) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory)

Example 18 with Call

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!");
}
Also used : Path(retrofit2.http.Path) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 19 with Call

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();
}
Also used : Path(retrofit2.http.Path) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 20 with Call

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();
}
Also used : Header(retrofit2.http.Header) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)186 Request (okhttp3.Request)169 Test (org.junit.Test)155 Call (okhttp3.Call)116 Response (retrofit2.Response)106 Response (okhttp3.Response)98 Observable (rx.Observable)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 MockResponse (okhttp3.mockwebserver.MockResponse)67 IOException (java.io.IOException)66 RequestBody (okhttp3.RequestBody)50 Callback (okhttp3.Callback)41 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)34 OkHttpClient (okhttp3.OkHttpClient)27 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)27 Buffer (okio.Buffer)19 Call (retrofit2.Call)18 MultipartBody (okhttp3.MultipartBody)17 HttpUrl (okhttp3.HttpUrl)15 Callback (retrofit2.Callback)14