Add MVC body processors to handle Flux
We don't need to cover all the possible uses of Flux (only Flux<String> really), so this isn't comprehensive coverage of all the features in Spring WebFlux, but it's good enough for functions to run with Spring Boot 1.5.
This commit is contained in:
@@ -47,17 +47,17 @@ import reactor.core.publisher.Flux;
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class RestApplicationTests {
|
||||
|
||||
private static final MediaType EVENT_STREAM = MediaType.valueOf("text/event-stream");
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
private TestRestTemplate rest = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void wordsSSE() throws Exception {
|
||||
assertThat(
|
||||
rest.exchange(
|
||||
RequestEntity.get(new URI("http://localhost:" + port + "/words"))
|
||||
.accept(MediaType.TEXT_EVENT_STREAM).build(),
|
||||
String.class).getBody()).isEqualTo(sse("foo", "bar"));
|
||||
assertThat(rest.exchange(
|
||||
RequestEntity.get(new URI("http://localhost:" + port + "/words"))
|
||||
.accept(EVENT_STREAM).build(),
|
||||
String.class).getBody()).isEqualTo(sse("foo", "bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -100,17 +100,17 @@ public class RestApplicationTests {
|
||||
.exchange(
|
||||
RequestEntity.post(new URI("http://localhost:" + port + "/maps"))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body("{\"value\":\"foo\"}{\"value\":\"bar\"}"),
|
||||
// TODO: make this work without newline separator
|
||||
.body("{\"value\":\"foo\"}\n{\"value\":\"bar\"}"),
|
||||
String.class)
|
||||
.getBody()).isEqualTo("{\"value\":\"FOO\"}{\"value\":\"BAR\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uppercaseSSE() throws Exception {
|
||||
assertThat(rest.exchange(
|
||||
RequestEntity.post(new URI("http://localhost:" + port + "/uppercase"))
|
||||
.accept(MediaType.TEXT_EVENT_STREAM)
|
||||
.contentType(MediaType.TEXT_EVENT_STREAM).body(sse("foo", "bar")),
|
||||
assertThat(rest.exchange(RequestEntity
|
||||
.post(new URI("http://localhost:" + port + "/uppercase"))
|
||||
.accept(EVENT_STREAM).contentType(EVENT_STREAM).body(sse("foo", "bar")),
|
||||
String.class).getBody()).isEqualTo(sse("[FOO]", "[BAR]"));
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ public class RestApplicationTests {
|
||||
|
||||
@Bean
|
||||
public Function<Flux<String>, Flux<String>> uppercase() {
|
||||
return flux -> flux.map(value -> "[" + value.trim().toUpperCase() + "]");
|
||||
return flux -> flux.log().map(value -> "[" + value.trim().toUpperCase() + "]");
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.function.web.flux;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.MockHttpInputMessage;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class FluxHttpMessageConverterTests {
|
||||
|
||||
private FluxHttpMessageConverter converter = new FluxHttpMessageConverter();
|
||||
|
||||
private Class<Flux<Object>> type = null;
|
||||
|
||||
@Test
|
||||
public void newlines() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage("foo\nbar".getBytes());
|
||||
assertThat(converter.read(type, message).collectList().block()).contains("foo",
|
||||
"bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sse() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(
|
||||
"data:foo\n\ndata:bar".getBytes());
|
||||
message.getHeaders().setContentType(MediaType.valueOf("text/event-stream"));
|
||||
assertThat(converter.read(type, message).collectList().block()).contains("foo",
|
||||
"bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStream() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(
|
||||
"{\"value\":\"foo\"}{\"value\":\"barrier\"}".getBytes());
|
||||
message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
assertThat(converter.read(type, message).collectList().block())
|
||||
.contains("{\"value\":\"foo\"}", "{\"value\":\"barrier\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStreamWhitespace() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(
|
||||
"{\"value\":\"foo\"} {\"value\":\"barrier\"} ".getBytes());
|
||||
message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
assertThat(converter.read(type, message).collectList().block())
|
||||
.contains("{\"value\":\"foo\"}", "{\"value\":\"barrier\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStreamNewline() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(
|
||||
"{\"value\":\"foo\"}\n{\"value\":\"barrier\"}".getBytes());
|
||||
message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
assertThat(converter.read(type, message).collectList().block())
|
||||
.contains("{\"value\":\"foo\"}", "{\"value\":\"barrier\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonArray() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(
|
||||
"[{\"value\":\"foo\"},{\"value\":\"barrier\"}]".getBytes());
|
||||
message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
assertThat(converter.read(type, message).collectList().block())
|
||||
.contains("{\"value\":\"foo\"}", "{\"value\":\"barrier\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonArrayWhitespace() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(
|
||||
"[{\"value\":\"foo\"}, {\"value\":\"barrier\"}] ".getBytes());
|
||||
message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
assertThat(converter.read(type, message).collectList().block())
|
||||
.contains("{\"value\":\"foo\"}", "{\"value\":\"barrier\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonArrayNewline() throws Exception {
|
||||
MockHttpInputMessage message = new MockHttpInputMessage(
|
||||
"[{\"value\":\"foo\"},\n{\"value\":\"barrier\"}]".getBytes());
|
||||
message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
assertThat(converter.read(type, message).collectList().block())
|
||||
.contains("{\"value\":\"foo\"}", "{\"value\":\"barrier\"}");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user