Add template rendering support
This commit introduces template rendering support in the web.reactive package, through a Response.render method and a Rendering interface.
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.web.reactive.function;
|
||||
import java.net.URI;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -32,7 +33,7 @@ import static org.junit.Assert.assertEquals;
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ResponseTests {
|
||||
public class DefaultResponseBuilderTests {
|
||||
|
||||
@Test
|
||||
public void from() throws Exception {
|
||||
@@ -121,6 +122,7 @@ public class ResponseTests {
|
||||
Response<Void> result = Response.ok().eTag("foo").build();
|
||||
assertEquals("\"foo\"", result.headers().getETag());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lastModified() throws Exception {
|
||||
ZonedDateTime now = ZonedDateTime.now();
|
||||
@@ -141,7 +143,13 @@ public class ResponseTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeTo() throws Exception {
|
||||
public void renderObjectArray() throws Exception {
|
||||
Response<Rendering> result =
|
||||
Response.ok().render("name", this, Collections.emptyList(), "foo");
|
||||
Map<String, Object> model = result.body().model();
|
||||
assertEquals(2, model.size());
|
||||
assertEquals(this, model.get("defaultResponseBuilderTests"));
|
||||
assertEquals("foo", model.get("string"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.reactive.function;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
@@ -45,6 +46,7 @@ import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.config.WebReactiveConfiguration;
|
||||
import org.springframework.web.reactive.function.support.HandlerFunctionAdapter;
|
||||
import org.springframework.web.reactive.function.support.ResponseResultHandler;
|
||||
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -128,6 +130,11 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
|
||||
public Supplier<Stream<HttpMessageWriter<?>>> messageWriters() {
|
||||
return () -> getMessageWriters().stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<Stream<ViewResolver>> viewResolvers() {
|
||||
return () -> Collections.<ViewResolver>emptySet().stream();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.web.reactive.function;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.web.reactive.result.view.View;
|
||||
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.server.session.MockWebSessionManager;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class RenderingResponseTests {
|
||||
|
||||
private final Map<String, Object> model = Collections.singletonMap("foo", "bar");
|
||||
|
||||
private final RenderingResponse renderingResponse = new RenderingResponse(200, new HttpHeaders(), "view",
|
||||
model);
|
||||
|
||||
@Test
|
||||
public void body() throws Exception {
|
||||
assertEquals("view", renderingResponse.body().name());
|
||||
assertEquals(model, renderingResponse.body().model());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeTo() throws Exception {
|
||||
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, URI.create("http://localhost"));
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
ServerWebExchange exchange = new DefaultServerWebExchange(request, response, new MockWebSessionManager());
|
||||
ViewResolver viewResolver = mock(ViewResolver.class);
|
||||
View view = mock(View.class);
|
||||
when(viewResolver.resolveViewName("view", Locale.ENGLISH)).thenReturn(Mono.just(view));
|
||||
when(view.render(model, null, exchange)).thenReturn(Mono.empty());
|
||||
exchange.getAttributes().put(Router.VIEW_RESOLVERS_ATTRIBUTE,
|
||||
(Supplier<Stream<ViewResolver>>) () -> Collections
|
||||
.singleton(viewResolver).stream());
|
||||
|
||||
|
||||
renderingResponse.writeTo(exchange).block();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -37,15 +37,11 @@ import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -137,6 +133,8 @@ public class RouterTests {
|
||||
() -> Collections.<HttpMessageReader<?>>emptyList().stream());
|
||||
when(configuration.messageWriters()).thenReturn(
|
||||
() -> Collections.<HttpMessageWriter<?>>emptyList().stream());
|
||||
when(configuration.viewResolvers()).thenReturn(
|
||||
() -> Collections.<ViewResolver>emptyList().stream());
|
||||
|
||||
HttpHandler result = Router.toHttpHandler(routingFunction, configuration);
|
||||
assertNotNull(result);
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.web.reactive.result.view;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ModelAndViewTests {
|
||||
|
||||
@Test
|
||||
public void view() {
|
||||
View view = mock(View.class);
|
||||
ModelAndView<View> mav = ModelAndView.view(view).build();
|
||||
assertFalse(mav.isReference());
|
||||
assertTrue(mav.view().isPresent());
|
||||
assertFalse(mav.viewName().isPresent());
|
||||
assertEquals(view, mav.view().get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void viewName() {
|
||||
String viewName = "foo";
|
||||
ModelAndView<String> mav = ModelAndView.viewName(viewName).build();
|
||||
assertTrue(mav.isReference());
|
||||
assertTrue(mav.viewName().isPresent());
|
||||
assertFalse(mav.view().isPresent());
|
||||
assertEquals(viewName, mav.viewName().get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modelAttribute() {
|
||||
ModelAndView<String> mav = ModelAndView.viewName("foo")
|
||||
.modelAttribute("foo", "bar").build();
|
||||
assertEquals(1, mav.model().size());
|
||||
assertTrue(mav.model().containsKey("foo"));
|
||||
assertEquals("bar", mav.model().get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modelAttributeNoName() {
|
||||
ModelAndView<String> mav = ModelAndView.viewName("foo")
|
||||
.modelAttribute(this).build();
|
||||
assertEquals(1, mav.model().size());
|
||||
assertTrue(mav.model().containsKey("modelAndViewTests"));
|
||||
assertEquals(this, mav.model().get("modelAndViewTests"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void modelAttributes() {
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("foo", "bar");
|
||||
model.put("baz", "qux");
|
||||
ModelAndView<String> mav = ModelAndView.viewName("foo")
|
||||
.modelAttributes(model).build();
|
||||
assertEquals(2, mav.model().size());
|
||||
assertTrue(mav.model().containsKey("foo"));
|
||||
assertEquals("bar", mav.model().get("foo"));
|
||||
assertTrue(mav.model().containsKey("baz"));
|
||||
assertEquals("qux", mav.model().get("baz"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user