Search in sources :

Example 1 with QueryMap

use of retrofit2.http.QueryMap in project retrofit by square.

the class RequestBuilderTest method fieldParamMapsConvertedToNullShouldError.

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

        @FormUrlEncoded
        @POST("/query")
        Call<ResponseBody> queryPath(@FieldMap Map<String, String> a) {
            return null;
        }
    }
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl("http://example.com").addConverterFactory(new NullObjectConverterFactory());
    Map<String, String> queryMap = Collections.singletonMap("kit", "kat");
    try {
        buildRequest(Example.class, retrofitBuilder, queryMap);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessageContaining("Field map value 'kat' converted to null by retrofit2.helpers.NullObjectConverterFactory$1 for key 'kit'.");
    }
}
Also used : NullObjectConverterFactory(retrofit2.helpers.NullObjectConverterFactory) FieldMap(retrofit2.http.FieldMap) 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) Test(org.junit.Test)

Example 2 with QueryMap

use of retrofit2.http.QueryMap in project retrofit by square.

the class RequestBuilderTest method queryMapSupportsSubclasses.

@Test
public void queryMapSupportsSubclasses() {
    class Foo extends HashMap<String, String> {
    }
    class Example {

        // 
        @GET("/")
        Call<ResponseBody> method(@QueryMap Foo a) {
            return null;
        }
    }
    Foo foo = new Foo();
    foo.put("hello", "world");
    Request request = buildRequest(Example.class, foo);
    assertThat(request.url().toString()).isEqualTo("http://example.com/?hello=world");
}
Also used : QueryMap(retrofit2.http.QueryMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 3 with QueryMap

use of retrofit2.http.QueryMap in project retrofit by square.

the class RequestBuilderTest method getWithEncodedQueryParamMap.

@Test
public void getWithEncodedQueryParamMap() {
    class Example {

        // 
        @GET("/foo/bar/")
        Call<ResponseBody> method(@QueryMap(encoded = true) Map<String, Object> query) {
            return null;
        }
    }
    Map<String, Object> params = new LinkedHashMap<>();
    params.put("kit", "k%20t");
    params.put("pi%20ng", "p%20g");
    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=k%20t&pi%20ng=p%20g");
    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 4 with QueryMap

use of retrofit2.http.QueryMap 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 5 with QueryMap

use of retrofit2.http.QueryMap in project retrofit by square.

the class RequestBuilderTest method queryParamMapsConvertedToNullShouldError.

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

        @GET("/query")
        Call<ResponseBody> queryPath(@QueryMap Map<String, String> a) {
            return null;
        }
    }
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl("http://example.com").addConverterFactory(new NullObjectConverterFactory());
    Map<String, String> queryMap = Collections.singletonMap("kit", "kat");
    try {
        buildRequest(Example.class, retrofitBuilder, queryMap);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessageContaining("Query map value 'kat' converted to null by retrofit2.helpers.NullObjectConverterFactory$1 for key 'kit'.");
    }
}
Also used : NullObjectConverterFactory(retrofit2.helpers.NullObjectConverterFactory) QueryMap(retrofit2.http.QueryMap) 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) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 ResponseBody (okhttp3.ResponseBody)5 Test (org.junit.Test)5 QueryMap (retrofit2.http.QueryMap)5 Map (java.util.Map)4 FieldMap (retrofit2.http.FieldMap)4 HeaderMap (retrofit2.http.HeaderMap)4 PartMap (retrofit2.http.PartMap)4 Request (okhttp3.Request)3 NullObjectConverterFactory (retrofit2.helpers.NullObjectConverterFactory)2