use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class KotlinScriptTemplateTests method setup.
@BeforeEach
public void setup() {
this.webAppContext = mock(WebApplicationContext.class);
this.servletContext = new MockServletContext();
this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webAppContext);
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ScriptTemplateViewTests method contentType.
// SPR-13379
@Test
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<>();
this.view.setEngine(mock(InvocableScriptEngine.class));
this.view.setRenderFunction("render");
this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/");
this.view.setUrl("empty.txt");
this.view.setApplicationContext(this.wac);
this.view.render(model, request, response);
assertThat(response.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo((MediaType.TEXT_HTML_VALUE + ";charset=" + StandardCharsets.UTF_8));
response = new MockHttpServletResponse();
this.view.setContentType(MediaType.TEXT_PLAIN_VALUE);
this.view.render(model, request, response);
assertThat(response.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo((MediaType.TEXT_PLAIN_VALUE + ";charset=" + StandardCharsets.UTF_8));
response = new MockHttpServletResponse();
this.view.setCharset(StandardCharsets.ISO_8859_1);
this.view.render(model, request, response);
assertThat(response.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo((MediaType.TEXT_PLAIN_VALUE + ";charset=" + StandardCharsets.ISO_8859_1));
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ScriptTemplateViewTests method missingTemplate.
@Test
public void missingTemplate() throws Exception {
MockServletContext servletContext = new MockServletContext();
this.wac.setServletContext(servletContext);
this.wac.refresh();
this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/");
this.view.setUrl("missing.txt");
this.view.setEngine(mock(InvocableScriptEngine.class));
this.configurer.setRenderFunction("render");
this.view.setApplicationContext(this.wac);
assertThat(this.view.checkResource(Locale.ENGLISH)).isFalse();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class InternalResourceViewTests method forward.
@Test
public void forward() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
request.setContextPath("/mycontext");
request.setServletPath("/myservlet");
request.setPathInfo(";mypathinfo");
request.setQueryString("?param1=value1");
view.setUrl(url);
view.setServletContext(new MockServletContext() {
@Override
public int getMinorVersion() {
return 4;
}
});
view.render(model, request, response);
assertThat(response.getForwardedUrl()).isEqualTo(url);
model.forEach((key, value) -> assertThat(request.getAttribute(key)).as("Values for model key '" + key + "' must match").isEqualTo(value));
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceBundleViewResolverTests method setUp.
@BeforeEach
public void setUp() throws Exception {
rb.setBasename(PROPS_FILE);
rb.setCache(getCache());
rb.setDefaultParentView("testParent");
wac.setServletContext(new MockServletContext());
wac.refresh();
// This will be propagated to views, so we need it.
rb.setApplicationContext(wac);
}
Aggregations