Search in sources :

Example 1 with PartMap

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

the class RequestBuilderTest method multipartPartMap.

@Test
public void multipartPartMap() throws IOException {
    class Example {

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) {
            return null;
        }
    }
    Map<String, RequestBody> params = new LinkedHashMap<>();
    params.put("ping", RequestBody.create(null, "pong"));
    params.put("kit", RequestBody.create(null, "kat"));
    Request request = buildRequest(Example.class, params);
    assertThat(request.method()).isEqualTo("POST");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    RequestBody body = request.body();
    Buffer buffer = new Buffer();
    body.writeTo(buffer);
    String bodyString = buffer.readUtf8();
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"ping\"\r\n").contains("\r\npong\r\n--");
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"kit\"").contains("\r\nkat\r\n--");
}
Also used : Buffer(okio.Buffer) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) 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) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 2 with PartMap

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

the class RequestBuilderTest method multipartPartMapWithEncoding.

@Test
public void multipartPartMapWithEncoding() throws IOException {
    class Example {

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@PartMap(encoding = "8-bit") Map<String, RequestBody> parts) {
            return null;
        }
    }
    Map<String, RequestBody> params = new LinkedHashMap<>();
    params.put("ping", RequestBody.create(null, "pong"));
    params.put("kit", RequestBody.create(null, "kat"));
    Request request = buildRequest(Example.class, params);
    assertThat(request.method()).isEqualTo("POST");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    RequestBody body = request.body();
    Buffer buffer = new Buffer();
    body.writeTo(buffer);
    String bodyString = buffer.readUtf8();
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"ping\"\r\n").contains("Content-Transfer-Encoding: 8-bit").contains("\r\npong\r\n--");
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"kit\"").contains("Content-Transfer-Encoding: 8-bit").contains("\r\nkat\r\n--");
}
Also used : Buffer(okio.Buffer) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) 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) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 3 with PartMap

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

the class RequestBuilderTest method multipartPartMapSupportsSubclasses.

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

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@PartMap Foo parts) {
            return null;
        }
    }
    Foo foo = new Foo();
    foo.put("hello", "world");
    Request request = buildRequest(Example.class, foo);
    Buffer buffer = new Buffer();
    request.body().writeTo(buffer);
    assertThat(buffer.readUtf8()).contains("name=\"hello\"").contains("\r\n\r\nworld\r\n--");
}
Also used : Buffer(okio.Buffer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Request (okhttp3.Request)3 ResponseBody (okhttp3.ResponseBody)3 Buffer (okio.Buffer)3 Test (org.junit.Test)3 PartMap (retrofit2.http.PartMap)3 Map (java.util.Map)2 RequestBody (okhttp3.RequestBody)2 FieldMap (retrofit2.http.FieldMap)2 HeaderMap (retrofit2.http.HeaderMap)2 QueryMap (retrofit2.http.QueryMap)2