Polishing

This commit is contained in:
Arjen Poutsma
2016-09-16 11:13:53 +02:00
parent 8072497ce2
commit 228a01b298
6 changed files with 52 additions and 143 deletions

View File

@@ -213,7 +213,6 @@ public class DefaultResponseBuilderTests {
assertNull(response.getBody());
}
@SuppressWarnings("rawtypes")
@Test
public void bodyPopulator() throws Exception {
String body = "foo";
@@ -246,81 +245,6 @@ public class DefaultResponseBuilderTests {
assertNotNull(response.getBody());
}
/*
@Test
public void bodyNotAcceptable() throws Exception {
String body = "foo";
Response<String> result = Response.ok().contentType(MediaType.APPLICATION_JSON).body(body);
assertEquals(body, result.body());
MockServerHttpRequest request =
new MockServerHttpRequest(HttpMethod.GET, "http://localhost");
MockServerHttpResponse response = new MockServerHttpResponse();
ServerWebExchange exchange =
new DefaultServerWebExchange(request, response, new MockWebSessionManager());
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<CharSequence>(new CharSequenceEncoder()));
Configuration configuration = mock(Configuration.class);
when(configuration.messageWriters()).thenReturn(messageWriters::stream);
result.writeTo(exchange, configuration).block();
assertEquals(HttpStatus.NOT_ACCEPTABLE, response.getStatusCode());
}
@Test
public void stream() throws Exception {
Publisher<String> publisher = Flux.just("foo", "bar");
Response<Publisher<String>> result = Response.ok().stream(publisher, String.class);
MockServerHttpRequest request =
new MockServerHttpRequest(HttpMethod.GET, "http://localhost");
MockServerHttpResponse response = new MockServerHttpResponse();
ServerWebExchange exchange =
new DefaultServerWebExchange(request, response, new MockWebSessionManager());
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<CharSequence>(new CharSequenceEncoder()));
Configuration mockConfig = mock(Configuration.class);
when(mockConfig.messageWriters()).thenReturn(messageWriters::stream);
exchange.getAttributes().put(RoutingFunctions.CONFIGURATION_ATTRIBUTE, mockConfig);
result.writeTo(exchange).block();
assertNotNull(response.getBody());
}
@Test
public void resource() throws Exception {
Resource resource = new ClassPathResource("response.txt", DefaultResponseBuilderTests.class);
Response<Resource> result = Response.ok().resource(resource);
ServerWebExchange exchange = mock(ServerWebExchange.class);
MockServerHttpResponse response = new MockServerHttpResponse();
when(exchange.getResponse()).thenReturn(response);
result.writeTo(exchange).block();
assertNotNull(response.getBody());
}
@Test
public void sse() throws Exception {
ServerSentEvent<String> sse = ServerSentEvent.<String>builder().data("42").build();
Mono<ServerSentEvent<String>> body = Mono.just(sse);
Response<Mono<ServerSentEvent<String>>> result = Response.ok().sse(body);
ServerWebExchange exchange = mock(ServerWebExchange.class);
MockServerHttpResponse response = new MockServerHttpResponse();
when(exchange.getResponse()).thenReturn(response);
result.writeTo(exchange).block();
assertNotNull(response.getBodyWithFlush());
}
*/
@Test
public void render() throws Exception {
Map<String, Object> model = Collections.singletonMap("foo", "bar");

View File

@@ -34,6 +34,7 @@ import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import static org.springframework.web.reactive.function.BodyPopulators.fromPublisher;
import static org.springframework.web.reactive.function.RequestPredicates.GET;
import static org.springframework.web.reactive.function.RequestPredicates.POST;
import static org.springframework.web.reactive.function.RoutingFunctions.route;
@@ -98,19 +99,19 @@ public class PublisherHandlerFunctionIntegrationTests
public Response<Publisher<Person>> mono(Request request) {
Person person = new Person("John");
return Response.ok().body(BodyPopulators.fromPublisher(Mono.just(person), Person.class));
return Response.ok().body(fromPublisher(Mono.just(person), Person.class));
}
public Response<Publisher<Person>> postMono(Request request) {
Mono<Person> personMono = request.body(toMono(Person.class));
return Response.ok().body(BodyPopulators.fromPublisher(personMono, Person.class));
return Response.ok().body(fromPublisher(personMono, Person.class));
}
public Response<Publisher<Person>> flux(Request request) {
Person person1 = new Person("John");
Person person2 = new Person("Jane");
return Response.ok().body(
BodyPopulators.fromPublisher(Flux.just(person1, person2), Person.class));
fromPublisher(Flux.just(person1, person2), Person.class));
}
}