Unverified Commit 1511167f by rockinrimmer Committed by Spencer Gibb

Check if object is already a ResponseEntity, if so return instead of … (#1343)

* Check if object is already a ResponseEntity, if so return instead of wrapping in another ResponseEnitiy. Which caused headers and status code from original ResponseEnitiy to be lost and for body to be output with whole ResponseEntity instead of just the body * added test
parent a1bcaba5
......@@ -100,6 +100,10 @@ public class SingleReturnValueHandler implements AsyncHandlerMethodReturnValueHa
.map(new Func1<Object, ResponseEntity<?>>() {
@Override
public ResponseEntity<?> call(Object object) {
if (object instanceof ResponseEntity){
return (ResponseEntity) object;
}
return new ResponseEntity<Object>(object,
getHttpHeaders(responseEntity),
getHttpStatus(responseEntity));
......
......@@ -67,10 +67,16 @@ public class SingleReturnValueHandlerTest {
return Single.just("single value");
}
@RequestMapping(method = RequestMethod.GET, value = "/singleWithResponse")
public ResponseEntity<Single<String>> singleWithResponse() {
return new ResponseEntity<>(Single.just("single value"), HttpStatus.NOT_FOUND);
}
@RequestMapping(method = RequestMethod.GET, value = "/singleWithResponse")
public ResponseEntity<Single<String>> singleWithResponse() {
return new ResponseEntity<>(Single.just("single value"),
HttpStatus.NOT_FOUND);
}
@RequestMapping(method = RequestMethod.GET, value = "/singleCreatedWithResponse")
public Single<ResponseEntity<String>> singleOuterWithResponse() {
return Single.just(new ResponseEntity<>("single value", HttpStatus.CREATED));
}
@RequestMapping(method = RequestMethod.GET, value = "/throw")
public Single<Object> error() {
......@@ -109,12 +115,25 @@ public class SingleReturnValueHandlerTest {
// when
ResponseEntity<Object> response = restTemplate.getForEntity(path("/throw"), Object.class);
// then
assertNotNull(response);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
// then
assertNotNull(response);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
private String path(String context) {
return String.format("http://localhost:%d%s", port, context);
}
}
\ No newline at end of file
@Test
public void shouldRetrieveSingleValueWithCreatedCode() {
// when
ResponseEntity<String> response = restTemplate.getForEntity(path("/singleCreatedWithResponse"),
String.class);
// then
assertNotNull(response);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals("single value", response.getBody());
}
private String path(String context) {
return String.format("http://localhost:%d%s", port, context);
}
}
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