Skip to content

Commit

Permalink
Fix HTTP responses that caller asked to be JSON
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsyer authored and markfisher committed Jan 20, 2017
1 parent 8e3370b commit 946f4a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Flux<Object> read(Class<? extends Flux<Object>> clazz,

MediaType mediaType = inputMessage.getHeaders().getContentType();
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());
}
if (mediaType.includes(EVENT_STREAM)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void onNext(T value) {
else {
responseBodyEmitter.send(",");
}
if (value.getClass()==String.class) {
if (value.getClass()==String.class && !((String)value).contains("\"")) {
object = "\"" + value + "\"";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -96,13 +97,24 @@ public void sentencesAcceptAny() throws Exception {
.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
public void sentencesAcceptSse() throws Exception {
assertThat(rest.exchange(
RequestEntity.get(new URI("http://localhost:" + port + "/sentences"))
.accept(EVENT_STREAM).build(),
String.class).getBody())
.isEqualTo(sse("[\"go\",\"home\"]","[\"come\",\"back\"]"));
.isEqualTo(sse("[\"go\",\"home\"]", "[\"come\",\"back\"]"));
}

@Test
Expand Down

0 comments on commit 946f4a3

Please sign in to comment.