use of retrofit2.http.Multipart in project retrofit by square.
the class RequestBuilderTest method multipartOkHttpPart.
@Test
public void multipartOkHttpPart() throws IOException {
class Example {
//
@Multipart
//
@POST("/foo/bar/")
Call<ResponseBody> method(@Part MultipartBody.Part part) {
return null;
}
}
MultipartBody.Part part = MultipartBody.Part.createFormData("kit", "kat");
Request request = buildRequest(Example.class, part);
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=\"kit\"\r\n").contains("\r\nkat\r\n--");
}
use of retrofit2.http.Multipart in project retrofit by square.
the class RequestBuilderTest method multipartOkHttpPartWithFilename.
@Test
public void multipartOkHttpPartWithFilename() throws IOException {
class Example {
//
@Multipart
//
@POST("/foo/bar/")
Call<ResponseBody> method(@Part MultipartBody.Part part) {
return null;
}
}
MultipartBody.Part part = MultipartBody.Part.createFormData("kit", "kit.txt", RequestBody.create(null, "kat"));
Request request = buildRequest(Example.class, part);
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=\"kit\"; filename=\"kit.txt\"\r\n").contains("\r\nkat\r\n--");
}
use of retrofit2.http.Multipart 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.Multipart 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.Multipart in project retrofit by square.
the class RequestBuilderTest method multipartOkHttpIterablePart.
@Test
public void multipartOkHttpIterablePart() throws IOException {
class Example {
//
@Multipart
//
@POST("/foo/bar/")
Call<ResponseBody> method(@Part List<MultipartBody.Part> part) {
return null;
}
}
MultipartBody.Part part1 = MultipartBody.Part.createFormData("foo", "bar");
MultipartBody.Part part2 = MultipartBody.Part.createFormData("kit", "kat");
Request request = buildRequest(Example.class, Arrays.asList(part1, part2));
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=\"foo\"\r\n").contains("\r\nbar\r\n--");
assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"kit\"\r\n").contains("\r\nkat\r\n--");
}
Aggregations