Commit ed137a93 by Tomasz Juchniewicz Committed by Spencer Gibb

Fix encoding issue in SpringEncoder for binary data (#1699)

* Fix encoding issue in SpringEncoder for binary data Fixes gh-1698 * Fix encoding issue in SpringEncoder for binary data Fixes gh-1698 * Fix encoding issue in SpringEncoder for binary data Add unit test. Fixes gh-1698
parent e73ee5f6
......@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import feign.RequestTemplate;
......@@ -95,8 +96,12 @@ public class SpringEncoder implements Encoder {
// with the modified headers
request.headers(getHeaders(outputMessage.getHeaders()));
request.body(outputMessage.getOutputStream().toByteArray(),
Charset.forName("UTF-8")); // TODO: set charset
// do not use charset for binary data
if (messageConverter instanceof ByteArrayHttpMessageConverter) {
request.body(outputMessage.getOutputStream().toByteArray(), null);
} else {
request.body(outputMessage.getOutputStream().toByteArray(), Charset.forName("UTF-8"));
}
return;
}
}
......
......@@ -2,10 +2,12 @@ package org.springframework.cloud.netflix.feign.support;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Collection;
import org.junit.Test;
......@@ -63,6 +65,20 @@ public class SpringEncoderTests {
String header = contentTypeHeader.iterator().next();
assertThat("content type header is wrong", header, is("application/mytype"));
assertThat("request charset is null", request.charset(), is(notNullValue()));
assertThat("request charset is wrong", request.charset(), is(Charset.forName("UTF-8")));
}
@Test
public void testBinaryData() {
SpringEncoder encoder = this.context.getInstance("foo", SpringEncoder.class);
assertThat(encoder, is(notNullValue()));
RequestTemplate request = new RequestTemplate();
encoder.encode("hi".getBytes(), null, request);
assertThat("request charset is not null", request.charset(), is(nullValue()));
}
class MediaTypeMatcher extends ArgumentMatcher<MediaType> {
......@@ -129,8 +145,11 @@ public class SpringEncoderTests {
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
if (clazz == String.class) {
return true;
}
return false;
}
@Override
protected void writeInternal(Object o, Type type,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment