Search in sources :

Example 1 with Decoder

use of feign.codec.Decoder in project feign by OpenFeign.

the class BaseApiTest method resolvesBodyParameter.

@Test
public void resolvesBodyParameter() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("foo"));
    String baseUrl = server.url("/default").toString();
    Feign.builder().encoder(new Encoder() {

        @Override
        public void encode(Object object, Type bodyType, RequestTemplate template) {
            assertThat(bodyType).isEqualTo(new TypeToken<Keys<String>>() {
            }.getType());
        }
    }).decoder(new Decoder() {

        @Override
        public Object decode(Response response, Type type) {
            assertThat(type).isEqualTo(new TypeToken<Entities<String, Long>>() {
            }.getType());
            return null;
        }
    }).target(MyApi.class, baseUrl).getAll(new Keys<String>());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Decoder(feign.codec.Decoder) MockResponse(okhttp3.mockwebserver.MockResponse) Type(java.lang.reflect.Type) Encoder(feign.codec.Encoder) TypeToken(com.google.gson.reflect.TypeToken) Test(org.junit.Test)

Example 2 with Decoder

use of feign.codec.Decoder in project feign by OpenFeign.

the class BaseApiTest method resolvesParameterizedResult.

@Test
public void resolvesParameterizedResult() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("foo"));
    String baseUrl = server.url("/default").toString();
    Feign.builder().decoder(new Decoder() {

        @Override
        public Object decode(Response response, Type type) {
            assertThat(type).isEqualTo(new TypeToken<Entity<String, Long>>() {
            }.getType());
            return null;
        }
    }).target(MyApi.class, baseUrl).get("foo");
    assertThat(server.takeRequest()).hasPath("/default/api/foo");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Type(java.lang.reflect.Type) Decoder(feign.codec.Decoder) Test(org.junit.Test)

Example 3 with Decoder

use of feign.codec.Decoder in project feign by OpenFeign.

the class FeignBuilderTest method testOverrideDecoder.

@Test
public void testOverrideDecoder() throws Exception {
    server.enqueue(new MockResponse().setBody("success!"));
    String url = "http://localhost:" + server.getPort();
    Decoder decoder = new Decoder() {

        @Override
        public Object decode(Response response, Type type) {
            return "fail";
        }
    };
    TestInterface api = Feign.builder().decoder(decoder).target(TestInterface.class, url);
    assertEquals("fail", api.decodedPost());
    assertEquals(1, server.getRequestCount());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Type(java.lang.reflect.Type) Decoder(feign.codec.Decoder) Test(org.junit.Test)

Aggregations

Decoder (feign.codec.Decoder)3 Type (java.lang.reflect.Type)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Test (org.junit.Test)3 TypeToken (com.google.gson.reflect.TypeToken)1 Encoder (feign.codec.Encoder)1