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; ...@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage; import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import feign.RequestTemplate; import feign.RequestTemplate;
...@@ -95,8 +96,12 @@ public class SpringEncoder implements Encoder { ...@@ -95,8 +96,12 @@ public class SpringEncoder implements Encoder {
// with the modified headers // with the modified headers
request.headers(getHeaders(outputMessage.getHeaders())); request.headers(getHeaders(outputMessage.getHeaders()));
request.body(outputMessage.getOutputStream().toByteArray(), // do not use charset for binary data
Charset.forName("UTF-8")); // TODO: set charset if (messageConverter instanceof ByteArrayHttpMessageConverter) {
request.body(outputMessage.getOutputStream().toByteArray(), null);
} else {
request.body(outputMessage.getOutputStream().toByteArray(), Charset.forName("UTF-8"));
}
return; return;
} }
} }
......
...@@ -2,10 +2,12 @@ package org.springframework.cloud.netflix.feign.support; ...@@ -2,10 +2,12 @@ package org.springframework.cloud.netflix.feign.support;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Collection; import java.util.Collection;
import org.junit.Test; import org.junit.Test;
...@@ -63,8 +65,22 @@ public class SpringEncoderTests { ...@@ -63,8 +65,22 @@ public class SpringEncoderTests {
String header = contentTypeHeader.iterator().next(); String header = contentTypeHeader.iterator().next();
assertThat("content type header is wrong", header, is("application/mytype")); 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> { class MediaTypeMatcher extends ArgumentMatcher<MediaType> {
private MediaType mediaType; private MediaType mediaType;
...@@ -129,7 +145,10 @@ public class SpringEncoderTests { ...@@ -129,7 +145,10 @@ public class SpringEncoderTests {
@Override @Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) { public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return true; if (clazz == String.class) {
return true;
}
return false;
} }
@Override @Override
......
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