Update ScriptTemplateView to manage content type
This commit introduces the following changes: - Content type can now be properly configured - Default content type is "text/html" - Content type and charset are now properly set in the response Issue: SPR-13379
This commit is contained in:
@@ -799,6 +799,7 @@ public class MvcNamespaceTests {
|
||||
ScriptTemplateConfigurer scriptTemplateConfigurer = appContext.getBean(ScriptTemplateConfigurer.class);
|
||||
assertNotNull(scriptTemplateConfigurer);
|
||||
assertEquals("render", scriptTemplateConfigurer.getRenderFunction());
|
||||
assertEquals(MediaType.TEXT_PLAIN_VALUE, scriptTemplateConfigurer.getContentType());
|
||||
assertEquals(StandardCharsets.ISO_8859_1, scriptTemplateConfigurer.getCharset());
|
||||
assertEquals("classpath:", scriptTemplateConfigurer.getResourceLoaderPath());
|
||||
assertFalse(scriptTemplateConfigurer.isSharedEngine());
|
||||
|
||||
@@ -20,7 +20,9 @@ import java.net.URLClassLoader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
@@ -34,6 +36,13 @@ import org.junit.Test;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -51,7 +60,7 @@ public class ScriptTemplateViewTests {
|
||||
|
||||
private ScriptTemplateConfigurer configurer;
|
||||
|
||||
private StaticApplicationContext applicationContext;
|
||||
private StaticWebApplicationContext wac;
|
||||
|
||||
private static final String RESOURCE_LOADER_PATH = "classpath:org/springframework/web/servlet/view/script/";
|
||||
|
||||
@@ -59,10 +68,10 @@ public class ScriptTemplateViewTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.configurer = new ScriptTemplateConfigurer();
|
||||
this.applicationContext = new StaticApplicationContext();
|
||||
this.applicationContext.getBeanFactory().registerSingleton("scriptTemplateConfigurer", this.configurer);
|
||||
this.wac = new StaticWebApplicationContext();
|
||||
this.wac.getBeanFactory().registerSingleton("scriptTemplateConfigurer", this.configurer);
|
||||
this.view = new ScriptTemplateView();
|
||||
this.view.setUrl("sampleView");
|
||||
this.view.setUrl(RESOURCE_LOADER_PATH + "empty.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,13 +92,18 @@ public class ScriptTemplateViewTests {
|
||||
this.configurer.setEngine(engine);
|
||||
this.configurer.setRenderObject("Template");
|
||||
this.configurer.setRenderFunction("render");
|
||||
this.configurer.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
this.configurer.setCharset(StandardCharsets.ISO_8859_1);
|
||||
this.configurer.setSharedEngine(true);
|
||||
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(this.view);
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
assertEquals(engine, accessor.getPropertyValue("engine"));
|
||||
assertEquals("Template", accessor.getPropertyValue("renderObject"));
|
||||
assertEquals("render", accessor.getPropertyValue("renderFunction"));
|
||||
assertEquals(MediaType.TEXT_PLAIN_VALUE, accessor.getPropertyValue("contentType"));
|
||||
assertEquals(StandardCharsets.ISO_8859_1, accessor.getPropertyValue("charset"));
|
||||
assertEquals(true, accessor.getPropertyValue("sharedEngine"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,16 +111,15 @@ public class ScriptTemplateViewTests {
|
||||
this.configurer.setEngineName("nashorn");
|
||||
this.configurer.setRenderObject("Template");
|
||||
this.configurer.setRenderFunction("render");
|
||||
this.configurer.setCharset(StandardCharsets.ISO_8859_1);
|
||||
this.configurer.setSharedEngine(true);
|
||||
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(this.view);
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
assertEquals("nashorn", accessor.getPropertyValue("engineName"));
|
||||
assertNotNull(accessor.getPropertyValue("engine"));
|
||||
assertEquals("Template", accessor.getPropertyValue("renderObject"));
|
||||
assertEquals("render", accessor.getPropertyValue("renderFunction"));
|
||||
assertEquals(StandardCharsets.ISO_8859_1, accessor.getPropertyValue("charset"));
|
||||
assertEquals(true, accessor.getPropertyValue("sharedEngine"));
|
||||
assertEquals(MediaType.TEXT_HTML_VALUE, accessor.getPropertyValue("contentType"));
|
||||
assertEquals(StandardCharsets.UTF_8, accessor.getPropertyValue("charset"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -115,7 +128,7 @@ public class ScriptTemplateViewTests {
|
||||
given(engine.get("key")).willReturn("value");
|
||||
this.view.setEngine(engine);
|
||||
this.view.setRenderFunction("render");
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
engine = this.view.getEngine();
|
||||
assertNotNull(engine);
|
||||
assertEquals("value", engine.get("key"));
|
||||
@@ -131,7 +144,7 @@ public class ScriptTemplateViewTests {
|
||||
this.view.setEngineName("nashorn");
|
||||
this.view.setRenderFunction("render");
|
||||
this.view.setSharedEngine(false);
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
ExecutorService executor = Executors.newFixedThreadPool(4);
|
||||
List<Future<Boolean>> results = new ArrayList<>();
|
||||
for(int i = 0; i < iterations; i++) {
|
||||
@@ -160,7 +173,7 @@ public class ScriptTemplateViewTests {
|
||||
public void noRenderFunctionDefined() {
|
||||
this.view.setEngine(mock(InvocableScriptEngine.class));
|
||||
try {
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
@@ -174,7 +187,7 @@ public class ScriptTemplateViewTests {
|
||||
this.view.setEngineName("test");
|
||||
this.view.setRenderFunction("render");
|
||||
try {
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
@@ -188,7 +201,7 @@ public class ScriptTemplateViewTests {
|
||||
this.view.setRenderFunction("render");
|
||||
this.view.setSharedEngine(false);
|
||||
try {
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
@@ -203,7 +216,7 @@ public class ScriptTemplateViewTests {
|
||||
this.view.setEngine(mock(InvocableScriptEngine.class));
|
||||
this.view.setRenderFunction("render");
|
||||
this.view.setResourceLoaderPath(RESOURCE_LOADER_PATH);
|
||||
this.view.setApplicationContext(this.applicationContext);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
ClassLoader classLoader = this.view.createClassLoader();
|
||||
assertNotNull(classLoader);
|
||||
URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
|
||||
@@ -219,6 +232,38 @@ public class ScriptTemplateViewTests {
|
||||
assertThat(Arrays.asList(urlClassLoader.getURLs()).get(1).toString(), Matchers.endsWith("org/springframework/web/servlet/view/"));
|
||||
}
|
||||
|
||||
@Test // SPR-13379
|
||||
public void contentType() throws Exception {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
this.wac.setServletContext(servletContext);
|
||||
this.wac.refresh();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
this.view.setEngine(mock(InvocableScriptEngine.class));
|
||||
this.view.setRenderFunction("render");
|
||||
this.view.setResourceLoaderPath(RESOURCE_LOADER_PATH);
|
||||
this.view.setApplicationContext(this.wac);
|
||||
|
||||
this.view.render(model, request, response);
|
||||
assertEquals(MediaType.TEXT_HTML_VALUE + ";charset=" +
|
||||
StandardCharsets.UTF_8, response.getHeader(HttpHeaders.CONTENT_TYPE));
|
||||
|
||||
response = new MockHttpServletResponse();
|
||||
this.view.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
this.view.render(model, request, response);
|
||||
assertEquals(MediaType.TEXT_PLAIN_VALUE + ";charset=" +
|
||||
StandardCharsets.UTF_8, response.getHeader(HttpHeaders.CONTENT_TYPE));
|
||||
|
||||
response = new MockHttpServletResponse();
|
||||
this.view.setCharset(StandardCharsets.ISO_8859_1);
|
||||
this.view.render(model, request, response);
|
||||
assertEquals(MediaType.TEXT_PLAIN_VALUE + ";charset=" +
|
||||
StandardCharsets.ISO_8859_1, response.getHeader(HttpHeaders.CONTENT_TYPE));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private interface InvocableScriptEngine extends ScriptEngine, Invocable {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user