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