Search in sources :

Example 31 with Call

use of retrofit2.Call 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 32 with Call

use of retrofit2.Call in project retrofit by square.

the class RequestBuilderTest method malformedContentTypeHeaderThrows.

@Test
public void malformedContentTypeHeaderThrows() {
    class Example {

        //
        @POST("/")
        //
        @Headers("Content-Type: hello, world!")
        Call<ResponseBody> method(@Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
    try {
        buildRequest(Example.class, body);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("Malformed content type: hello, world!\n" + "    for method Example.method");
    }
}
Also used : RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 33 with Call

use of retrofit2.Call 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)

Example 34 with Call

use of retrofit2.Call in project retrofit by square.

the class RequestBuilderTest method getWithQueryNameParam.

@Test
public void getWithQueryNameParam() {
    class Example {

        //
        @GET("/foo/bar/")
        Call<ResponseBody> method(@QueryName String ping) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "pong");
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/?pong");
    assertThat(request.body()).isNull();
}
Also used : Request(okhttp3.Request) QueryName(retrofit2.http.QueryName) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 35 with Call

use of retrofit2.Call in project retrofit by square.

the class RequestBuilderTest method getWithEncodedPathParam.

@Test
public void getWithEncodedPathParam() {
    class Example {

        //
        @GET("/foo/bar/{ping}/")
        Call<ResponseBody> method(@Path(value = "ping", encoded = true) String ping) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "po%20ng");
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.headers().size()).isZero();
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/po%20ng/");
    assertThat(request.body()).isNull();
}
Also used : Path(retrofit2.http.Path) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)189 Request (okhttp3.Request)176 Test (org.junit.Test)155 Call (okhttp3.Call)127 Response (retrofit2.Response)107 Response (okhttp3.Response)105 Observable (rx.Observable)94 ServiceResponse (com.microsoft.rest.ServiceResponse)92 IOException (java.io.IOException)72 MockResponse (okhttp3.mockwebserver.MockResponse)67 RequestBody (okhttp3.RequestBody)51 Callback (okhttp3.Callback)44 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)34 OkHttpClient (okhttp3.OkHttpClient)33 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)27 Buffer (okio.Buffer)19 Call (retrofit2.Call)19 MultipartBody (okhttp3.MultipartBody)17 HttpUrl (okhttp3.HttpUrl)15 Callback (retrofit2.Callback)15