Search in sources :

Example 1 with Part

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

the class RequestBuilderTest method multipartPartsShouldBeInOrder.

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

        @Multipart
        @POST("/foo")
        Call<ResponseBody> get(@Part("first") String data, @Part("second") String dataTwo, @Part("third") String dataThree) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "firstParam", "secondParam", "thirdParam");
    MultipartBody body = (MultipartBody) request.body();
    Buffer buffer = new Buffer();
    body.writeTo(buffer);
    String readBody = buffer.readUtf8();
    assertThat(readBody.indexOf("firstParam")).isLessThan(readBody.indexOf("secondParam"));
    assertThat(readBody.indexOf("secondParam")).isLessThan(readBody.indexOf("thirdParam"));
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) MultipartBody(okhttp3.MultipartBody) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 2 with Part

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

the class RequestBuilderTest method multipartWithEncoding.

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

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part(value = "ping", encoding = "8-bit") String ping, @Part(value = "kit", encoding = "7-bit") RequestBody kit) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "pong", RequestBody.create(MediaType.parse("text/plain"), "kat"));
    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: 7-bit").contains("\r\nkat\r\n--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 3 with Part

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

the class RequestBuilderTest method multipartIterable.

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

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part("ping") List<String> ping) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, Arrays.asList("pong1", "pong2"));
    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\npong1\r\n--");
    assertThat(bodyString).contains("Content-Disposition: form-data;").contains("name=\"ping\"").contains("\r\npong2\r\n--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) Request(okhttp3.Request) List(java.util.List) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 4 with Part

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

the class RequestBuilderTest method multipartOkHttpArrayPart.

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

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part 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, new Object[] { new MultipartBody.Part[] { 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--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) MultipartBody(okhttp3.MultipartBody) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 5 with Part

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

the class RequestBuilderTest method multipartNullRemovesPart.

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

        //
        @Multipart
        //
        @POST("/foo/bar/")
        Call<ResponseBody> method(@Part("ping") String ping, @Part("fizz") String fizz) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "pong", null);
    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\"").contains("\r\npong\r\n--");
}
Also used : Buffer(okio.Buffer) Part(retrofit2.http.Part) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Aggregations

Request (okhttp3.Request)10 ResponseBody (okhttp3.ResponseBody)10 Buffer (okio.Buffer)10 Test (org.junit.Test)10 Part (retrofit2.http.Part)10 RequestBody (okhttp3.RequestBody)9 MultipartBody (okhttp3.MultipartBody)5 List (java.util.List)2