Search in sources :

Example 21 with retrofit2.http

use of retrofit2.http in project autorest-clientruntime-for-java by Azure.

the class UserAgentTests method customUserAgentTests.

@Test
public void customUserAgentTests() throws Exception {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().addInterceptor(new UserAgentInterceptor().withUserAgent("Awesome")).addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            String header = chain.request().header("User-Agent");
            Assert.assertEquals("Awesome", header);
            return new Response.Builder().request(chain.request()).code(200).protocol(Protocol.HTTP_1_1).build();
        }
    });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) {
    };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().get().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) Request(okhttp3.Request) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) Test(org.junit.Test)

Example 22 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method getWithEncodedQueryParam.

@Test
public void getWithEncodedQueryParam() {
    class Example {

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

Example 23 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method fieldParamMapsConvertedToNullShouldError.

@Test
public void fieldParamMapsConvertedToNullShouldError() throws Exception {
    class Example {

        @FormUrlEncoded
        @POST("/query")
        Call<ResponseBody> queryPath(@FieldMap Map<String, String> a) {
            return null;
        }
    }
    Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl("http://example.com").addConverterFactory(new NullObjectConverterFactory());
    Map<String, String> queryMap = Collections.singletonMap("kit", "kat");
    try {
        buildRequest(Example.class, retrofitBuilder, queryMap);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessageContaining("Field map value 'kat' converted to null by retrofit2.helpers.NullObjectConverterFactory$1 for key 'kit'.");
    }
}
Also used : NullObjectConverterFactory(retrofit2.helpers.NullObjectConverterFactory) FieldMap(retrofit2.http.FieldMap) 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) Test(org.junit.Test)

Example 24 with retrofit2.http

use of retrofit2.http in project retrofit by square.

the class RequestBuilderTest method getWithUnusedAndInvalidNamedPathParam.

@Test
public void getWithUnusedAndInvalidNamedPathParam() {
    class Example {

        // 
        @GET("/foo/bar/{ping}/{kit,kat}/")
        Call<ResponseBody> method(@Path("ping") 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/%7Bkit,kat%7D/");
    assertThat(request.body()).isNull();
}
Also used : Path(retrofit2.http.Path) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 25 with retrofit2.http

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

Aggregations

Test (org.junit.Test)81 ResponseBody (okhttp3.ResponseBody)76 Request (okhttp3.Request)72 Retrofit (retrofit2.Retrofit)38 OkHttpClient (okhttp3.OkHttpClient)27 RequestBody (okhttp3.RequestBody)22 IOException (java.io.IOException)19 Query (retrofit2.http.Query)15 MultipartBody (okhttp3.MultipartBody)12 Buffer (okio.Buffer)12 Path (retrofit2.http.Path)12 Interceptor (okhttp3.Interceptor)11 HttpUrl (okhttp3.HttpUrl)10 Response (okhttp3.Response)10 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Response (retrofit2.Response)9 Part (retrofit2.http.Part)9 List (java.util.List)8 QueryMap (retrofit2.http.QueryMap)8