Add support for Message handling in web functions
If the output is a Message we grab the headers from the first one and send those as response headers. A function can add headers to messages. The HTTP response will contain only headers that are in x-* _or_ were added to the message by user (i.e. they weren't in the request). We certainly do not want to pass through all HTTP headers (request headers can conflict with or invalidate response headers).
This commit is contained in:
@@ -45,6 +45,8 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -256,6 +258,26 @@ public class RestApplicationTests {
|
||||
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void messages() throws Exception {
|
||||
ResponseEntity<String> result = rest.exchange(RequestEntity
|
||||
.post(new URI("/messages")).contentType(MediaType.APPLICATION_JSON)
|
||||
.header("x-foo", "bar").body("[\"foo\",\"bar\"]"), String.class);
|
||||
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
|
||||
assertThat(result.getHeaders().getFirst("x-foo")).isEqualTo("bar");
|
||||
assertThat(result.getHeaders()).doesNotContainKey("id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void headers() throws Exception {
|
||||
ResponseEntity<String> result = rest.exchange(RequestEntity
|
||||
.post(new URI("/headers")).contentType(MediaType.APPLICATION_JSON)
|
||||
.body("[\"foo\",\"bar\"]"), String.class);
|
||||
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
|
||||
assertThat(result.getHeaders().getFirst("foo")).isEqualTo("bar");
|
||||
assertThat(result.getHeaders()).doesNotContainKey("id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uppercaseSingleValue() throws Exception {
|
||||
ResponseEntity<String> result = rest
|
||||
@@ -406,6 +428,20 @@ public class RestApplicationTests {
|
||||
return value -> "(" + value.trim().toUpperCase() + ")";
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Message<String>, Message<String>> messages() {
|
||||
return value -> MessageBuilder
|
||||
.withPayload("(" + value.getPayload().trim().toUpperCase() + ")")
|
||||
.copyHeaders(value.getHeaders()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Flux<Message<String>>, Flux<Message<String>>> headers() {
|
||||
return flux -> flux.map(value -> MessageBuilder
|
||||
.withPayload("(" + value.getPayload().trim().toUpperCase() + ")")
|
||||
.setHeader("foo", "bar").build());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Flux<Foo>, Flux<Foo>> upFoos() {
|
||||
return flux -> flux.log()
|
||||
|
||||
Reference in New Issue
Block a user