Commit 9e0d8cae by Spencer Gibb

Support HttpEntity not just ResponseEntity

fixes gh-859
parent b6fef047
......@@ -5,6 +5,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.LinkedList;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
......@@ -33,13 +34,13 @@ public class ResponseEntityDecoder implements Decoder {
public Object decode(final Response response, Type type) throws IOException,
FeignException {
if (isParameterizeResponseEntity(type)) {
if (isParameterizeHttpEntity(type)) {
type = ((ParameterizedType) type).getActualTypeArguments()[0];
Object decodedObject = decoder.decode(response, type);
return createResponse(decodedObject, response);
}
else if (isResponseEntity(type)) {
else if (isHttpEntity(type)) {
return createResponse(null, response);
}
else {
......@@ -47,14 +48,19 @@ public class ResponseEntityDecoder implements Decoder {
}
}
private boolean isParameterizeResponseEntity(Type type) {
return type instanceof ParameterizedType
&& ((ParameterizedType) type).getRawType().equals(ResponseEntity.class);
private boolean isParameterizeHttpEntity(Type type) {
if (type instanceof ParameterizedType) {
return isHttpEntity(((ParameterizedType) type).getRawType());
}
return false;
}
private boolean isResponseEntity(Type type) {
return type instanceof Class && type.equals(ResponseEntity.class);
private boolean isHttpEntity(Type type) {
if (type instanceof Class) {
Class c = (Class) type;
return HttpEntity.class.isAssignableFrom(c);
}
return false;
}
@SuppressWarnings("unchecked")
......
......@@ -54,6 +54,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
......@@ -87,6 +88,11 @@ import rx.Observable;
@DirtiesContext
public class FeignClientTests {
public static final String HELLO_WORLD_1 = "hello world 1";
public static final String OI_TERRA_2 = "oi terra 2";
public static final String MYHEADER1 = "myheader1";
public static final String MYHEADER2 = "myheader2";
@Value("${local.server.port}")
private int port = 0;
......@@ -130,6 +136,9 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.HEAD, value = "/head")
ResponseEntity head();
@RequestMapping(method = RequestMethod.GET, value = "/hello")
HttpEntity<Hello> getHelloEntity();
}
public static class TestClientConfig {
......@@ -139,7 +148,7 @@ public class FeignClientTests {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
template.header("myheader1", "myheader1value");
template.header(MYHEADER1, "myheader1value");
}
};
}
......@@ -149,7 +158,7 @@ public class FeignClientTests {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
template.header("myheader2", "myheader2value");
template.header(MYHEADER2, "myheader2value");
}
};
}
......@@ -220,7 +229,7 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
public Hello getHello() {
return new Hello("hello world 1");
return new Hello(HELLO_WORLD_1);
}
@RequestMapping(method = RequestMethod.GET, value = "/hellos")
......@@ -232,14 +241,14 @@ public class FeignClientTests {
@RequestMapping(method = RequestMethod.GET, value = "/hellostrings")
public List<String> getHelloStrings() {
ArrayList<String> hellos = new ArrayList<>();
hellos.add("hello world 1");
hellos.add("oi terra 2");
hellos.add(HELLO_WORLD_1);
hellos.add(OI_TERRA_2);
return hellos;
}
@RequestMapping(method = RequestMethod.GET, value = "/helloheaders")
public List<String> getHelloHeaders(@RequestHeader("myheader1") String myheader1,
@RequestHeader("myheader2") String myheader2) {
public List<String> getHelloHeaders(@RequestHeader(MYHEADER1) String myheader1,
@RequestHeader(MYHEADER2) String myheader2) {
ArrayList<String> headers = new ArrayList<>();
headers.add(myheader1);
headers.add(myheader2);
......@@ -281,8 +290,8 @@ public class FeignClientTests {
private static ArrayList<Hello> getHelloList() {
ArrayList<Hello> hellos = new ArrayList<>();
hellos.add(new Hello("hello world 1"));
hellos.add(new Hello("oi terra 2"));
hellos.add(new Hello(HELLO_WORLD_1));
hellos.add(new Hello(OI_TERRA_2));
return hellos;
}
......@@ -299,7 +308,7 @@ public class FeignClientTests {
public void testSimpleType() {
Hello hello = this.testClient.getHello();
assertNotNull("hello was null", hello);
assertEquals("first hello didn't match", new Hello("hello world 1"), hello);
assertEquals("first hello didn't match", new Hello(HELLO_WORLD_1), hello);
}
@Test
......@@ -332,7 +341,7 @@ public class FeignClientTests {
assertNotNull("testClientServiceId was null", this.testClientServiceId);
final Hello hello = this.testClientServiceId.getHello();
assertNotNull("The hello response was null", hello);
assertEquals("first hello didn't match", new Hello("hello world 1"), hello);
assertEquals("first hello didn't match", new Hello(HELLO_WORLD_1), hello);
}
@Test
......@@ -368,6 +377,15 @@ public class FeignClientTests {
}
@Test
public void testHttpEntity() {
HttpEntity<Hello> entity = testClient.getHelloEntity();
assertNotNull("entity was null", entity);
Hello hello = entity.getBody();
assertNotNull("hello was null", hello);
assertEquals("first hello didn't match", new Hello(HELLO_WORLD_1), hello);
}
@Test
public void testDecodeNotFound() {
ResponseEntity response = decodingTestClient.notFound();
assertNotNull("response was null", response);
......
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