Search in sources :

Example 96 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method getWithQueryParamMap.

@Test
public void getWithQueryParamMap() {
    class Example {

        // 
        @GET("/foo/bar/")
        Call<ResponseBody> method(@QueryMap Map<String, Object> query) {
            return null;
        }
    }
    Map<String, Object> params = new LinkedHashMap<>();
    params.put("kit", "kat");
    params.put("ping", "pong");
    Request request = buildRequest(Example.class, params);
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/?kit=kat&ping=pong");
    assertThat(request.body()).isNull();
}
Also used : QueryMap(retrofit2.http.QueryMap) 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 97 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method fieldParamsSkippedIfConvertedToNull.

@Test
public void fieldParamsSkippedIfConvertedToNull() throws Exception {
    class Example {

        @FormUrlEncoded
        @POST("/query")
        Call<ResponseBody> queryPath(@Field("a") Object a) {
            return null;
        }
    }
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl("http://example.com").addConverterFactory(new NullObjectConverterFactory());
    Request request = buildRequest(Example.class, retrofitBuilder, "Ignored");
    assertThat(request.url().toString()).doesNotContain("Ignored");
}
Also used : NullObjectConverterFactory(retrofit2.helpers.NullObjectConverterFactory) Field(retrofit2.http.Field) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 98 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method customMethodWithBody.

@Test
public void customMethodWithBody() {
    class Example {

        @HTTP(method = "CUSTOM2", path = "/foo", hasBody = true)
        Call<ResponseBody> method(@Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
    Request request = buildRequest(Example.class, body);
    assertThat(request.method()).isEqualTo("CUSTOM2");
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo");
    assertBody(request.body(), "hi");
}
Also used : 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 99 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method getWithQueryNameParamPrimitiveArray.

@Test
public void getWithQueryNameParamPrimitiveArray() {
    class Example {

        // 
        @GET("/foo/bar/")
        Call<ResponseBody> method(@QueryName int[] keys) {
            return null;
        }
    }
    int[] values = { 1, 2, 3, 1 };
    Request request = buildRequest(Example.class, new Object[] { values });
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/?1&2&3&1");
    assertThat(request.body()).isNull();
}
Also used : Request(okhttp3.Request) QueryName(retrofit2.http.QueryName) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 100 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method getWithPathAndQueryParam.

@Test
public void getWithPathAndQueryParam() {
    class Example {

        // 
        @GET("/foo/bar/{ping}/")
        Call<ResponseBody> method(@Path("ping") String ping, @Query("kit") String kit, @Query("riff") String riff) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "pong", "kat", "raff");
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/pong/?kit=kat&riff=raff");
    assertThat(request.body()).isNull();
}
Also used : Path(retrofit2.http.Path) Query(retrofit2.http.Query) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)82 ResponseBody (okhttp3.ResponseBody)76 Request (okhttp3.Request)72 Retrofit (retrofit2.Retrofit)39 OkHttpClient (okhttp3.OkHttpClient)28 RequestBody (okhttp3.RequestBody)22 IOException (java.io.IOException)19 Query (retrofit2.http.Query)15 MultipartBody (okhttp3.MultipartBody)12 Buffer (okio.Buffer)12 Path (retrofit2.http.Path)12 Interceptor (okhttp3.Interceptor)11 HttpUrl (okhttp3.HttpUrl)10 Response (okhttp3.Response)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Response (retrofit2.Response)9 Part (retrofit2.http.Part)9 List (java.util.List)8 QueryMap (retrofit2.http.QueryMap)8