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--");
}
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--");
}
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--");
}
Aggregations