Fix HTTP responses that caller asked to be JSON
If caller says he accepts application/json we need to be sure that we send an array. That wasn't quite working and hadn't been tested.
This commit is contained in:
@@ -65,7 +65,7 @@ public class FluxHttpMessageConverter implements HttpMessageConverter<Flux<Objec
|
|||||||
|
|
||||||
MediaType mediaType = inputMessage.getHeaders().getContentType();
|
MediaType mediaType = inputMessage.getHeaders().getContentType();
|
||||||
if (mediaType != null) {
|
if (mediaType != null) {
|
||||||
if (mediaType.includes(MediaType.APPLICATION_JSON)) {
|
if (!MediaType.ALL.equals(mediaType) && mediaType.includes(MediaType.APPLICATION_JSON)) {
|
||||||
return new JsonObjectDecoder().decode(inputMessage.getBody());
|
return new JsonObjectDecoder().decode(inputMessage.getBody());
|
||||||
}
|
}
|
||||||
if (mediaType.includes(EVENT_STREAM)) {
|
if (mediaType.includes(EVENT_STREAM)) {
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class ResponseBodyEmitterSubscriber<T> implements Subscriber<T>, Runnable {
|
|||||||
else {
|
else {
|
||||||
responseBodyEmitter.send(",");
|
responseBodyEmitter.send(",");
|
||||||
}
|
}
|
||||||
if (value.getClass()==String.class) {
|
if (value.getClass()==String.class && !((String)value).contains("\"")) {
|
||||||
object = "\"" + value + "\"";
|
object = "\"" + value + "\"";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import org.springframework.boot.test.web.client.TestRestTemplate;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.RequestEntity;
|
import org.springframework.http.RequestEntity;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@@ -96,13 +97,24 @@ public class RestApplicationTests {
|
|||||||
.isEqualTo("[\"go\",\"home\"][\"come\",\"back\"]");
|
.isEqualTo("[\"go\",\"home\"][\"come\",\"back\"]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sentencesAcceptJson() throws Exception {
|
||||||
|
ResponseEntity<String> result = rest.exchange(
|
||||||
|
RequestEntity.get(new URI("http://localhost:" + port + "/sentences"))
|
||||||
|
.accept(MediaType.APPLICATION_JSON).build(),
|
||||||
|
String.class);
|
||||||
|
assertThat(result.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
|
||||||
|
// TODO: test this
|
||||||
|
// assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sentencesAcceptSse() throws Exception {
|
public void sentencesAcceptSse() throws Exception {
|
||||||
assertThat(rest.exchange(
|
assertThat(rest.exchange(
|
||||||
RequestEntity.get(new URI("http://localhost:" + port + "/sentences"))
|
RequestEntity.get(new URI("http://localhost:" + port + "/sentences"))
|
||||||
.accept(EVENT_STREAM).build(),
|
.accept(EVENT_STREAM).build(),
|
||||||
String.class).getBody())
|
String.class).getBody())
|
||||||
.isEqualTo(sse("[\"go\",\"home\"]","[\"come\",\"back\"]"));
|
.isEqualTo(sse("[\"go\",\"home\"]", "[\"come\",\"back\"]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user