Search in sources :

Example 1 with Encoder

use of feign.codec.Encoder 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 Encoder

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

the class FeignBuilderTest method testOverrideEncoder.

@Test
public void testOverrideEncoder() throws Exception {
    server.enqueue(new MockResponse().setBody("response data"));
    String url = "http://localhost:" + server.getPort();
    Encoder encoder = new Encoder() {

        @Override
        public void encode(Object object, Type bodyType, RequestTemplate template) {
            template.body(object.toString());
        }
    };
    TestInterface api = Feign.builder().encoder(encoder).target(TestInterface.class, url);
    api.encodedPost(Arrays.asList("This", "is", "my", "request"));
    assertThat(server.takeRequest()).hasBody("[This, is, my, request]");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Type(java.lang.reflect.Type) Encoder(feign.codec.Encoder) Test(org.junit.Test)

Example 3 with Encoder

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

the class JAXBCodecTest method encodesXmlWithCustomJAXBNoNamespaceSchemaLocation.

@Test
public void encodesXmlWithCustomJAXBNoNamespaceSchemaLocation() throws Exception {
    JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().withMarshallerNoNamespaceSchemaLocation("http://apihost/schema.xsd").build();
    Encoder encoder = new JAXBEncoder(jaxbContextFactory);
    MockObject mock = new MockObject();
    mock.value = "Test";
    RequestTemplate template = new RequestTemplate();
    encoder.encode(mock, MockObject.class, template);
    assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-8\" " + "standalone=\"yes\"?><mockObject xsi:noNamespaceSchemaLocation=\"http://apihost/schema.xsd\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<value>Test</value></mockObject>");
}
Also used : Encoder(feign.codec.Encoder) RequestTemplate(feign.RequestTemplate) Test(org.junit.Test)

Example 4 with Encoder

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

the class JAXBCodecTest method encodesXmlWithCustomJAXBFormattedOutput.

@Test
public void encodesXmlWithCustomJAXBFormattedOutput() {
    JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().withMarshallerFormattedOutput(true).build();
    Encoder encoder = new JAXBEncoder(jaxbContextFactory);
    MockObject mock = new MockObject();
    mock.value = "Test";
    RequestTemplate template = new RequestTemplate();
    encoder.encode(mock, MockObject.class, template);
    String NEWLINE = System.getProperty("line.separator");
    assertThat(template).hasBody(new StringBuilder().append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>").append(NEWLINE).append("<mockObject>").append(NEWLINE).append("    <value>Test</value>").append(NEWLINE).append("</mockObject>").append(NEWLINE).toString());
}
Also used : Encoder(feign.codec.Encoder) RequestTemplate(feign.RequestTemplate) Test(org.junit.Test)

Example 5 with Encoder

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

the class JAXBCodecTest method encodesXmlWithCustomJAXBEncoding.

@Test
public void encodesXmlWithCustomJAXBEncoding() throws Exception {
    JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-16").build();
    Encoder encoder = new JAXBEncoder(jaxbContextFactory);
    MockObject mock = new MockObject();
    mock.value = "Test";
    RequestTemplate template = new RequestTemplate();
    encoder.encode(mock, MockObject.class, template);
    assertThat(template).hasBody("<?xml version=\"1.0\" encoding=\"UTF-16\" " + "standalone=\"yes\"?><mockObject><value>Test</value></mockObject>");
}
Also used : Encoder(feign.codec.Encoder) RequestTemplate(feign.RequestTemplate) Test(org.junit.Test)

Aggregations

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