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