Fix some logic with media types

If user sends "Accept: */*" we don't want to default to sending
an SSE (for instance). So the logic for detecting those preferences
has to take MediaType.ALL into account as a special case.
This commit is contained in:
Dave Syer
2017-01-20 13:10:54 +00:00
parent f709a4e08a
commit 452106f287
3 changed files with 40 additions and 5 deletions

View File

@@ -16,7 +16,9 @@
package org.springframework.cloud.function.web;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -77,6 +79,32 @@ public class RestApplicationTests {
String.class).getBody()).isEqualTo("foobar");
}
@Test
public void sentences() throws Exception {
assertThat(rest.exchange(RequestEntity
.get(new URI("http://localhost:" + port + "/sentences")).build(),
String.class).getBody())
.isEqualTo("[\"go\",\"home\"][\"come\",\"back\"]");
}
@Test
public void sentencesAcceptAny() throws Exception {
assertThat(rest.exchange(
RequestEntity.get(new URI("http://localhost:" + port + "/sentences"))
.accept(MediaType.ALL).build(),
String.class).getBody())
.isEqualTo("[\"go\",\"home\"][\"come\",\"back\"]");
}
@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\"]"));
}
@Test
public void uppercase() {
assertThat(rest.postForObject("http://localhost:" + port + "/uppercase",
@@ -123,7 +151,8 @@ public class RestApplicationTests {
@Bean
public Function<Flux<String>, Flux<String>> uppercase() {
return flux -> flux.log().map(value -> "[" + value.trim().toUpperCase() + "]");
return flux -> flux.log()
.map(value -> "[" + value.trim().toUpperCase() + "]");
}
@Bean
@@ -139,6 +168,12 @@ public class RestApplicationTests {
return () -> Flux.fromArray(new String[] { "foo", "bar" });
}
@Bean
public Supplier<Flux<List<String>>> sentences() {
return () -> Flux.just(Arrays.asList("go", "home"),
Arrays.asList("come", "back"));
}
}
}