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
This commit is contained in:
committed by
Spencer Gibb
parent
e73ee5f690
commit
ed137a93ce
@@ -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,8 +65,22 @@ 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> {
|
||||
|
||||
private MediaType mediaType;
|
||||
@@ -129,7 +145,10 @@ public class SpringEncoderTests {
|
||||
|
||||
@Override
|
||||
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
|
||||
return true;
|
||||
if (clazz == String.class) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user